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]

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

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. A tuple (outputs, state, sequence_length).
class opennmt.encoders.rnn_encoder.BidirectionalRNNEncoder(num_layers, num_units, reducer=, cell_class=None, dropout=0.3, residual_connections=False)[source]

An encoder that encodes an input sequence in both directions.

__init__(num_layers, num_units, reducer=, 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. 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. 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]

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. A tuple (outputs, state, sequence_length).
class opennmt.encoders.rnn_encoder.GoogleRNNEncoder(num_layers, num_units, dropout=0.3)[source]

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

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. 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=, cell_class=None, **kwargs)[source]

A RNN sequence encoder.

Note

TensorFlow 2.0 version.

__init__(num_layers, num_units, bidirectional=False, residual_connections=False, dropout=0.3, reducer=, 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. A tuple (outputs, state, sequence_length).
class opennmt.encoders.rnn_encoder.GNMTEncoder(num_layers, num_units, dropout=0.3)[source]

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. ValueError – if num_layers < 2.