Returns a mask tensor representing the first N positions of each cell. If `lengths` has shape `[d_1, d_2, ..., d_n]` the resulting tensor `mask` has dtype `dtype` and shape `[d_1, d_2, ..., d_n, maxlen]`, with ``` mask[i_1, i_2, ..., i_n, j] = (j < lengths[i_1, i_2, ..., i_n]) ``` Exa
(lengths, maxlen=None, dtype=dtypes.bool, name=None)
| 3535 | |
| 3536 | @tf_export("sequence_mask") |
| 3537 | def sequence_mask(lengths, maxlen=None, dtype=dtypes.bool, name=None): |
| 3538 | """Returns a mask tensor representing the first N positions of each cell. |
| 3539 | |
| 3540 | If `lengths` has shape `[d_1, d_2, ..., d_n]` the resulting tensor `mask` has |
| 3541 | dtype `dtype` and shape `[d_1, d_2, ..., d_n, maxlen]`, with |
| 3542 | |
| 3543 | ``` |
| 3544 | mask[i_1, i_2, ..., i_n, j] = (j < lengths[i_1, i_2, ..., i_n]) |
| 3545 | ``` |
| 3546 | |
| 3547 | Examples: |
| 3548 | |
| 3549 | ```python |
| 3550 | tf.sequence_mask([1, 3, 2], 5) # [[True, False, False, False, False], |
| 3551 | # [True, True, True, False, False], |
| 3552 | # [True, True, False, False, False]] |
| 3553 | |
| 3554 | tf.sequence_mask([[1, 3],[2,0]]) # [[[True, False, False], |
| 3555 | # [True, True, True]], |
| 3556 | # [[True, True, False], |
| 3557 | # [False, False, False]]] |
| 3558 | ``` |
| 3559 | |
| 3560 | Args: |
| 3561 | lengths: integer tensor, all its values <= maxlen. |
| 3562 | maxlen: scalar integer tensor, size of last dimension of returned tensor. |
| 3563 | Default is the maximum value in `lengths`. |
| 3564 | dtype: output type of the resulting tensor. |
| 3565 | name: name of the op. |
| 3566 | |
| 3567 | Returns: |
| 3568 | A mask tensor of shape `lengths.shape + (maxlen,)`, cast to specified dtype. |
| 3569 | Raises: |
| 3570 | ValueError: if `maxlen` is not a scalar. |
| 3571 | """ |
| 3572 | with ops.name_scope(name, "SequenceMask", [lengths, maxlen]): |
| 3573 | lengths = ops.convert_to_tensor(lengths) |
| 3574 | |
| 3575 | if maxlen is None: |
| 3576 | maxlen = gen_math_ops._max(lengths, _all_dimensions(lengths)) |
| 3577 | maxlen = gen_math_ops.maximum(constant(0, maxlen.dtype), maxlen) |
| 3578 | else: |
| 3579 | maxlen = ops.convert_to_tensor(maxlen) |
| 3580 | if maxlen.get_shape().ndims is not None and maxlen.get_shape().ndims != 0: |
| 3581 | raise ValueError("maxlen must be scalar for sequence_mask") |
| 3582 | |
| 3583 | # The basic idea is to compare a range row vector of size maxlen: |
| 3584 | # [0, 1, 2, 3, 4] |
| 3585 | # to length as a matrix with 1 column: [[1], [3], [2]]. |
| 3586 | # Because of broadcasting on both arguments this comparison results |
| 3587 | # in a matrix of size (len(lengths), maxlen) |
| 3588 | row_vector = gen_math_ops._range( |
| 3589 | constant(0, maxlen.dtype), maxlen, constant(1, maxlen.dtype)) |
| 3590 | # Since maxlen >= max(lengths), it is safe to use maxlen as a cast |
| 3591 | # authoritative type. Whenever maxlen fits into tf.int32, so do the lengths. |
| 3592 | matrix = gen_math_ops.cast(expand_dims(lengths, -1), maxlen.dtype) |
| 3593 | result = row_vector < matrix |
| 3594 |
no test coverage detected