GreedyEmbeddingHelper¶
- class paddle.fluid.layers. GreedyEmbeddingHelper ( embedding_fn, start_tokens, end_token ) [source]
-
GreedyEmbeddingHelper is a subclass of DecodeHelper. It is a decoding helper uses the argmax of the output (treated as logits) and passes the results through an embedding layer to get inputs 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.GreedyEmbeddingHelper(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
(
)
initialize¶
-
GreedyEmbeddingHelper initialization produces inputs for the first decoding step by using start_tokens of the constructor, and gives initial status telling whether each sequence in the batch is finished. It is the partial of the initialization of BasicDecoder.
- Returns
-
-
A tuple(
(initial_inputs, initial_finished)
). -
initial_inputs is same as start_tokens of the constructor. initial_finished is a bool tensor filled by False and has the same shape as start_tokens.
-
A tuple(
- Return type
-
tuple
-
sample
(
time,
outputs,
states
)
sample¶
-
Perform sampling by using argmax according to the outputs.
- Parameters
-
time (Variable) – An int64 tensor with shape [1] provided by the caller, representing the current time step number of decoding.
outputs (Variable) – A tensor variable. Usually it’s data type is float32 or float64, and it’s shape is [batch_size, vocabulary_size], representing the predicted logits of current step. It is same as outputs returned by BasicDecoder.output_fn(BasicDecoder.cell.call()).
states (Variable) – A (possibly nested structure of) tensor variable[s]. It is same as new_states returned by BasicDecoder.cell.call().
- Returns
-
- An int64 tensor with shape [batch_size], representing
-
the sampled ids.
- Return type
-
Variable
-
next_inputs
(
time,
outputs,
states,
sample_ids
)
next_inputs¶
-
Generate inputs for the next decoding step by applying embedding_fn to sample_ids. Simultaneously, produce the states for next time step by directly using the input states and emit status telling whether each minibatch entry gets an end_token sample.
- Parameters
-
time (Variable) – An int64 tensor with shape [1] provided by the caller, representing the current time step number of decoding.
outputs (Variable) – A tensor variable. Usually it’s data type is float32 or float64, and it’s shape is [batch_size, vocabulary_size], representing the predicted logits of current step. It is same as outputs returned by BasicDecoder.output_fn(BasicDecoder.cell.call()).
states (Variable) – A (possibly nested structure of) tensor variable[s]. It is same as new_states returned by BasicDecoder.cell.call().
sample_ids (Variable) – An int64 tensor variable shaped [batch_size]. It is same as sample_ids returned by sample().
- Returns
-
-
A tuple(
(finished, next_inputs, next_states)
). -
next_inputs and next_states both are a (possibly nested structure of) tensor variable[s], and the tensor’s shape is [batch_size, …]. next_states is identical to the input argument states. finished is a bool Tensor with shape [batch_size].
-
A tuple(
- Return type
-
tuple
-
initialize
(
)