PaddlePaddle
- abs
- acos
- add
- add_n
- addmm
- all
- allclose
- any
- arange
- argmax
- argmin
- argsort
- asin
- assign
- atan
- bernoulli
- bmm
- broadcast_to
- cast
- ceil
- cholesky
- chunk
- clip
- concat
- conj
- cos
- cosh
- CPUPlace
- cross
- CUDAPinnedPlace
- CUDAPlace
- cumsum
- DataParallel
- diag
- disable_static
- dist
- divide
- dot
- empty
- empty_like
- enable_static
- equal
- equal_all
- erf
- exp
- expand
- expand_as
- eye
- flatten
- flip
- floor
- floor_divide
- flops
- full
- full_like
- gather
- gather_nd
- get_cuda_rng_state
- get_cudnn_version
- get_default_dtype
- get_device
- grad
- greater_equal
- greater_than
- histogram
- imag
- in_dynamic_mode
- increment
- index_sample
- index_select
- inverse
- is_compiled_with_cuda
- is_compiled_with_xpu
- is_empty
- is_tensor
- isfinite
- isinf
- isnan
- kron
- less_equal
- less_than
- linspace
- load
- log
- log10
- log1p
- log2
- logical_and
- logical_not
- logical_or
- logical_xor
- logsumexp
- masked_select
- matmul
- max
- maximum
- mean
- median
- meshgrid
- min
- minimum
- mm
- mod
- Model
- multinomial
- multiplex
- multiply
- mv
- no_grad
- nonzero
- norm
- normal
- not_equal
- numel
- ones
- ones_like
- ParamAttr
- pow
- prod
- rand
- randint
- randn
- randperm
- rank
- real
- reciprocal
- reshape
- reshape_
- roll
- round
- rsqrt
- save
- scale
- scatter
- scatter_
- scatter_nd
- scatter_nd_add
- seed
- set_cuda_rng_state
- set_default_dtype
- set_device
- shape
- shard_index
- sign
- sin
- sinh
- slice
- sort
- split
- sqrt
- square
- squeeze
- squeeze_
- stack
- stanh
- std
- strided_slice
- subtract
- sum
- summary
- t
- tan
- tanh
- tanh_
- Tensor
- tile
- to_tensor
- topk
- trace
- transpose
- tril
- triu
- unbind
- uniform
- unique
- unsqueeze
- unsqueeze_
- unstack
- var
- where
- XPUPlace
- zeros
- zeros_like
- create_lod_tensor
- create_random_int_lodtensor
- cuda_pinned_places
- data
- DataFeedDesc
- DataFeeder
- device_guard
- DistributeTranspiler
- DistributeTranspilerConfig
- get_flags
-
- adaptive_pool2d
- adaptive_pool3d
- add_position_encoding
- affine_channel
- affine_grid
- anchor_generator
- argmax
- argmin
- argsort
- array_length
- array_read
- array_write
- assign
- autoincreased_step_counter
- BasicDecoder
- beam_search
- beam_search_decode
- bipartite_match
- box_clip
- box_coder
- box_decoder_and_assign
- bpr_loss
- brelu
- Categorical
- center_loss
- clip
- clip_by_norm
- collect_fpn_proposals
- concat
- cond
- continuous_value_model
- cosine_decay
- create_array
- create_py_reader_by_data
- create_tensor
- crop
- crop_tensor
- cross_entropy
- ctc_greedy_decoder
- cumsum
- data
- DecodeHelper
- Decoder
- deformable_conv
- deformable_roi_pooling
- density_prior_box
- detection_output
- diag
- distribute_fpn_proposals
- double_buffer
- dropout
- dynamic_gru
- dynamic_lstm
- dynamic_lstmp
- DynamicRNN
- edit_distance
- elementwise_add
- elementwise_div
- elementwise_floordiv
- elementwise_max
- elementwise_min
- elementwise_mod
- elementwise_pow
- elementwise_sub
- elu
- embedding
- equal
- expand
- expand_as
- exponential_decay
- eye
- fc
- fill_constant
- filter_by_instag
- flatten
- fsp_matrix
- gather
- gather_nd
- gaussian_random
- gelu
- generate_mask_labels
- generate_proposal_labels
- generate_proposals
- get_tensor_from_selected_rows
- greater_equal
- greater_than
- GreedyEmbeddingHelper
- grid_sampler
- gru_unit
- GRUCell
- hard_shrink
- hard_sigmoid
- hard_swish
- has_inf
- has_nan
- hash
- hsigmoid
- huber_loss
- IfElse
- im2sequence
- image_resize
- image_resize_short
- increment
- inplace_abn
- inverse_time_decay
- iou_similarity
- isfinite
- kldiv_loss
- l2_normalize
- label_smooth
- leaky_relu
- less_equal
- less_than
- linear_chain_crf
- linear_lr_warmup
- locality_aware_nms
- lod_append
- lod_reset
- logsigmoid
- lrn
- lstm
- lstm_unit
- LSTMCell
- margin_rank_loss
- matmul
- matrix_nms
- maxout
- mean
- merge_selected_rows
- mse_loss
- mul
- multiclass_nms
- MultivariateNormalDiag
- natural_exp_decay
- noam_decay
- Normal
- not_equal
- one_hot
- ones
- ones_like
- pad
- pad2d
- pad_constant_like
- piecewise_decay
- pixel_shuffle
- polygon_box_transform
- polynomial_decay
- pool2d
- pool3d
- pow
- prior_box
- prroi_pool
- psroi_pool
- py_reader
- random_crop
- range
- rank_loss
- read_file
- reduce_all
- reduce_any
- reduce_max
- reduce_mean
- reduce_min
- reduce_prod
- reduce_sum
- relu
- relu6
- reorder_lod_tensor_by_rank
- reshape
- resize_bilinear
- resize_nearest
- resize_trilinear
- retinanet_detection_output
- retinanet_target_assign
- reverse
- rnn
- RNNCell
- roi_align
- roi_perspective_transform
- roi_pool
- rpn_target_assign
- sampled_softmax_with_cross_entropy
- SampleEmbeddingHelper
- sampling_id
- scatter
- selu
- sequence_concat
- sequence_conv
- sequence_enumerate
- sequence_expand
- sequence_expand_as
- sequence_first_step
- sequence_last_step
- sequence_mask
- sequence_pad
- sequence_pool
- sequence_reshape
- sequence_reverse
- sequence_scatter
- sequence_slice
- sequence_softmax
- sequence_unpad
- shuffle_channel
- sigmoid_cross_entropy_with_logits
- sigmoid_focal_loss
- sign
- similarity_focus
- size
- smooth_l1
- soft_relu
- softmax
- softplus
- softshrink
- softsign
- space_to_depth
- split
- squeeze
- ssd_loss
- stack
- StaticRNN
- strided_slice
- sum
- sums
- swish
- Switch
- tanh
- tanh_shrink
- target_assign
- teacher_student_sigmoid_loss
- tensor_array_to_tensor
- thresholded_relu
- topk
- TrainingHelper
- unbind
- Uniform
- uniform_random
- unique
- unique_with_counts
- unsqueeze
- warpctc
- where
- While
- while_loop
- yolo_box
- yolov3_loss
- zeros
- zeros_like
- load_op_library
- LoDTensor
- LoDTensorArray
- memory_optimize
- one_hot
- release_memory
- require_version
- set_flags
- Tensor
- Overview
- AdaptiveAvgPool1D
- AdaptiveAvgPool2D
- AdaptiveAvgPool3D
- AdaptiveMaxPool1D
- AdaptiveMaxPool2D
- AdaptiveMaxPool3D
- AlphaDropout
- AvgPool1D
- AvgPool2D
- AvgPool3D
- BatchNorm
- BatchNorm1D
- BatchNorm2D
- BatchNorm3D
- BCELoss
- BCEWithLogitsLoss
- BeamSearchDecoder
- Bilinear
- BiRNN
- ClipGradByGlobalNorm
- ClipGradByNorm
- ClipGradByValue
- Conv1D
- Conv1DTranspose
- Conv2D
- Conv2DTranspose
- Conv3D
- Conv3DTranspose
- CosineSimilarity
- CrossEntropyLoss
- CTCLoss
- Dropout
- Dropout2D
- Dropout3D
- dynamic_decode
- ELU
- Embedding
- Flatten
-
- adaptive_avg_pool1d
- adaptive_avg_pool2d
- adaptive_avg_pool3d
- adaptive_max_pool1d
- adaptive_max_pool2d
- adaptive_max_pool3d
- affine_grid
- alpha_dropout
- avg_pool1d
- avg_pool2d
- avg_pool3d
- batch_norm
- bilinear
- binary_cross_entropy
- binary_cross_entropy_with_logits
- conv1d
- conv1d_transpose
- conv2d
- conv2d_transpose
- conv3d
- conv3d_transpose
- cosine_similarity
- cross_entropy
- ctc_loss
- diag_embed
- dice_loss
- dropout
- dropout2d
- dropout3d
- elu
- elu_
- embedding
- gather_tree
- gelu
- grid_sample
- hardshrink
- hardsigmoid
- hardswish
- hardtanh
- hsigmoid_loss
- instance_norm
- interpolate
- kl_div
- l1_loss
- label_smooth
- layer_norm
- leaky_relu
- linear
- local_response_norm
- log_loss
- log_sigmoid
- log_softmax
- margin_ranking_loss
- max_pool1d
- max_pool2d
- max_pool3d
- maxout
- mse_loss
- nll_loss
- normalize
- npair_loss
- one_hot
- pad
- pixel_shuffle
- prelu
- relu
- relu6
- relu_
- selu
- sigmoid
- sigmoid_focal_loss
- smooth_l1_loss
- softmax
- softmax_
- softmax_with_cross_entropy
- softplus
- softshrink
- softsign
- square_error_cost
- swish
- tanhshrink
- temporal_shift
- thresholded_relu
- unfold
- upsample
- GELU
- GroupNorm
- GRU
- GRUCell
- Hardshrink
- Hardsigmoid
- Hardswish
- Hardtanh
- HSigmoidLoss
- InstanceNorm1D
- InstanceNorm2D
- InstanceNorm3D
- KLDivLoss
- L1Loss
- Layer
- LayerList
- LayerNorm
- LeakyReLU
- Linear
- LocalResponseNorm
- LogSigmoid
- LogSoftmax
- LSTM
- LSTMCell
- MarginRankingLoss
- Maxout
- MaxPool1D
- MaxPool2D
- MaxPool3D
- MSELoss
- MultiHeadAttention
- NLLLoss
- Pad1D
- Pad2D
- Pad3D
- PairwiseDistance
- ParameterList
- PixelShuffle
- PReLU
- ReLU
- ReLU6
- RNN
- RNNCellBase
- SELU
- Sequential
- Sigmoid
- SimpleRNN
- SimpleRNNCell
- SmoothL1Loss
- Softmax
- Softplus
- Softshrink
- Softsign
- SpectralNorm
- Swish
- SyncBatchNorm
- Tanh
- Tanhshrink
- ThresholdedReLU
- Transformer
- TransformerDecoder
- TransformerDecoderLayer
- TransformerEncoder
- TransformerEncoderLayer
- Upsample
- UpsamplingBilinear2D
- UpsamplingNearest2D
- append_backward
- BuildStrategy
- CompiledProgram
- cpu_places
- create_global_var
- create_parameter
- cuda_places
- data
- default_main_program
- default_startup_program
- deserialize_persistables
- deserialize_program
- device_guard
- ExecutionStrategy
- Executor
- global_scope
- gradients
- InputSpec
- load
- load_from_file
- load_inference_model
- load_program_state
- name_scope
- ParallelExecutor
- Program
- program_guard
- py_func
- save
- save_inference_model
- save_to_file
- scope_guard
- serialize_persistables
- serialize_program
- set_program_state
- Variable
- WeightNormParamAttr
-
- adjust_brightness
- adjust_contrast
- adjust_hue
- adjust_saturation
- BaseTransform
- BrightnessTransform
- center_crop
- CenterCrop
- ColorJitter
- Compose
- ContrastTransform
- crop
- Grayscale
- hflip
- HueTransform
- Normalize
- normalize
- Pad
- pad
- RandomCrop
- RandomHorizontalFli
- RandomResizedCrop
- RandomRotation
- RandomVerticalFlip
- Resize
- resize
- rotate
- SaturationTransform
- to_grayscale
- to_tensor
- ToTensor
- Transpose
- vflip
paddle.nn / BeamSearchDecoder
BeamSearchDecoder¶
-
class
paddle.nn.
BeamSearchDecoder
( cell, start_token, end_token, beam_size, embedding_fn=None, output_fn=None ) [源代码] ¶
带beam search解码策略的解码器。该接口包装一个cell来计算概率,然后执行一个beam search步骤计算得分,并为每个解码步骤选择候选输出。更多详细信息请参阅 Beam search
注意 在使用beam search解码时,cell的输入和状态将被扩展到 \(beam\_size\) ,得到 \([batch\_size * beam\_size, ...]\) 一样的形状,这个操作在BeamSearchDecoder中自动完成,因此,其他任何在 cell.call
中使用的tensor,如果形状为 \([batch\_size, ...]\) ,都必须先手动使用 BeamSearchDecoder.tile_beam_merge_with_batch
接口扩展。最常见的情况是带注意机制的编码器输出。
- 参数:
-
cell (RNNCell) - RNNCell的实例或者具有相同接口定义的对象。
start_token (int) - 起始标记id。
end_token (int) - 结束标记id。
beam_size (int) - 在beam search中使用的beam宽度。
embedding_fn (可选) - 处理选中的候选id的接口。它通常是一个将词id转换为词嵌入的嵌入层,其返回值将作为
cell.call
接口的input
参数。注意 ,这里要使用 cn_api_fluid_embedding 而非 embedding,因为选中的id的形状是 \([batch\_size, beam\_size]\) ,如果使用后者则还需要在这里提供unsqueeze。如果embedding_fn
未提供,则必须在cell.call
中实现词嵌入转换。默认值None。output_fn (可选) - 处理cell输出的接口,在计算得分和选择候选标记id之前使用。默认值None。
示例代码
import paddle
from paddle.nn import BeamSearchDecoder, dynamic_decode
from paddle.nn import GRUCell, Linear, Embedding
trg_embeder = Embedding(100, 32)
output_layer = Linear(32, 32)
decoder_cell = GRUCell(input_size=32, hidden_size=32)
decoder = BeamSearchDecoder(decoder_cell,
start_token=0,
end_token=1,
beam_size=4,
embedding_fn=trg_embeder,
output_fn=output_layer)
encoder_output = paddle.ones((4, 8, 32), dtype=paddle.get_default_dtype())
outputs = dynamic_decode(decoder=decoder,
inits=decoder_cell.get_initial_states(encoder_output),
max_step_num=10)
-
tile_beam_merge_with_batch
( x, beam_size ) ¶
扩展tensor的batch维度。此函数的输入是形状为 \([batch\_size, s_0, s_1, ...]\) 的tensor t,由minibatch中的样本 \(t[0], ..., t[batch\_size - 1]\) 组成。将其扩展为形状是 \([batch\_size * beam\_size, s_0, s_1, ...]\) 的tensor,由 \(t[0], t[0], ..., t[1], t[1], ...\) 组成, 每个minibatch中的样本重复 \(beam\_size\) 次。
- 参数:
-
x (Variable) - 形状为 \([batch\_size, ...]\) 的tenosr。数据类型应为float32,float64,int32,int64或bool。
beam_size (int) - 在beam search中使用的beam宽度。
返回:形状为 \([batch\_size * beam\_size, ...]\) 的tensor,其数据类型与 x
相同。
返回类型:Variable
-
_split_batch_beams
( x ) ¶
将形状为 \([batch\_size * beam\_size, ...]\) 的tensor变换为形状为 \([batch\_size, beam\_size, ...]\) 的新tensor。
- 参数:
-
x (Variable) - 形状为 \([batch\_size * beam\_size, ...]\) 的tenosr。数据类型应为float32,float64,int32,int64或bool。
返回:形状为 \([batch\_size, beam\_size, ...]\) 的tensor,其数据类型与 x
相同。
返回类型:Variable
-
_merge_batch_beams
( x ) ¶
将形状为 \([batch\_size, beam\_size, ...]\) 的tensor变换为形状为 \([batch\_size * beam\_size,...]\) 的新tensor。
- 参数:
-
x (Variable) - 形状为 \([batch\_size, beam_size,...]\) 的tenosr。数据类型应为float32,float64,int32,int64或bool。
返回:形状为 \([batch\_size * beam\_size, ...]\) 的tensor,其数据类型与 x
相同。
返回类型:Variable
-
_expand_to_beam_size
( x ) ¶
此函数输入形状为 \([batch\_size,s_0,s_1,...]\) 的tensor t,由minibatch中的样本 \(t[0],...,t[batch\_size-1]\) 组成。将其扩展为形状 \([ batch\_size,beam\_size,s_0,s_1,...]\) 的tensor,由 \(t[0],t[0],...,t[1],t[1],...\) 组成,其中每个minibatch中的样本重复 \(beam\_size\) 次。
- 参数:
-
x (Variable) - 形状为 \([batch\_size, ...]\) 的tenosr。数据类型应为float32,float64,int32,int64或bool。
返回:具有与 x
相同的形状和数据类型的tensor,其中未完成的beam保持不变,而已完成的beam被替换成特殊的tensor(tensor中所有概率质量被分配给EOS标记)。
返回类型:Variable
-
_mask_probs
( probs, finished ) ¶
屏蔽对数概率。该函数使已完成的beam将所有概率质量分配给EOS标记,而未完成的beam保持不变。
- 参数:
-
probs (Variable) - 形状为 \([batch\_size,beam\_size,vocab\_size]\) 的tensor,表示对数概率。其数据类型应为float32。
finish (Variable) - 形状为 \([batch\_size,beam\_size]\) 的tensor,表示所有beam的完成状态。其数据类型应为bool。
返回:具有与 x
相同的形状和数据类型的tensor,其中未完成的beam保持不变,而已完成的beam被替换成特殊的tensor(tensor中所有概率质量被分配给EOS标记)。
返回类型:Variable
-
_gather
( x, indices, batch_size ) ¶
对tensor x
根据索引 indices
收集。
- 参数:
-
x (Variable) - 形状为 \([batch\_size, beam\_size,...]\) 的tensor。
index (Variable) - 一个形状为 \([batch\_size, beam\_size]\) 的int64 tensor,表示我们用来收集的索引。
batch_size (Variable) - 形状为 \([1]\) 的tensor。其数据类型应为int32或int64。
返回:具有与 :code:``x` 相同的形状和数据类型的tensor,表示收集后的tensor。
返回类型:Variable
-
initialize
( initial_cell_states ) ¶
初始化BeamSearchDecoder。
- 参数:
-
initial_cell_states (Variable) - 单个tensor变量或tensor变量组成的嵌套结构。调用者提供的参数。
返回:一个元组 (initial_inputs, initial_states, finished)
。initial_inputs
是一个tensor,当 embedding_fn
为None时,该tensor t的形状为 \([batch\_size,beam\_size]\) ,值为 start_token
;否则使用 embedding_fn(t)
返回的值。initial_states
是tensor变量的嵌套结构(命名元组,字段包括 cell_states,log_probs,finished,lengths
),其中 log_probs,finished,lengths
都含有一个tensor,形状为 \([batch\_size, beam\_size]\),数据类型为float32,bool,int64。cell_states
具有与输入参数 initial_cell_states
相同结构的值,但形状扩展为 \([batch\_size,beam\_size,...]\)。 finished
是一个布尔型tensor,由False填充,形状为 \([batch\_size,beam\_size]\)。
返回类型:tuple
-
_beam_search_step
( time, logits, next_cell_states, beam_state ) ¶
计算得分并选择候选id。
- 参数:
-
time (Variable) - 调用者提供的形状为[1]的tensor,表示当前解码的时间步长。其数据类型为int64。
logits (Variable) - 形状为 \([batch\_size,beam\_size,vocab\_size]\) 的tensor,表示当前时间步的logits。其数据类型为float32。
next_cell_states (Variable) - 单个tensor变量或tensor变量组成的嵌套结构。它的结构,形状和数据类型与
initialize()
的返回值initial_states
中的cell_states
相同。它代表该cell的下一个状态。beam_state (Variable) - tensor变量的结构。在第一个解码步骤与
initialize()
返回的initial_states
同,其他步骤与step()
返回的beam_search_state
相同。
返回:一个元组 (beam_search_output, beam_search_state)
。beam_search_output
是tensor变量的命名元组,字段为 scores,predicted_ids parent_ids
。其中 scores,predicted_ids,parent_ids
都含有一个tensor,形状为 \([batch\_size,beam\_size]\),数据类型为float32 ,int64,int64。beam_search_state
具有与输入参数 beam_state
相同的结构,形状和数据类型。
返回类型:tuple
-
step
( time, inputs, states, **kwargs ) ¶
执行beam search解码步骤,该步骤使用 cell
来计算概率,然后执行beam search步骤以计算得分并选择候选标记ID。
- 参数:
-
time (Variable) - 调用者提供的形状为[1]的tensor,表示当前解码的时间步长。其数据类型为int64。。
inputs (Variable) - tensor变量。在第一个解码时间步时与由
initialize()
返回的initial_inputs
相同,其他时间步与由step()
返回的next_inputs
相同。states (Variable) - tensor变量的结构。在第一个解码时间步时与
initialize()
返回的initial_states
相同,其他时间步与由step()
返回的beam_search_state
相同。kwargs - 附加的关键字参数,由调用者提供。
返回:一个元组 (beam_search_output,beam_search_state,next_inputs,finish)
。beam_search_state
和参数 states
具有相同的结构,形状和数据类型。 next_inputs
与输入参数 inputs
具有相同的结构,形状和数据类型。 beam_search_output
是tensor变量的命名元组(字段包括 scores,predicted_ids,parent_ids
),其中 scores,predicted_ids,parent_ids
都含有一个tensor,形状为 \([batch\_size,beam\_size]\),数据类型为float32 ,int64,int64。finished
是一个bool类型的tensor,形状为 \([batch\_size,beam\_size]\)。
返回类型:tuple
-
finalize
( outputs, final_states, sequence_lengths ) ¶
使用 gather_tree
沿beam search树回溯并构建完整的预测序列。
- 参数:
-
outputs (Variable) - tensor变量组成的结构(命名元组),该结构和数据类型与
output_dtype
相同。tensor将所有时间步的输出堆叠,因此具有形状 \([time\_step,batch\_size,...]\)。final_states (Variable) - tensor变量组成的结构(命名元组)。它是
decoder.step
在最后一个解码步骤返回的next_states
,因此具有与任何时间步的state
相同的结构、形状和数据类型。sequence_lengths (Variable) - tensor,形状为 \([batch\_size,beam\_size]\),数据类型为int64。它包含解码期间确定的每个beam的序列长度。
返回:一个元组 (predicted_ids, final_states)
。predicted_ids
是一个tensor,形状为 \([time\_step,batch\_size,beam\_size]\),数据类型为int64。final_states
与输入参数 final_states
相同。
返回类型:tuple
此页内容是否对您有帮助
感谢反馈!