opennmt.utils.cell module

RNN cells helpers.

opennmt.utils.cell.build_cell(num_layers, num_units, mode, dropout=0.0, residual_connections=False, cell_class=None, attention_layers=None, attention_mechanisms=None)[source]

Convenience function to build a multi-layer RNN cell.

  • num_layers – The number of layers.
  • num_units – The number of units in each layer.
  • mode – A tf.estimator.ModeKeys mode.
  • dropout – The probability to drop units in each layer output.
  • residual_connections – If True, each layer input will be added to its output.
  • cell_class – The inner cell class or a callable taking num_units as argument and returning a cell. Defaults to a LSTM cell.
  • attention_layers – A list of integers, the layers after which to add attention.
  • attention_mechanisms – A list of tf.contrib.seq2seq.AttentionMechanism with the same length as attention_layers.

A tf.nn.rnn_cell.RNNCell.


ValueError – if attention_layers and attention_mechanisms do not have the same length.


Returns the last encoding vector from the state.

For example, this is the last hidden states of the last LSTM layer for a LSTM-based encoder.

Parameters:state – The encoder state.
Returns:The last encoding vector.