Decoder¶
- class paddle.fluid.layers. Decoder [source]
-
- api_attr
-
Static Graph
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
)
initialize¶
-
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.
-
A tuple(
- Return type
-
tuple
-
step
(
time,
inputs,
states,
**kwargs
)
step¶
-
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
)
finalize¶
-
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].
-
A tuple(
- Return type
-
tuple
- property tracks_own_finished
-
Describes whether the Decoder keeps track of finished states by itself.
decoder.step() would emit a bool finished value at each decoding step. The emited finished can be used to determine whether every batch entries is finished directly, or it can be combined with the finished tracker keeped in dynamic_decode by performing a logical OR to take the already finished into account.
If False, the latter would be took when performing dynamic_decode, which is the default. Otherwise, the former would be took, which uses the finished value emited by the decoder as all batch entry finished status directly, and it is the case when batch entries might be reordered such as beams in BeamSearchDecoder.
- Returns
-
A python bool False.
- Return type
-
bool