opennmt.encoders.rnn_encoder module

Define RNN-based encoders.

class opennmt.encoders.rnn_encoder.RNNEncoder(num_layers, num_units, cell_class=None, dropout=0.0, residual_connections=False)[source]

Bases: opennmt.encoders.encoder.Encoder

Base class for RNN encoders.

__init__(num_layers, num_units, cell_class=None, dropout=0.0, residual_connections=False)[source]

Common constructor to save parameters.

encode(inputs, sequence_length=None, mode='train')[source]

Encodes an input sequence.

Parameters:
  • inputs – The inputs to encode of shape \([B, T, ...]\).
  • sequence_length – The length of each input with shape \([B]\).
  • mode – A tf.estimator.ModeKeys mode.
Returns:

A tuple (outputs, state, sequence_length).

class opennmt.encoders.rnn_encoder.UnidirectionalRNNEncoder(num_layers, num_units, cell_class=None, dropout=0.3, residual_connections=False)[source]

Bases: opennmt.encoders.rnn_encoder.RNNEncoder

A simple RNN encoder.

__init__(num_layers, num_units, cell_class=None, dropout=0.3, residual_connections=False)[source]

Initializes the parameters of the encoder.

Parameters:
  • num_layers – The number of layers.
  • num_units – The number of units in each layer.
  • cell_class – The inner cell class or a callable taking num_units as argument and returning a cell. Defaults to a LSTM cell.
  • dropout – The probability to drop units in each layer output.
  • residual_connections – If True, each layer input will be added to its output.
encode(inputs, sequence_length=None, mode='train')[source]

Encodes an input sequence.

Parameters:
  • inputs – The inputs to encode of shape \([B, T, ...]\).
  • sequence_length – The length of each input with shape \([B]\).
  • mode – A tf.estimator.ModeKeys mode.
Returns:

A tuple (outputs, state, sequence_length).

class opennmt.encoders.rnn_encoder.BidirectionalRNNEncoder(num_layers, num_units, reducer=<opennmt.layers.reducer.SumReducer object>, cell_class=None, dropout=0.3, residual_connections=False)[source]

Bases: opennmt.encoders.rnn_encoder.RNNEncoder

An encoder that encodes an input sequence in both directions.

__init__(num_layers, num_units, reducer=<opennmt.layers.reducer.SumReducer object>, cell_class=None, dropout=0.3, residual_connections=False)[source]

Initializes the parameters of the encoder.

Parameters:
  • num_layers – The number of layers.
  • num_units – The number of units in each layer.
  • reducer – A opennmt.layers.reducer.Reducer instance to merge bidirectional state and outputs.
  • cell_class – The inner cell class or a callable taking num_units as argument and returning a cell. Defaults to a LSTM cell.
  • dropout – The probability to drop units in each layer output.
  • residual_connections – If True, each layer input will be added to its output.
Raises:

ValueError – when using opennmt.layers.reducer.ConcatReducer and num_units is not divisible by 2.

encode(inputs, sequence_length=None, mode='train')[source]

Encodes an input sequence.

Parameters:
  • inputs – The inputs to encode of shape \([B, T, ...]\).
  • sequence_length – The length of each input with shape \([B]\).
  • mode – A tf.estimator.ModeKeys mode.
Returns:

A tuple (outputs, state, sequence_length).

class opennmt.encoders.rnn_encoder.RNMTPlusEncoder(num_layers=6, num_units=1024, cell_class=None, dropout=0.3)[source]

Bases: opennmt.encoders.encoder.Encoder

The RNMT+ encoder described in https://arxiv.org/abs/1804.09849.

__init__(num_layers=6, num_units=1024, cell_class=None, dropout=0.3)[source]

Initializes the parameters of the encoder.

Parameters:
  • num_layers – The number of layers.
  • num_units – The number of units in each RNN layer and the final output.
  • cell_class – The inner cell class or a callable taking num_units as argument and returning a cell. Defaults to a layer normalized LSTM cell. For efficiency, consider using the standard tf.nn.rnn_cell.LSTMCell instead.
  • dropout – The probability to drop units in each layer output.
encode(inputs, sequence_length=None, mode='train')[source]

Encodes an input sequence.

Parameters:
  • inputs – The inputs to encode of shape \([B, T, ...]\).
  • sequence_length – The length of each input with shape \([B]\).
  • mode – A tf.estimator.ModeKeys mode.
Returns:

A tuple (outputs, state, sequence_length).

class opennmt.encoders.rnn_encoder.GoogleRNNEncoder(num_layers, num_units, dropout=0.3)[source]

Bases: opennmt.encoders.encoder.Encoder

The RNN encoder used in GNMT as described in https://arxiv.org/abs/1609.08144.

__init__(num_layers, num_units, dropout=0.3)[source]

Initializes the parameters of the encoder.

Parameters:
  • num_layers – The number of layers.
  • num_units – The number of units in each layer.
  • dropout – The probability to drop units in each layer output.
Raises:

ValueError – if num_layers < 2.

encode(inputs, sequence_length=None, mode='train')[source]

Encodes an input sequence.

Parameters:
  • inputs – The inputs to encode of shape \([B, T, ...]\).
  • sequence_length – The length of each input with shape \([B]\).
  • mode – A tf.estimator.ModeKeys mode.
Returns:

A tuple (outputs, state, sequence_length).

class opennmt.encoders.rnn_encoder.PyramidalRNNEncoder(num_layers, num_units, reduction_factor=2, cell_class=None, dropout=0.3)[source]

Bases: opennmt.encoders.encoder.Encoder

An encoder that reduces the time dimension after each bidirectional layer.

__init__(num_layers, num_units, reduction_factor=2, cell_class=None, dropout=0.3)[source]

Initializes the parameters of the encoder.

Parameters:
  • num_layers – The number of layers.
  • num_units – The number of units in each layer.
  • reduction_factor – The time reduction factor.
  • cell_class – The inner cell class or a callable taking num_units as argument and returning a cell. Defaults to a LSTM cell.
  • dropout – The probability to drop units in each layer output.
encode(inputs, sequence_length=None, mode='train')[source]

Encodes an input sequence.

Parameters:
  • inputs – The inputs to encode of shape \([B, T, ...]\).
  • sequence_length – The length of each input with shape \([B]\).
  • mode – A tf.estimator.ModeKeys mode.
Returns:

A tuple (outputs, state, sequence_length).

class opennmt.encoders.rnn_encoder.RNNEncoderV2(num_layers, num_units, bidirectional=False, residual_connections=False, dropout=0.3, reducer=<opennmt.layers.reducer.ConcatReducer object>, cell_class=None, **kwargs)[source]

Bases: opennmt.encoders.encoder.Encoder

A RNN sequence encoder.

Note

TensorFlow 2.0 version.

__init__(num_layers, num_units, bidirectional=False, residual_connections=False, dropout=0.3, reducer=<opennmt.layers.reducer.ConcatReducer object>, cell_class=None, **kwargs)[source]

Initializes the parameters of the encoder.

Parameters:
  • num_layers – The number of layers.
  • num_units – The number of units in each layer.
  • bidirectional – Use a bidirectional RNN.
  • residual_connections – If True, each layer input will be added to its output.
  • reducer – A opennmt.layers.reducer.Reducer instance to merge bidirectional state and outputs.
  • dropout – The probability to drop units in each layer output.
  • cell_class – The inner cell class or a callable taking num_units as argument and returning a cell. Defaults to a LSTM cell.
  • **kwargs – Additional layer arguments.
call(inputs, sequence_length=None, training=None)[source]

Encodes an input sequence.

Parameters:
  • inputs – The inputs to encode of shape \([B, T, ...]\).
  • sequence_length – The length of each input with shape \([B]\).
  • training – Run in training mode.
Returns:

A tuple (outputs, state, sequence_length).

class opennmt.encoders.rnn_encoder.GNMTEncoder(num_layers, num_units, dropout=0.3)[source]

Bases: opennmt.encoders.encoder.SequentialEncoder

The RNN encoder used in GNMT as described in https://arxiv.org/abs/1609.08144.

Note

TensorFlow 2.0 version.

__init__(num_layers, num_units, dropout=0.3)[source]

Initializes the parameters of the encoder.

Parameters:
  • num_layers – The number of layers.
  • num_units – The number of units in each layer.
  • dropout – The probability to drop units in each layer output.
Raises:

ValueError – if num_layers < 2.