opennmt.layers.bridge module

Define bridges: logic of passing the encoder state to the decoder.

opennmt.layers.bridge.assert_state_is_compatible(expected_state, state)[source]

Asserts that states are compatible.

Parameters:
  • expected_state – The reference state.
  • state – The state that must be compatible with expected_state.
Raises:

ValueError – if the states are incompatible.

class opennmt.layers.bridge.Bridge(trainable=True, name=None, dtype=None, **kwargs)[source]

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

Base class for bridges.

__call__(encoder_state, decoder_zero_state)[source]

Returns the initial decoder state.

Parameters:
  • encoder_state – The encoder state.
  • decoder_zero_state – The default decoder state.
Returns:

The decoder initial state.

call(states)[source]

This is where the layer’s logic lives.

Parameters:
  • inputs – Input tensor, or list/tuple of input tensors.
  • **kwargs – Additional keyword arguments.
Returns:

A tensor or list/tuple of tensors.

class opennmt.layers.bridge.CopyBridge(trainable=True, name=None, dtype=None, **kwargs)[source]

Bases: opennmt.layers.bridge.Bridge

A bridge that passes the encoder state as is.

call(states)[source]

This is where the layer’s logic lives.

Parameters:
  • inputs – Input tensor, or list/tuple of input tensors.
  • **kwargs – Additional keyword arguments.
Returns:

A tensor or list/tuple of tensors.

class opennmt.layers.bridge.ZeroBridge(trainable=True, name=None, dtype=None, **kwargs)[source]

Bases: opennmt.layers.bridge.Bridge

A bridge that does not pass information from the encoder.

call(states)[source]

This is where the layer’s logic lives.

Parameters:
  • inputs – Input tensor, or list/tuple of input tensors.
  • **kwargs – Additional keyword arguments.
Returns:

A tensor or list/tuple of tensors.

class opennmt.layers.bridge.DenseBridge(activation=None)[source]

Bases: opennmt.layers.bridge.Bridge

A bridge that applies a parameterized linear transformation from the encoder state to the decoder state size.

__init__(activation=None)[source]

Initializes the bridge.

Parameters:activation – Activation function (a callable). Set it to None to maintain a linear activation.
build(input_shape)[source]

Creates the variables of the layer (optional, for subclass implementers).

This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call.

This is typically used to create the weights of Layer subclasses.

Parameters:input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).
call(states)[source]

This is where the layer’s logic lives.

Parameters:
  • inputs – Input tensor, or list/tuple of input tensors.
  • **kwargs – Additional keyword arguments.
Returns:

A tensor or list/tuple of tensors.