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 )

initialize

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 ( cell_outputs, sample_ids )

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, / )

count

Return number of occurrences of value.

index ( value, start=0, stop=9223372036854775807, / )

index

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 )

step

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 )

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].

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