TensorPlay
API reference
latest (dev)
Copy
View Markdown

Latest development documentation · Updated 2026-10-08

tensorplay.masked API

Functions 22

#

amax

functionFull reference ↗
tensorplay.masked.amax(input, dim, *, keepdim=False, dtype=None, mask=None) → Tensor[source]

Returns maximum of all the elements in the input tensor along the given dimension(s) dim while the input elements are masked out according to the boolean tensor mask.

The identity value of maximum operation, which is used to start the reduction, depends on input dtype. For instance, for float32, uint8, and int32 dtypes, the identity values are tensor(-inf), tensor(Tensor(shape=tensorplay.Size(), dtype=UInt8, device=cpu), and tensor(-2147483648, dtype=Int32), respectively.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in maximum computation, otherwise the element is ignored.

When all elements of input along the given dimension dim are ignored (fully masked-out), the corresponding element of the output tensor will have undefined value: it may or may not correspond to the identity value of maximum operation; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the output tensor can be computed as tensorplay.any(tensorplay.broadcast_to(mask, input.shape), dim, keepdim=keepdim, dtype=tensorplay.bool).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3, -2, -1], [0, 1, 2]], dtype=Int64)
>>> input
tensor([[-3, -2, -1],
        [0, 1, 2]], dtype=Int64)
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.amax(input, 1, mask=mask)
tensor([-1, -9223372036854775808], dtype=Int64)
#

amin

functionFull reference ↗
tensorplay.masked.amin(input, dim, *, keepdim=False, dtype=None, mask=None) → Tensor[source]

Returns minimum of all the elements in the input tensor along the given dimension(s) dim while the input elements are masked out according to the boolean tensor mask.

The identity value of minimum operation, which is used to start the reduction, depends on input dtype. For instance, for float32, uint8, and int32 dtypes, the identity values are tensor(inf), tensor(Tensor(shape=tensorplay.Size(), dtype=UInt8, device=cpu), and tensor(2147483647, dtype=Int32), respectively.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in minimum computation, otherwise the element is ignored.

When all elements of input along the given dimension dim are ignored (fully masked-out), the corresponding element of the output tensor will have undefined value: it may or may not correspond to the identity value of minimum operation; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the output tensor can be computed as tensorplay.any(tensorplay.broadcast_to(mask, input.shape), dim, keepdim=keepdim, dtype=tensorplay.bool).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3, -2, -1], [0, 1, 2]], dtype=Int64)
>>> input
tensor([[-3, -2, -1],
        [0, 1, 2]], dtype=Int64)
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.amin(input, 1, mask=mask)
tensor([-3, -9223372036854775808], dtype=Int64)
#

argmax

functionFull reference ↗
tensorplay.masked.argmax(input, dim, *, keepdim=False, dtype=None, mask=None) → Tensor[source]

Returns argmax of all the elements in the input tensor along the given dimension(s) dim while the input elements are masked out according to the boolean tensor mask. The identity value of argmax operation, which is used to start the reduction, depends on input dtype. For instance, for float32, uint8, and int32 dtypes, the identity values are tensor(-inf), tensor(Tensor(shape=tensorplay.Size(), dtype=UInt8, device=cpu), and tensor(-2147483648, dtype=Int32), respectively. If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in argmax computation, otherwise the element is ignored.

When all elements of input along the given dimension dim are ignored (fully masked-out), the corresponding element of the output tensor will have undefined value: it may or may not correspond to the identity value of argmax operation; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the output tensor can be computed as tensorplay.any(tensorplay.broadcast_to(mask, input.shape), dim, keepdim=keepdim, dtype=tensorplay.bool).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3, -2, -1], [0, 1, 2]], dtype=Int64)
>>> input
tensor([[-3, -2, -1],
        [0, 1, 2]], dtype=Int64)
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.argmax(input, 1, mask=mask)
tensor([2, 0], dtype=Int64)
#

argmin

functionFull reference ↗
tensorplay.masked.argmin(input, dim, *, keepdim=False, dtype=None, mask=None) → Tensor[source]

Returns argmin of all the elements in the input tensor along the given dimension(s) dim while the input elements are masked out according to the boolean tensor mask. The identity value of argmin operation, which is used to start the reduction, depends on input dtype. For instance, for float32, uint8, and int32 dtypes, the identity values are tensor(inf), tensor(Tensor(shape=tensorplay.Size(), dtype=UInt8, device=cpu), and tensor(2147483647, dtype=Int32), respectively. If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in argmin computation, otherwise the element is ignored.

When all elements of input along the given dimension dim are ignored (fully masked-out), the corresponding element of the output tensor will have undefined value: it may or may not correspond to the identity value of argmin operation; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the output tensor can be computed as tensorplay.any(tensorplay.broadcast_to(mask, input.shape), dim, keepdim=keepdim, dtype=tensorplay.bool).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3, -2, -1], [0, 1, 2]], dtype=Int64)
>>> input
tensor([[-3, -2, -1],
        [0, 1, 2]], dtype=Int64)
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.argmin(input, 1, mask=mask)
tensor([0, 0], dtype=Int64)
#

cumprod

functionFull reference ↗
tensorplay.masked.cumprod(input, dim, *, dtype=None, mask=None) → Tensor[source]

Returns cumulative_prod of all the slices in the input tensor along dim while the input elements are masked out according to the boolean tensor mask.

Let x be a sequence of unmasked elements of one-dimensional slice of the input tensor. Cumsum of i-th element in x is defined as prod(x[:i]).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in cumulative_prod computation, otherwise the element is ignored.

The values of masked-out elements of the output tensor have undefined value: it may or may not be set to zero or nan; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the cumulative_prod output tensor can be computed as tensorplay.broadcast_to(mask, input.shape).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3., -2., -1.], [0., 1., 2.]])
>>> input
tensor([[-3., -2., -1.],
        [0., 1., 2.]])
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.cumprod(input, 1, mask=mask)
tensor([[-3., -3., 3.],
        [1., 1., 1.]])
#

cumsum

functionFull reference ↗
tensorplay.masked.cumsum(input, dim, *, dtype=None, mask=None) → Tensor[source]

Returns cumulative_sum of all the slices in the input tensor along dim while the input elements are masked out according to the boolean tensor mask.

Let x be a sequence of unmasked elements of one-dimensional slice of the input tensor. Cumsum of i-th element in x is defined as sum(x[:i]).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in cumulative_sum computation, otherwise the element is ignored.

The values of masked-out elements of the output tensor have undefined value: it may or may not be set to zero or nan; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the cumulative_sum output tensor can be computed as tensorplay.broadcast_to(mask, input.shape).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3., -2., -1.], [0., 1., 2.]])
>>> input
tensor([[-3., -2., -1.],
        [0., 1., 2.]])
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.cumsum(input, 1, mask=mask)
tensor([[-3., -3., -4.],
        [0., 0., 0.]])
#

is_masked_tensor

functionFull reference ↗
tensorplay.masked.is_masked_tensor(obj: Any, /) → bool[source]

Return True if the input is a MaskedTensor, else False.

Parameters:

obj – any input

Examples

>>> # xdoctest: +SKIP
>>> from tensorplay.masked import MaskedTensor
>>> data = tensorplay.arange(6).reshape(2, 3)
>>> mask = tp.tensor([[True, False, False], [True, True, False]])
>>> mt = MaskedTensor(data, mask)
>>> is_masked_tensor(mt)
True
#

log_softmax

functionFull reference ↗
tensorplay.masked.log_softmax(input, dim, *, dtype=None, mask=None) → Tensor[source]

Returns log_softmax of all the slices in the input tensor along dim while the input elements are masked out according to the boolean tensor mask.

Let x be a sequence of unmasked elements of one-dimensional slice of the input tensor. LogSoftmax of i-th element in x is defined as log(exp(x[i])/sum(exp(x))).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in log_softmax computation, otherwise the element is ignored.

The values of masked-out elements of the output tensor have undefined value: it may or may not be set to zero or nan; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the log_softmax output tensor can be computed as tensorplay.broadcast_to(mask, input.shape).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3., -2., -1.], [0., 1., 2.]])
>>> input
tensor([[-3., -2., -1.],
        [0., 1., 2.]])
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.log_softmax(input, 1, mask=mask)
tensor([[-2.1269, -inf, -0.1269],
        [-nan, -nan, -nan]])
#

logaddexp

functionFull reference ↗
tensorplay.masked.logaddexp(input, other, *, dtype=None, input_mask=None, other_mask=None) → Tensor[source]

Returns logaddexp of all the elements in the input and the other tensor. The input elements are masked out according to the boolean tensor input_mask and the attr:other elements are masked out according to the boolean tensor other_mask.

The shapes of a mask tensor and the tensor to be masked don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the tensor to be masked.

Parameters:
  • input (Tensor) – the input tensor

  • other (Tensor) – the second input tensor

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. If specified, the output tensor is casted to dtype after the operation is performed. Default: None.

  • input_mask (tensorplay.Tensor, optional) – the boolean tensor containing the binary mask of validity of input tensor elements. Default: None that is equivalent to tensorplay.ones(input.shape, dtype=tensorplay.bool).

  • other_mask (tensorplay.Tensor, optional) – the boolean tensor containing the binary mask of validity of other tensor elements. Default: None that is equivalent to tensorplay.ones(other.shape, dtype=tensorplay.bool).

Example:

>>> input = tensorplay.tensor([-100.0, -200, -300])
>>> input
tensor([-100., -200., -300.])
>>> other = tensorplay.tensor([-1.0, -2, -3])
>>> other
tensor([-1., -2., -3.])
>>> mask = tensorplay.tensor([True, False, True])
>>> mask
tensor([ True, False,  True])
>>> tensorplay.masked._ops.logaddexp(input, other, input_mask=mask, other_mask=mask)
tensor([-1., -inf, -3.])
#

logsumexp

functionFull reference ↗
tensorplay.masked.logsumexp(input, dim, *, keepdim=False, dtype=None, mask=None) → Tensor[source]

Returns logsumexp of all the elements in the input tensor along the given dimension(s) dim while the input elements are masked out according to the boolean tensor mask.

The identity value of logsumexp operation, which is used to start the reduction, is tensor(-2147483648, dtype=Int32).

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in logsumexp computation, otherwise the element is ignored.

When all elements of input along the given dimension dim are ignored (fully masked-out), the corresponding element of the output tensor will have undefined value: it may or may not correspond to the identity value of logsumexp operation; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the output tensor can be computed as tensorplay.any(tensorplay.broadcast_to(mask, input.shape), dim, keepdim=keepdim, dtype=tensorplay.bool).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3., -2., -1.], [0., 1., 2.]])
>>> input
tensor([[-3., -2., -1.],
        [0., 1., 2.]])
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.logsumexp(input, 1, mask=mask)
tensor([-0.8731, -inf])
#

mean

functionFull reference ↗
tensorplay.masked.mean(input, dim, *, keepdim=False, dtype=None, mask=None) → Tensor[source]

Returns mean of all the elements in the input tensor along the given dimension(s) dim while the input elements are masked out according to the boolean tensor mask.

By definition, the identity value of a mean operation is the mean value of the tensor. If all elements of the input tensor along given dimension(s) dim are masked-out, the identity value of the mean is undefined. Due to this ambiguity, the elements of output tensor with strided layout, that correspond to fully masked-out elements, have nan values.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in mean computation, otherwise the element is ignored.

When all elements of input along the given dimension dim are ignored (fully masked-out), the corresponding element of the output tensor will have undefined value: it may or may not correspond to the identity value of mean operation; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the output tensor can be computed as tensorplay.any(tensorplay.broadcast_to(mask, input.shape), dim, keepdim=keepdim, dtype=tensorplay.bool).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3., -2., -1.], [0., 1., 2.]])
>>> input
tensor([[-3., -2., -1.],
        [0., 1., 2.]])
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.mean(input, 1, mask=mask)
tensor([-2., -nan])
#

median

functionFull reference ↗
tensorplay.masked.median(input, dim, *, keepdim=False, dtype=None, mask=None) → Tensor[source]

Returns median of all the elements in the input tensor along the given dimension(s) dim while the input elements are masked out according to the boolean tensor mask. By definition, the identity value of a median operation is the median value of the tensor. If all elements of the input tensor along given dimension(s) dim are masked-out, the identity value of the median is undefined. Due to this ambiguity, the elements of output tensor with strided layout, that correspond to fully masked-out elements, have nan values. If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in median computation, otherwise the element is ignored.

When all elements of input along the given dimension dim are ignored (fully masked-out), the corresponding element of the output tensor will have undefined value: it may or may not correspond to the identity value of median operation; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the output tensor can be computed as tensorplay.any(tensorplay.broadcast_to(mask, input.shape), dim, keepdim=keepdim, dtype=tensorplay.bool).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3., -2., -1.], [0., 1., 2.]])
>>> input
tensor([[-3., -2., -1.],
        [0., 1., 2.]])
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.median(input, 1, mask=mask)
tensor([-3., nan])
#

norm

functionFull reference ↗
tensorplay.masked.norm(input, ord, dim, *, keepdim=False, dtype=None, mask=None) → Tensor[source]

Returns norm of all the elements in the input tensor along the given dimension(s) dim while the input elements are masked out according to the boolean tensor mask.

The identity value of norm operation, which is used to start the reduction, is tensor(0.), except for ord=-inf it is tensor(inf).

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in norm computation, otherwise the element is ignored.

When all elements of input along the given dimension dim are ignored (fully masked-out), the corresponding element of the output tensor will have undefined value: it may or may not correspond to the identity value of norm operation; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the output tensor can be computed as tensorplay.any(tensorplay.broadcast_to(mask, input.shape), dim, keepdim=keepdim, dtype=tensorplay.bool).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3., -2., -1.], [0., 1., 2.]])
>>> input
tensor([[-3., -2., -1.],
        [0., 1., 2.]])
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.norm(input, 2.0, 1, mask=mask)
tensor([3.1623, 0.])
#

normalize

functionFull reference ↗
tensorplay.masked.normalize(input, ord, dim, *, eps=1e-12, dtype=None, mask=None) → Tensor[source]

Returns normalize of all the slices in the input tensor along dim while the input elements are masked out according to the boolean tensor mask.

Let x be a sequence of unmasked elements of one-dimensional slice of the input tensor. Normalize of i-th element in x is defined as x[i]/max(norm(x, p), eps).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in normalize computation, otherwise the element is ignored.

The values of masked-out elements of the output tensor have undefined value: it may or may not be set to zero or nan; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the normalize output tensor can be computed as tensorplay.broadcast_to(mask, input.shape).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3., -2., -1.], [0., 1., 2.]])
>>> input
tensor([[-3., -2., -1.],
        [0., 1., 2.]])
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.normalize(input, 2.0, 1, mask=mask)
tensor([[-0.9487, 0., -0.3162],
        [0., 0., 0.]])
#

prod

functionFull reference ↗
tensorplay.masked.prod(input, dim, *, keepdim=False, dtype=None, mask=None) → Tensor[source]

Returns product of all the elements in the input tensor along the given dimension(s) dim while the input elements are masked out according to the boolean tensor mask.

The identity value of product operation, which is used to start the reduction, is tensor(1, dtype=Int32).

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in product computation, otherwise the element is ignored.

When all elements of input along the given dimension dim are ignored (fully masked-out), the corresponding element of the output tensor will have undefined value: it may or may not correspond to the identity value of product operation; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the output tensor can be computed as tensorplay.any(tensorplay.broadcast_to(mask, input.shape), dim, keepdim=keepdim, dtype=tensorplay.bool).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3, -2, -1], [0, 1, 2]], dtype=Int64)
>>> input
tensor([[-3, -2, -1],
        [0, 1, 2]], dtype=Int64)
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.prod(input, 1, mask=mask)
tensor([3, 1], dtype=Int64)
#

softmax

functionFull reference ↗
tensorplay.masked.softmax(input, dim, *, dtype=None, mask=None) → Tensor[source]

Returns softmax of all the slices in the input tensor along dim while the input elements are masked out according to the boolean tensor mask.

Let x be a sequence of unmasked elements of one-dimensional slice of the input tensor. Softmax of i-th element in x is defined as exp(x[i])/sum(exp(x)).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in softmax computation, otherwise the element is ignored.

The values of masked-out elements of the output tensor have undefined value: it may or may not be set to zero or nan; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the softmax output tensor can be computed as tensorplay.broadcast_to(mask, input.shape).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3., -2., -1.], [0., 1., 2.]])
>>> input
tensor([[-3., -2., -1.],
        [0., 1., 2.]])
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.softmax(input, 1, mask=mask)
tensor([[0.1192, 0., 0.8808],
        [-nan, -nan, -nan]])
#

softmin

functionFull reference ↗
tensorplay.masked.softmin(input, dim, *, dtype=None, mask=None) → Tensor[source]

Returns softmin of all the slices in the input tensor along dim while the input elements are masked out according to the boolean tensor mask.

Let x be a sequence of unmasked elements of one-dimensional slice of the input tensor. Softmin of i-th element in x is defined as exp(-x[i])/sum(exp(-x)).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in softmin computation, otherwise the element is ignored.

The values of masked-out elements of the output tensor have undefined value: it may or may not be set to zero or nan; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the softmin output tensor can be computed as tensorplay.broadcast_to(mask, input.shape).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3., -2., -1.], [0., 1., 2.]])
>>> input
tensor([[-3., -2., -1.],
        [0., 1., 2.]])
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.softmin(input, 1, mask=mask)
tensor([[0.8808, 0., 0.1192],
        [-nan, -nan, -nan]])
#

std

functionFull reference ↗
tensorplay.masked.std(input, dim, unbiased, *, keepdim=False, dtype=None, mask=None) → Tensor[source]

Returns standard_deviation of all the elements in the input tensor along the given dimension(s) dim while the input elements are masked out according to the boolean tensor mask. The identity value of sample standard deviation operation is undefined. The elements of output tensor with strided layout, that correspond to fully masked-out elements, have nan values. If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in standard_deviation computation, otherwise the element is ignored.

When all elements of input along the given dimension dim are ignored (fully masked-out), the corresponding element of the output tensor will have undefined value: it may or may not correspond to the identity value of standard_deviation operation; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the output tensor can be computed as tensorplay.any(tensorplay.broadcast_to(mask, input.shape), dim, keepdim=keepdim, dtype=tensorplay.bool).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3, -2, -1], [0, 1, 2]], dtype=Int64)
>>> input
tensor([[-3, -2, -1],
        [0, 1, 2]], dtype=Int64)
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.std(input, 1, False, mask=mask)
tensor([1., -nan])
#

sum

functionFull reference ↗
tensorplay.masked.sum(input, dim, *, keepdim=False, dtype=None, mask=None) → Tensor[source]

Returns sum of all the elements in the input tensor along the given dimension(s) dim while the input elements are masked out according to the boolean tensor mask.

The identity value of sum operation, which is used to start the reduction, is tensor(0, dtype=Int32).

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in sum computation, otherwise the element is ignored.

When all elements of input along the given dimension dim are ignored (fully masked-out), the corresponding element of the output tensor will have undefined value: it may or may not correspond to the identity value of sum operation; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the output tensor can be computed as tensorplay.any(tensorplay.broadcast_to(mask, input.shape), dim, keepdim=keepdim, dtype=tensorplay.bool).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3, -2, -1], [0, 1, 2]], dtype=Int64)
>>> input
tensor([[-3, -2, -1],
        [0, 1, 2]], dtype=Int64)
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.sum(input, 1, mask=mask)
tensor([-4, 0], dtype=Int64)
#

var

functionFull reference ↗
tensorplay.masked.var(input, dim, unbiased, *, keepdim=False, dtype=None, mask=None) → Tensor[source]

Returns variance of all the elements in the input tensor along the given dimension(s) dim while the input elements are masked out according to the boolean tensor mask. The identity value of sample variance operation is undefined. The elements of output tensor with strided layout, that correspond to fully masked-out elements, have nan values. If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

The boolean tensor mask defines the “validity” of input tensor elements: if mask element is True then the corresponding element in input tensor will be included in variance computation, otherwise the element is ignored.

When all elements of input along the given dimension dim are ignored (fully masked-out), the corresponding element of the output tensor will have undefined value: it may or may not correspond to the identity value of variance operation; the choice may correspond to the value that leads to the most efficient storage of output tensor.

The mask of the output tensor can be computed as tensorplay.any(tensorplay.broadcast_to(mask, input.shape), dim, keepdim=keepdim, dtype=tensorplay.bool).

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of the mask tensor must not be greater than of the input tensor.

Example:

>>> input = tensor([[-3, -2, -1], [0, 1, 2]], dtype=Int64)
>>> input
tensor([[-3, -2, -1],
        [0, 1, 2]], dtype=Int64)
>>> mask = tensor([[True, False, True], [False, False, False]], dtype=Bool)
>>> mask
tensor([[True, False, True],
        [False, False, False]], dtype=Bool)
>>> tensorplay.masked._ops.var(input, 1, False, mask=mask)
tensor([1., -nan])

Classes 1

#

MaskedTensor

classFull reference ↗
class tensorplay.masked.MaskedTensor(data, mask, requires_grad=False)[source]

A pair of plain tensors data and mask presented as a single value.

mask is boolean and has the same shape as data. An element of data participates in computations only where the corresponding element of mask is True; positions where the mask is False are rendered as -- in the string representation and are replaced by a caller-supplied fill value by to_tensor().

The mask is always the source of truth for validity: masked-out entries of data may hold arbitrary values and are never read semantically.

This class is not a tensor subclass and does not hook into a dispatcher. Operations are applied through explicit methods (elementwise ops, reductions and structural ops) or through the masking-aware functions in tensorplay.masked, which accept both plain tensors and MaskedTensor inputs.

abs(*args, **kwargs)
abs_(*args, **kwargs)
absolute(*args, **kwargs)
absolute_(*args, **kwargs)
acos(*args, **kwargs)
acos_(*args, **kwargs)
acosh(*args, **kwargs)
acosh_(*args, **kwargs)
add(*args, **kwargs)
add_(*args, **kwargs)
all(*args, **kwargs)
amax(*args, **kwargs)
amin(*args, **kwargs)
angle(*args, **kwargs)
any(*args, **kwargs)
arccos(*args, **kwargs)
arccos_(*args, **kwargs)
arccosh(*args, **kwargs)
arccosh_(*args, **kwargs)
arcsin(*args, **kwargs)
arcsin_(*args, **kwargs)
arcsinh(*args, **kwargs)
arcsinh_(*args, **kwargs)
arctan(*args, **kwargs)
arctan2(*args, **kwargs)
arctan2_(*args, **kwargs)
arctan_(*args, **kwargs)
arctanh(*args, **kwargs)
arctanh_(*args, **kwargs)
argmax(*args, **kwargs)
argmin(*args, **kwargs)
asin(*args, **kwargs)
asin_(*args, **kwargs)
asinh(*args, **kwargs)
asinh_(*args, **kwargs)
atan(*args, **kwargs)
atan2(*args, **kwargs)
atan2_(*args, **kwargs)
atan_(*args, **kwargs)
atanh(*args, **kwargs)
atanh_(*args, **kwargs)
bitwise_and(*args, **kwargs)
bitwise_and_(*args, **kwargs)
bitwise_left_shift(*args, **kwargs)
bitwise_left_shift_(*args, **kwargs)
bitwise_not(*args, **kwargs)
bitwise_not_(*args, **kwargs)
bitwise_or(*args, **kwargs)
bitwise_or_(*args, **kwargs)
bitwise_right_shift(*args, **kwargs)
bitwise_right_shift_(*args, **kwargs)
bitwise_xor(*args, **kwargs)
bitwise_xor_(*args, **kwargs)
cat(*args, **kwargs)
ceil(*args, **kwargs)
ceil_(*args, **kwargs)
clamp(*args, **kwargs)
clamp_(*args, **kwargs)
clip(*args, **kwargs)
clip_(*args, **kwargs)
clone(*args, **kwargs)
col2im(*args, **kwargs)
conj(*args, **kwargs)
conj_physical(*args, **kwargs)
conj_physical_(*args, **kwargs)
contiguous(*args, **kwargs)
copy_(*args, **kwargs)
cos(*args, **kwargs)
cos_(*args, **kwargs)
cosh(*args, **kwargs)
cosh_(*args, **kwargs)
deg2rad(*args, **kwargs)
deg2rad_(*args, **kwargs)
detach(*args, **kwargs)
digamma(*args, **kwargs)
digamma_(*args, **kwargs)
div(*args, **kwargs)
div_(*args, **kwargs)
divide(*args, **kwargs)
divide_(*args, **kwargs)
eq(*args, **kwargs)
eq_(*args, **kwargs)
erf(*args, **kwargs)
erf_(*args, **kwargs)
erfc(*args, **kwargs)
erfc_(*args, **kwargs)
erfinv(*args, **kwargs)
erfinv_(*args, **kwargs)
exp(*args, **kwargs)
exp2(*args, **kwargs)
exp2_(*args, **kwargs)
exp_(*args, **kwargs)
expand(*args, **kwargs)
expm1(*args, **kwargs)
expm1_(*args, **kwargs)
fix(*args, **kwargs)
fix_(*args, **kwargs)
floor(*args, **kwargs)
floor_(*args, **kwargs)
floor_divide(*args, **kwargs)
floor_divide_(*args, **kwargs)
fmax(*args, **kwargs)
fmin(*args, **kwargs)
fmod(*args, **kwargs)
fmod_(*args, **kwargs)
frac(*args, **kwargs)
frac_(*args, **kwargs)
ge(*args, **kwargs)
ge_(*args, **kwargs)
greater(*args, **kwargs)
greater_(*args, **kwargs)
greater_equal(*args, **kwargs)
greater_equal_(*args, **kwargs)
gt(*args, **kwargs)
gt_(*args, **kwargs)
i0(*args, **kwargs)
i0_(*args, **kwargs)
im2col(*args, **kwargs)
index(*args, **kwargs)
indices(*args, **kwargs)
is_contiguous(*args, **kwargs)
is_same_size(*args, **kwargs)
isnan(*args, **kwargs)
item(*args, **kwargs)
le(*args, **kwargs)
le_(*args, **kwargs)
less(*args, **kwargs)
less_(*args, **kwargs)
less_equal(*args, **kwargs)
less_equal_(*args, **kwargs)
lgamma(*args, **kwargs)
lgamma_(*args, **kwargs)
log(*args, **kwargs)
log10(*args, **kwargs)
log10_(*args, **kwargs)
log1p(*args, **kwargs)
log1p_(*args, **kwargs)
log2(*args, **kwargs)
log2_(*args, **kwargs)
log_(*args, **kwargs)
logaddexp(*args, **kwargs)
logaddexp2(*args, **kwargs)
logit(*args, **kwargs)
logit_(*args, **kwargs)
lt(*args, **kwargs)
lt_(*args, **kwargs)
maximum(*args, **kwargs)
mean(*args, **kwargs)
minimum(*args, **kwargs)
mul(*args, **kwargs)
mul_(*args, **kwargs)
multiply(*args, **kwargs)
multiply_(*args, **kwargs)
nan_to_num(*args, **kwargs)
nan_to_num_(*args, **kwargs)
ne(*args, **kwargs)
ne_(*args, **kwargs)
neg(*args, **kwargs)
neg_(*args, **kwargs)
negative(*args, **kwargs)
negative_(*args, **kwargs)
new_empty_strided(*args, **kwargs)
nextafter(*args, **kwargs)
nextafter_(*args, **kwargs)
norm(*args, **kwargs)
not_equal(*args, **kwargs)
not_equal_(*args, **kwargs)
ones_like(*args, **kwargs)
positive(*args, **kwargs)
pow(*args, **kwargs)
pow_(*args, **kwargs)
prod(*args, **kwargs)
rad2deg(*args, **kwargs)
rad2deg_(*args, **kwargs)
reciprocal(*args, **kwargs)
reciprocal_(*args, **kwargs)
remainder(*args, **kwargs)
remainder_(*args, **kwargs)
round(*args, **kwargs)
round_(*args, **kwargs)
rsqrt(*args, **kwargs)
rsqrt_(*args, **kwargs)
select(*args, **kwargs)
select_backward(*args, **kwargs)
sgn(*args, **kwargs)
sgn_(*args, **kwargs)
sigmoid(*args, **kwargs)
sigmoid_(*args, **kwargs)
sign(*args, **kwargs)
sign_(*args, **kwargs)
signbit(*args, **kwargs)
sin(*args, **kwargs)
sin_(*args, **kwargs)
sinc(*args, **kwargs)
sinc_(*args, **kwargs)
sinh(*args, **kwargs)
sinh_(*args, **kwargs)
slice(*args, **kwargs)
slice_backward(*args, **kwargs)
softmax(*args, **kwargs)
split(*args, **kwargs)
sqrt(*args, **kwargs)
sqrt_(*args, **kwargs)
square(*args, **kwargs)
square_(*args, **kwargs)
stack(*args, **kwargs)
std(*args, **kwargs)
stride(*args, **kwargs)
sub(*args, **kwargs)
sub_(*args, **kwargs)
subtract(*args, **kwargs)
subtract_(*args, **kwargs)
sum(*args, **kwargs)
t(*args, **kwargs)
tan(*args, **kwargs)
tan_(*args, **kwargs)
tanh(*args, **kwargs)
tanh_(*args, **kwargs)
to(*args, **kwargs)
to_dense(*args, **kwargs)
to_sparse(*args, **kwargs)
to_sparse_csr(*args, **kwargs)
transpose(*args, **kwargs)
true_divide(*args, **kwargs)
true_divide_(*args, **kwargs)
trunc(*args, **kwargs)
trunc_(*args, **kwargs)
unfold(*args, **kwargs)
unfold_backward(*args, **kwargs)
unsqueeze(*args, **kwargs)
values(*args, **kwargs)
var(*args, **kwargs)
view(*args, **kwargs)

On this page

Ask DeepWiki