espnet2.gan_svs.vits.modules.sequence_mask
Less than 1 minute
espnet2.gan_svs.vits.modules.sequence_mask
espnet2.gan_svs.vits.modules.sequence_mask(length, max_length=None)
Creates a sequence mask for a given length tensor, which can be used to mask
out padded values in sequences.
This function generates a boolean mask tensor where the positions that are less than the lengths specified in the length tensor are marked as True, and positions that exceed these lengths are marked as False. This is useful in scenarios where you want to ignore padding in sequence processing.
- Parameters:
- length (torch.Tensor) – A 1D tensor containing the lengths of sequences.
- max_length (int , optional) – The maximum length for the mask. If not specified, it will be set to the maximum value in length.
- Returns: A 2D boolean tensor of shape (1, max_length) where each : row corresponds to the mask for a sequence, indicating which positions are valid (True) and which are padding (False).
- Return type: torch.Tensor
Examples
>>> lengths = torch.tensor([3, 5, 2])
>>> mask = sequence_mask(lengths)
>>> print(mask)
tensor([[ True, True, True, False, False],
[ True, True, True, True, True],
[ True, True, False, False, False]])
>>> lengths = torch.tensor([4, 2, 3])
>>> mask = sequence_mask(lengths, max_length=5)
>>> print(mask)
tensor([[ True, True, True, True, False],
[ True, True, False, False, False],
[ True, True, True, False, False]])