paddle.nn. dynamic_decode ( decoder, inits=None, max_step_num=None, output_time_major=False, impute_finished=False, is_test=False, return_length=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 reaches 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 outputs(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.

  • impute_finished (bool, optional) – If True and decoder.tracks_own_finished is False, then states get copied through for batch entries which are marked as finished, which differs with the unfinished using the new states returned by decoder.step() and ensures that the final states have the correct values. Otherwise, states wouldn’t be copied through when finished. If the returned final_states is needed, it should be set as True, which causes some slowdown. Default False.

  • is_test (bool, optional) – A flag indicating whether to use test mode. In test mode, it is more memory saving. Default False.

  • return_length (bool, optional) – A flag indicating whether to return an extra Tensor variable in the output tuple, which stores the actual lengths of all decoded sequences. Default False.

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


A tuple( (final_outputs, final_states, sequence_lengths) )

when return_length is True, otherwise a tuple( (final_outputs, final_states) ). The final outputs and states, both are Tensor or nested structure of Tensor. final_outputs has the same structure and data types as the outputs returned by decoder.step() , and each Tenser in final_outputs is the stacked of all decoding steps’ outputs, which might be revised by decoder.finalize() if the decoder has implemented 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. sequence_lengths is an int64 tensor with the same shape as finished returned by decoder.initialize() , and it stores the actual lengths of all decoded sequences.

Return type



import numpy as np
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,
encoder_output = paddle.ones((4, 8, 32), dtype=paddle.get_default_dtype())
outputs = dynamic_decode(decoder=decoder,