opennmt.layers.reducer module

Define reducers: objects that merge inputs.

opennmt.layers.reducer.pad_in_time(x, padding_length)[source]

Helper function to pad a tensor in the time dimension and retain the static depth dimension.

opennmt.layers.reducer.align_in_time(x, length)[source]

Aligns the time dimension of x with length.

opennmt.layers.reducer.pad_with_identity(x, sequence_length, max_sequence_length, identity_values=0, maxlen=None)[source]

Pads a tensor with identity values up to max_sequence_length.

Parameters:
  • x – A tf.Tensor of shape [batch_size, time, depth].
  • sequence_length – The true sequence length of x.
  • max_sequence_length – The sequence length up to which the tensor must contain identity values.
  • identity_values – The identity value.
  • maxlen – Size of the output time dimension. Default is the maximum value in obj:max_sequence_length.
Returns:

A tf.Tensor of shape [batch_size, maxlen, depth].

opennmt.layers.reducer.pad_n_with_identity(inputs, sequence_lengths, identity_values=0)[source]

Pads each input tensors with identity values up to max(sequence_lengths) for each batch.

Parameters:
  • inputs – A list of tf.Tensor.
  • sequence_lengths – A list of sequence length.
  • identity_values – The identity value.
Returns:

A tuple (padded, max_sequence_length) which are respectively a list of tf.Tensor where each tensor are padded with identity and the combined sequence length.

opennmt.layers.reducer.roll_sequence(tensor, offsets)[source]

Shifts sequences by an offset.

Parameters:
  • tensor – A tf.Tensor of shape [batch_size, time, ...].
  • offsets – The offset of each sequence.
Returns:

A tf.Tensor of the same shape as tensor with sequences shifted by offsets.

class opennmt.layers.reducer.Reducer[source]

Bases: object

Base class for reducers.

zip_and_reduce(x, y)[source]

Zips x with y and reduces all elements.

__call__(inputs, sequence_length=None)[source]

Reduces all input elements.

Parameters:
  • inputs – A list of tf.Tensor.
  • sequence_length – The length of each input, if reducing sequences.
Returns:

If sequence_length is set, a tuple (reduced_input, reduced_length), otherwise a reduced tf.Tensor only.

reduce(inputs)[source]

Shortcut for __call__.

reduce_sequence(inputs, sequence_lengths)[source]

Shortcut for __call__.

class opennmt.layers.reducer.SumReducer[source]

Bases: opennmt.layers.reducer.Reducer

A reducer that sums the inputs.

reduce(inputs)[source]

Shortcut for __call__.

reduce_sequence(inputs, sequence_lengths)[source]

Shortcut for __call__.

class opennmt.layers.reducer.MultiplyReducer[source]

Bases: opennmt.layers.reducer.Reducer

A reducer that multiplies the inputs.

reduce(inputs)[source]

Shortcut for __call__.

reduce_sequence(inputs, sequence_lengths)[source]

Shortcut for __call__.

class opennmt.layers.reducer.ConcatReducer(axis=-1)[source]

Bases: opennmt.layers.reducer.Reducer

A reducer that concatenates the inputs.

reduce(inputs)[source]

Shortcut for __call__.

reduce_sequence(inputs, sequence_lengths)[source]

Shortcut for __call__.

class opennmt.layers.reducer.JoinReducer[source]

Bases: opennmt.layers.reducer.Reducer

A reducer that joins its inputs in a single tuple.

reduce(inputs)[source]

Shortcut for __call__.

reduce_sequence(inputs, sequence_lengths)[source]

Shortcut for __call__.