paddle.fluid.layers.dynamic_decode(decoder, inits=None, max_step_num=None, output_time_major=False, **kwargs)[source]

Dynamic decoding performs decoder.step() repeatedly until the returned Tensor indicating finished status contains all True values or the number of decoding step reachs to max_step_num.

decoder.initialize() would be called once before the decoding loop. If the decoder has implemented finalize method, decoder.finalize() would be called once after the decoding loop.

  • decoder (Decoder) – An instance of Decoder.

  • inits (object, optional) – Argument passed to decoder.initialize. Default None.

  • max_step_num (int, optional) – The maximum number of steps. If not provided, decode until the decoder is fully done, or in other words, the returned Tensor by decoder.step() indicating finished status contains all True). Default None.

  • output_time_major (bool, optional) – Indicate the data layout of Tensor included in the final outpus(the first returned value of this method). If attr:False, the data layout would be batch major with shape [batch_size, seq_len, …]. If attr:True, the data layout would be time major with shape [seq_len, batch_size, …]. Default: False.

  • **kwargs – Additional keyword arguments. Arguments passed to decoder.step.


A tuple( (final_outputs, final_states) ) including the final outputs and states, both are Tensor or nested structure of Tensor. final_outputs has the same structure and data types as decoder.output_dtype , and each Tenser in final_outputs is the stacked of all decoding steps’ outputs, which might be revised by decoder.finalize . final_states is the counterpart at last time step of initial states returned by decoder.initialize , thus has the same structure with it and has tensors with same shapes and data types.

Return type



import paddle.fluid as fluid
import paddle.fluid.layers as layers
from paddle.fluid.layers import GRUCell, BeamSearchDecoder, dynamic_decode

encoder_output = fluid.data(name="encoder_output",
                        shape=[-1, 32, 128],
trg_embeder = lambda x: fluid.embedding(
    x, size=[10000, 128], param_attr=fluid.ParamAttr(name="trg_embedding"))
output_layer = lambda x: layers.fc(x,
                                num_flatten_dims=len(x.shape) - 1,
decoder_cell = GRUCell(hidden_size=128)
decoder = BeamSearchDecoder(decoder_cell,

outputs = dynamic_decode(
    decoder=decoder, inits=decoder_cell.get_initial_states(encoder_output))