Decoder

api_attr

declarative programming (static graph)

class paddle.fluid.layers.Decoder[source]

Decoder is the base class for any decoder instance used in dynamic_decode. It provides interface for output generation for one time step, which can be used to generate sequences.

The key abstraction provided by Decoder is:

1. (initial_input, initial_state, finished) = initialize(inits) , which generates the input and state for the first decoding step, and gives the initial status telling whether each sequence in the batch is finished. It would be called once before the decoding iterations.

2. (output, next_state, next_input, finished) = step(time, input, state) , which transforms the input and state to the output and new state, generates input for the next decoding step, and emits the flag indicating finished status. It is the main part for each decoding iteration.

3. (final_outputs, final_state) = finalize(outputs, final_state, sequence_lengths) , which revises the outputs(stack of all time steps’ output) and final state(state from the last decoding step) to get the counterpart for special usage. Not necessary to be implemented if no need to revise the stacked outputs and state from the last decoding step. If implemented, it would be called after the decoding iterations.

Decoder is more general compared to RNNCell, since the returned next_input and finished make it can determine the input and when to finish by itself when used in dynamic decoding. Decoder always wraps a RNNCell instance though not necessary.

initialize(inits)

Called once before the decoding iterations.

Parameters

inits – Argument provided by the caller.

Returns

A tuple( (initial_inputs, initial_states, finished) ). initial_inputs and initial_states both are a (possibly nested structure of) tensor variable[s], and finished is a tensor with bool data type.

Return type

tuple

step(time, inputs, states, **kwargs)

Called per step of decoding.

Parameters
  • time (Variable) – A Tensor with shape \([1]\) provided by the caller. The data type is int64.

  • inputs (Variable) – A (possibly nested structure of) tensor variable[s].

  • states (Variable) – A (possibly nested structure of) tensor variable[s].

  • **kwargs – Additional keyword arguments, provided by the caller.

Returns

A tuple( :code:(outputs, next_states, next_inputs, finished)` ). next_inputs and next_states both are a (possibly nested structure of) tensor variable[s], and the structure, shape and data type must be same as the counterpart from input arguments. outputs is a (possibly nested structure of) tensor variable[s]. finished is a Tensor with bool data type.

Return type

tuple

finalize(outputs, final_states, sequence_lengths)

Called once after the decoding iterations if implemented.

Parameters
  • outputs (Variable) – A (possibly nested structure of) tensor variable[s]. The structure and data type is same as output_dtype. The tensor stacks all time steps’ output thus has shape \([time\_step, batch\_size, ...]\) , which is done by the caller.

  • final_states (Variable) – A (possibly nested structure of) tensor variable[s]. It is the next_states returned by decoder.step at last decoding step, thus has the same structure, shape and data type with states at any time step.

Returns

A tuple( (final_outputs, final_states) ). final_outputs and final_states both are a (possibly nested structure of) tensor variable[s].

Return type

tuple