StateCell

class paddle.fluid.contrib.decoder.beam_search_decoder. StateCell ( inputs, states, out_state, name=None ) [source]

The state cell class stores the hidden state of the RNN cell. A typical RNN cell has one or more hidden states, and one or more step inputs. This class allows you to defines the name of hidden states as well as step inputs, and their associated variables.

Parameters
  • inputs (dict) – A feeding dict of {name(str) : Variable}. It specifies the names of step inputs for RNN cell, and the associated variables. The variable could initially be None and set manually during each RNN step.

  • states (dict) – A feeding dict of {name(str) : InitState object}. It specifies the names of hidden states and their initialized state.

  • out_state (str) – A string that specifies the name of hidden state that will be used to compute the score in beam search process.

  • name (str) – The name of the RNN cell. Default None.

Raises

ValueError – If the initial state is not an instance of InitState, or the out_state is not in the dict of states.

Returns

The initialized StateCell object.

Return type

StateCell

Examples

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

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

.. code-block:: python
  hidden_state = InitState(init=encoder_out, need_reorder=True)
  state_cell = StateCell(
      inputs={'current_word': None},
      states={'h': hidden_state},
      out_state='h')

get_state ( state_name )

get_state

The getter of state object. Find the state variable by its name.

Parameters

state_name (str) – A string of the state’s name.

Returns

The associated state object.

get_input ( input_name )

get_input

The getter of input variable. Find the input variable by its name.

Parameters

input_name (str) – The string of the input’s name.

Returns

The associated input variable.

set_state ( state_name, state_value )

set_state

The setter of the state variable. Change the variable of the given state_name.

Parameters
  • state_name (str) – The name of the state to change.

  • state_value (Var) – The variable of the new state.

state_updater ( updater )

state_updater

Set up the updater to update the hidden state every RNN step. The behavior of updater could be customized by users. The updater should be a function that takes a StateCell object as input and update the hidden state within it. The hidden state could be accessed through get_state method.

Parameters

updater (func) – the updater to update the state cell.

compute_state ( inputs )

compute_state

Provide the step input of RNN cell, and compute the new hidden state with updater and give step input.

Parameters
  • inputs (dict) – A feed dict, {name(str): Variable}. name should be

  • cell (the names of step inputs for this RNN) –

  • be (and Variable should) –

  • variables. (the associated) –

Examples: .. code-block:: python

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/fluid/contrib/decoder/beam_search_decoder.py:docstring of paddle.fluid.contrib.decoder.beam_search_decoder.StateCell.compute_state, line 12)

Unexpected indentation.

state_cell.compute_state(inputs={‘x’: current_word})

update_states ( )

update_states

Update and record state information after each RNN step.

out_state ( )

out_state

Get the output state variable. This must be called after update_states.

Returns

The output variable of the RNN cell.