declarative programming (static graph)
dynamic_decode(decoder, inits=None, max_step_num=None, output_time_major=False, impute_finished=False, is_test=False, return_length=False, **kwargs)
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
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, 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.
(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
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 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], dtype="float32") trg_embeder = lambda x: fluid.embedding( x, size=[10000, 128], param_attr=fluid.ParamAttr(name="trg_embedding")) output_layer = lambda x: layers.fc(x, size=10000, num_flatten_dims=len(x.shape) - 1, param_attr=fluid.ParamAttr(name= "output_w"), bias_attr=False) decoder_cell = GRUCell(hidden_size=128) decoder = BeamSearchDecoder(decoder_cell, start_token=0, end_token=1, beam_size=4, embedding_fn=trg_embeder, output_fn=output_layer) outputs = dynamic_decode( decoder=decoder, inits=decoder_cell.get_initial_states(encoder_output))