MCPcopy Create free account
hub / github.com/NVIDIA/FasterTransformer / pad_with_identity

Function pad_with_identity

examples/tensorflow/decoder/utils/reducer.py:32–59  ·  view source on GitHub ↗

Pads a tensor with identity values up to :obj:`max_sequence_length`. Args: x: A ``tf.Tensor`` of shape ``[batch_size, time, depth]``. sequence_length: The true sequence length of :obj:`x`. max_sequence_length: The sequence length up to which the tensor must contain :obj

(x, sequence_length, max_sequence_length, identity_values=0, maxlen=None)

Source from the content-addressed store, hash-verified

30
31
32def pad_with_identity(x, sequence_length, max_sequence_length, identity_values=0, maxlen=None):
33 """Pads a tensor with identity values up to :obj:`max_sequence_length`.
34 Args:
35 x: A ``tf.Tensor`` of shape ``[batch_size, time, depth]``.
36 sequence_length: The true sequence length of :obj:`x`.
37 max_sequence_length: The sequence length up to which the tensor must contain
38 :obj:`identity values`.
39 identity_values: The identity value.
40 maxlen: Size of the output time dimension. Default is the maximum value in
41 obj:`max_sequence_length`.
42 Returns:
43 A ``tf.Tensor`` of shape ``[batch_size, maxlen, depth]``.
44 """
45 if maxlen is None:
46 maxlen = tf.reduce_max(max_sequence_length)
47
48 mask = tf.sequence_mask(sequence_length, maxlen=maxlen, dtype=x.dtype)
49 mask = tf.expand_dims(mask, axis=-1)
50 mask_combined = tf.sequence_mask(
51 max_sequence_length, maxlen=maxlen, dtype=x.dtype)
52 mask_combined = tf.expand_dims(mask_combined, axis=-1)
53
54 identity_mask = mask_combined * (1.0 - mask)
55
56 x = pad_in_time(x, maxlen - tf.shape(x)[1])
57 x = x * mask + (identity_mask * identity_values)
58
59 return x
60
61
62def pad_n_with_identity(inputs, sequence_lengths, identity_values=0):

Callers 1

pad_n_with_identityFunction · 0.70

Calls 1

pad_in_timeFunction · 0.70

Tested by

no test coverage detected