BeamSearchDecoder¶
- class paddle.fluid.contrib.decoder.beam_search_decoder. BeamSearchDecoder ( state_cell, init_ids, init_scores, target_dict_dim, word_dim, input_var_dict={}, topk_size=50, sparse_emb=True, max_len=100, beam_size=1, end_id=1, name=None ) [source]
-
A beam search decoder that can be used for inference. The decoder should be initialized with a StateCell object. The decode process can be defined within its block.
- Parameters
-
state_cell (StateCell) – A StateCell object that handles the input and state variables.
init_ids (Variable) – The init beam search token ids.
init_scores (Variable) – The associated score of each id.
target_dict_dim (int) – Size of dictionary.
word_dim (int) – Word embedding dimension.
input_var_dict (dict) – A feeding dict to feed the required input variables to the state cell. It will be used by state_cell ‘s compute method. Default empty.
topk_size (int) – The topk size used for beam search. Default 50.
max_len (int) – The maximum allowed length of the generated sentence. Default 100.
beam_size (int) – The beam width of beam search decode. Default 1.
end_id (int) – The id of end token within beam search.
name (str) – The name of this decoder. Default None.
- Returns
-
A initialized BeamSearchDecoder object.
- Return type
-
BeamSearchDecoder
Examples: .. code-block:: python
- decoder = BeamSearchDecoder(
-
state_cell=state_cell, init_ids=init_ids, init_scores=init_scores, target_dict_dim=target_dict_dim, word_dim=word_dim, init_var_dict={}, topk_size=topk_size, sparse_emb=IS_SPARSE, max_len=max_length, beam_size=beam_size, end_id=1, name=None
) decoder.decode() translation_ids, translation_scores = decoder()
-
block
(
)
block¶
-
Define the behavior of the decoder for each RNN time step.
-
early_stop
(
)
early_stop¶
-
Stop the generation process in advance. Could be used as “break”.
-
decode
(
)
decode¶
-
Set up the computation within the decoder. Then you could call the decoder to get the result of beam search decode. If you want to define a more specific decoder, you could override this function.
Examples: .. code-block:: python
decoder.decode() translation_ids, translation_scores = decoder()
-
read_array
(
init,
is_ids=False,
is_scores=False
)
read_array¶
-
Read an array to get the decoded ids and scores generated by previous RNN step. At the first step of RNN, the init variable mut be used to initialize the array.
- Parameters
-
init (Variable) – The initial variable for first step usage. init must be provided.
is_ids (bool) – Specify whether the variable is an id.
is_scores (bool) – Specify whether the variable is a score.
- Returns
-
The associated variable generated during previous RNN steps.
Examples
-
update_array
(
array,
value
)
update_array¶
-
Store the value generated in current step in an array for each RNN step. This array could be accessed by read_array method.
- Parameters
-
array (Variable) – The array to append the new variable to.
value (Variable) – The newly generated value to be stored.