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

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]
. x – A

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 oftf.Tensor
where each tensor are padded with identity and the combined sequence length. inputs – A list of

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 astensor
with sequences shifted byoffsets
. tensor – A

class
opennmt.layers.reducer.
Reducer
[source]¶ Bases:
object
Base class for reducers.

class
opennmt.layers.reducer.
SumReducer
[source]¶ Bases:
opennmt.layers.reducer.Reducer
A reducer that sums the inputs.

class
opennmt.layers.reducer.
MultiplyReducer
[source]¶ Bases:
opennmt.layers.reducer.Reducer
A reducer that multiplies the inputs.

class
opennmt.layers.reducer.
ConcatReducer
(axis=1)[source]¶ Bases:
opennmt.layers.reducer.Reducer
A reducer that concatenates the inputs.