dynamic_decode

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.

Parameters
  • 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.

Returns

  • final_outputs (Tensor, nested structure of Tensor), each Tensor in final_outputs is the stacked of all decoding steps’ outputs, which might be revised

    by decoder.finalize() if the decoder has implemented finalize. And final_outputs has the same structure and data types as the outputs returned by decoder.step()

  • final_states (Tensor, nested structure of Tensor), 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 (Tensor), stores the actual lengths of all decoded sequences.

    sequence_lengths is provided only if return_length is True.

Examples

>>> 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)
>>> print(outputs[0].shape)
[4, 11, 4]