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.

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


A initialized BeamSearchDecoder object.

Return type


Examples: .. code-block:: python

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/fluid/contrib/decoder/ of paddle.fluid.contrib.decoder.beam_search_decoder.BeamSearchDecoder, line 37)

Unexpected indentation.

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

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/fluid/contrib/decoder/ of paddle.fluid.contrib.decoder.beam_search_decoder.BeamSearchDecoder, line 50)

Definition list ends without a blank line; unexpected unindent.

) decoder.decode() translation_ids, translation_scores = decoder()

block ( )


Define the behavior of the decoder for each RNN time step.

early_stop ( )


Stop the generation process in advance. Could be used as “break”.

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

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/fluid/contrib/decoder/ of paddle.fluid.contrib.decoder.beam_search_decoder.BeamSearchDecoder.decode, line 7)

Unexpected indentation.

decoder.decode() translation_ids, translation_scores = decoder()

read_array ( init, is_ids=False, is_scores=False )


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.

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


The associated variable generated during previous RNN steps.


System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/fluid/contrib/decoder/ of paddle.fluid.contrib.decoder.beam_search_decoder.BeamSearchDecoder.read_array, line 17)

Error in “code-block” directive: maximum 1 argument(s) allowed, 9 supplied.

.. code-block:: python
  prev_ids = decoder.read_array(init=init_ids, is_ids=True)
  prev_scores = decoder.read_array(init=init_scores, is_scores=True)
update_array ( array, value )


Store the value generated in current step in an array for each RNN step. This array could be accessed by read_array method.

  • array (Variable) – The array to append the new variable to.

  • value (Variable) – The newly generated value to be stored.