opennmt.encoders.encoder module

Base class for encoders and generic multi encoders.

class opennmt.encoders.encoder.Encoder(trainable=True, name=None, dtype=None, **kwargs)[source]

Bases: tensorflow.python.keras.engine.base_layer.Layer

Base class for encoders.

build_mask(inputs, sequence_length=None, dtype=tf.bool)[source]

Builds a boolean mask for inputs.

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

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.encoder.SequentialEncoder(encoders, states_reducer=, transition_layer_fn=None)[source]

Bases: opennmt.encoders.encoder.Encoder

An encoder that executes multiple encoders sequentially with optional transition layers.

See for example “Cascaded Encoder” in https://arxiv.org/abs/1804.09849.

__init__(encoders, states_reducer=, transition_layer_fn=None)[source]

Initializes the parameters of the encoder.

Parameters:
  • encoders – A list of opennmt.encoders.encoder.Encoder.
  • states_reducer – A opennmt.layers.reducer.Reducer to merge all states.
  • transition_layer_fn – A callable or list of callables applied to the output of an encoder before passing it as input to the next. If it is a single callable, it is applied between every encoders. Otherwise, the i th callable will be applied between encoders i and i + 1.
Raises:

ValueError – if transition_layer_fn is a list with a size not equal to the number of encoder transitions len(encoders) - 1.

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.encoder.ParallelEncoder(encoders, outputs_reducer=, states_reducer=, outputs_layer_fn=None, combined_output_layer_fn=None, share_parameters=False)[source]

Bases: opennmt.encoders.encoder.Encoder

An encoder that encodes its input with several encoders and reduces the outputs and states together. Additional layers can be applied on each encoder output and on the combined output (e.g. to layer normalize each encoder output).

If the input is a single tf.Tensor, the same input will be encoded by every encoders. Otherwise, when the input is a Python sequence (e.g. the non reduced output of a opennmt.inputters.inputter.ParallelInputter), each encoder will encode its corresponding input in the sequence.

See for example “Multi-Columnn Encoder” in https://arxiv.org/abs/1804.09849.

__init__(encoders, outputs_reducer=, states_reducer=, outputs_layer_fn=None, combined_output_layer_fn=None, share_parameters=False)[source]

Initializes the parameters of the encoder.

Parameters:
  • encoders – A list of opennmt.encoders.encoder.Encoder or a single one, in which case the same encoder is applied to each input.
  • outputs_reducer – A opennmt.layers.reducer.Reducer to merge all outputs. If None, defaults to opennmt.layers.reducer.JoinReducer.
  • states_reducer – A opennmt.layers.reducer.Reducer to merge all states. If None, defaults to opennmt.layers.reducer.JoinReducer.
  • outputs_layer_fn – A callable or list of callables applied to the encoders outputs If it is a single callable, it is on each encoder output. Otherwise, the i th callable is applied on encoder i output.
  • combined_output_layer_fn – A callable to apply on the combined output (i.e. the output of outputs_reducer).
  • share_parameters – If True, share parameters between the parallel encoders. For stateful encoders, simply pass a single encoder instance to encoders for parameter sharing.
Raises:

ValueError – if outputs_layer_fn is a list with a size not equal to the number of encoders.

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