Encoder

class opennmt.encoders.Encoder(*args, **kwargs)[source]

Base class for encoders.

Inherits from: keras.src.engine.base_layer.Layer

Extended by:

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

Builds a boolean mask for inputs.

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

__call__(inputs, sequence_length=None, **kwargs)[source]

Encodes an input sequence.

Parameters
  • inputs – A 3D tf.Tensor or tf.RaggedTensor.

  • sequence_length – A 1D tf.Tensor (optional if inputs is a tf.RaggedTensor).

  • training – Run the encoder in training mode.

Returns

If inputs is a tf.Tensor, the encoder returns a tuple (outputs, state, sequence_length). If inputs is a tf.RaggedTensor, the encoder returns a tuple (outputs, state), where outputs is a tf.RaggedTensor.