jactorch.functional.masking#

Masking-related functions.

Functions

length2mask(lengths, max_length)

Convert a length vector to a mask.

length_masked_reversed(tensor, lengths[, dim])

Reverse a padded sequence tensor along the given dimension.

length_masked_softmax(logits, lengths[, ...])

Compute the softmax of the tensor while ignoring some masked elements.

mask_meshgrid(mask[, target_dims])

Create an N-dimensional meshgrid-like mask, where output[i, j, k, ...] = mask[i] * mask[j] * mask[k] * ....

masked_average(tensor, mask[, eps])

Compute the average of the tensor while ignoring some masked elements.

masked_softmax(logits[, mask, dim, eps, ninf])

Compute the softmax of the tensor while ignoring some masked elements.

Functions

length2mask(lengths, max_length)[source]#

Convert a length vector to a mask.

Parameters:
  • lengths (Tensor) – a vector of length. Batch dimensions are supported, but the length dimension is assumed to be the last one.

  • max_length (int) – the maximum length of the mask.

Returns:

a mask with shape lengths.shape + (max_length,).

Return type:

Tensor

length_masked_reversed(tensor, lengths, dim=1)[source]#

Reverse a padded sequence tensor along the given dimension.

Parameters:
  • tensor (Tensor) – padded batch of variable length sequences.

  • lengths (Tensor) – list of sequence lengths

  • dim (int) – dimension along which to reverse sequences. Currently only supports dim=1.

Returns:

A tensor with the same size as the input, but with each sequence reversed.

Return type:

Tensor

length_masked_softmax(logits, lengths, dim=-1, ninf=-1e4)[source]#

Compute the softmax of the tensor while ignoring some masked elements. Unlike masked_softmax(), this function uses the lengths to compute the mask. When all elements are masked, the result is a uniform distribution.

Parameters:
  • logits – tensor to be softmaxed.

  • lengths – a vector of length. Batch dimensions are supported, but the length dimension is assumed to be the last one.

  • dim – the dimension to be softmaxed.

  • ninf – the value to be used for masked elements.

Returns:

the softmax of the input tensor.

mask_meshgrid(mask, target_dims=2)[source]#

Create an N-dimensional meshgrid-like mask, where output[i, j, k, ...] = mask[i] * mask[j] * mask[k] * ....

Parameters:
  • mask (Tensor) – the original mask. Batch dimensions are supported, but the mask dimension is assumed to be the last one.

  • target_dims (int) – the number of target dimensions of the output mask.

Returns:

a mask with shape mask.shape + (target_dims - mask.dim()).

Return type:

Tensor

masked_average(tensor, mask, eps=1e-8)[source]#

Compute the average of the tensor while ignoring some masked elements.

Parameters:
  • tensor (Tensor) – tensor to be averaged.

  • mask (Tensor) – a mask indicating the element-wise weight.

  • eps – eps for numerical stability.

Returns:

the average of the input tensor.

Return type:

Tensor

masked_softmax(logits, mask=None, dim=-1, eps=1e-20, ninf=-1e4)[source]#

Compute the softmax of the tensor while ignoring some masked elements. When all elements are masked, the result is a uniform distribution.

Parameters:
  • logits – tensor to be softmaxed.

  • mask – a mask indicating the element-wise weight.

  • dim – the dimension to be softmaxed.

  • eps – eps for numerical stability.

  • ninf – the value to be used for masked elements.

Returns:

the softmax of the input tensor.