BasicDecoder¶
-
class
paddle.fluid.layers.
BasicDecoder
(cell, helper, output_fn=None)[source] BasicDecoder is a subclass of Decoder and assembles a RNNCell and DecodeHelper instance as members, where the DecodeHelper helps to implement customed decoding strategies.. It performs one decoding step as following steps:
1. Perform cell_outputs, cell_states = cell.call(inputs, states) to get outputs and new states from cell.
2. Perform sample_ids = helper.sample(time, cell_outputs, cell_states) to sample ids as decoded results of the current time step.
3. Perform finished, next_inputs, next_states = helper.next_inputs(time, cell_outputs, cell_states, sample_ids) to generate inputs, states and finished status for the next decoding step.
Examples
import paddle.fluid as fluid import paddle.fluid.layers as layers trg_emb = fluid.data(name="trg_emb", shape=[None, None, 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) helper = layers.SampleEmbeddingHelper(trg_embeder, start_tokens=0, end_token=1) decoder_cell = layers.GRUCell(hidden_size=128) decoder = layers.BasicDecoder(decoder_cell, helper, output_fn=output_layer) outputs = layers.dynamic_decode( decoder=decoder, inits=decoder_cell.get_initial_states(encoder_output))
-
initialize
(initial_cell_states) BasicDecoder initialization includes helper initialization and cell initialization, and cell initialization uses initial_cell_states as the result directly.
- Parameters
initial_cell_states (Variable) – A (possibly nested structure of) tensor variable[s]. An argument provided by the caller dynamic_decode.
- Returns
A tuple( :code:(initial_inputs, initial_cell_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. initial_inputs and finished are the results of helper.initialize(), and initial_cell_states is same as the input argument counterpart.
- Return type
tuple
-
class
OutputWrapper
The structure for the returned value outputs of decoder.step. A namedtuple includes cell_outputs, sample_ids as fields.
-
cell_outputs
Alias for field number 0
-
count
(value) → integer -- return number of occurrences of value
-
index
(value[, start[, stop]]) → integer -- return first index of value. Raises ValueError if the value is not present.
-
sample_ids
Alias for field number 1
-
-
step
(time, inputs, states, **kwargs) Perform one decoding step as following steps:
1. Perform cell_outputs, cell_states = cell.call(inputs, states) to get outputs and new states from cell.
2. Perform sample_ids = helper.sample(time, cell_outputs, cell_states) to sample ids as decoded results of the current time step.
3. Perform finished, next_inputs, next_states = helper.next_inputs(time, cell_outputs, cell_states, sample_ids) to generate inputs, states and finished status for the next decoding step.
- Parameters
time (Variable) – An int64 tensor with shape [1] provided by the caller, representing the current time step number of decoding.
inputs (Variable) – A tensor variable. It is same as initial_inputs returned by initialize() for the first decoding step and next_inputs returned by step() for the others.
states (Variable) – A structure of tensor variables. It is same as the initial_cell_states returned by initialize() for the first decoding step and next_states returned by step() for the others.
**kwargs – Additional keyword arguments, provided by the caller dynamic_decode.
- Returns
A tuple(
(outputs, next_states, next_inputs, finished)
). outputs is a namedtuple(including cell_outputs, sample_ids, as fields) of tensor variables, where cell_outputs is the result fof cell.call() and sample_ids is the result of helper.sample(). next_states and next_inputs have the same structure, shape and data type as the input arguments states and inputs separately. finished is a bool tensor with shape [batch_size].- 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
-