MCPcopy Create free account
hub / github.com/google-research/language / sequence_mask

Function sequence_mask

language/common/utils/tensor_utils.py:224–252  ·  view source on GitHub ↗

Returns a sequence mask, like tf.sequence_mask(). Unlike tf.sequence_mask(), this can also generate a transposed mask, which is convenient for working with time-major sequences. Args: lengths: [batch_size] Sequence lengths. maxlen: [] Maximum length, or None to compute the

(lengths, maxlen=None, dtype=tf.bool, transpose=False)

Source from the content-addressed store, hash-verified

222
223
224def sequence_mask(lengths, maxlen=None, dtype=tf.bool, transpose=False):
225 """Returns a sequence mask, like tf.sequence_mask().
226
227 Unlike tf.sequence_mask(), this can also generate a transposed mask, which is
228 convenient for working with time-major sequences.
229
230 Args:
231 lengths: <int>[batch_size] Sequence lengths.
232 maxlen: <int>[] Maximum length, or None to compute the max of `lengths`.
233 dtype: DType of the generated mask.
234 transpose: Whether to generate the transpose of the mask.
235
236 Returns:
237 <dtype>[maxlen, batch_size] Sequence mask.
238 """
239 with tf.name_scope("sequence_mask"):
240 if maxlen is None:
241 maxlen = tf.reduce_max(lengths)
242 positions = tf.range(maxlen, dtype=lengths.dtype)
243
244 positions_singleton_axis = 1 if transpose else 0
245 lengths_singleton_axis = 0 if transpose else 1
246 positions = tf.expand_dims(positions, axis=positions_singleton_axis)
247 lengths = tf.expand_dims(lengths, axis=lengths_singleton_axis)
248 mask = positions < lengths
249
250 if dtype != tf.bool:
251 mask = tf.cast(mask, dtype)
252 return mask

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…