latest (dev)
Copy
Latest development documentation · Updated 2026-10-08
tensorplay.masked.sum
- tensorplay.masked.sum(input, dim, *, keepdim=False, dtype=None, mask=None) Tensor[source]
Returns sum of all the elements in the
inputtensor along the given dimension(s)dimwhile theinputelements are masked out according to the boolean tensormask.The identity value of sum operation, which is used to start the reduction, is
tensor(0, dtype=Int32).If
keepdimisTrue, the output tensor is of the same size asinputexcept in the dimension(s)dimwhere it is of size 1. Otherwise,dimis squeezed (seetensorplay.squeeze()), resulting in the output tensor having 1 (orlen(dim)) fewer dimension(s).The boolean tensor
maskdefines the “validity” ofinputtensor elements: ifmaskelement is True then the corresponding element ininputtensor will be included in sum computation, otherwise the element is ignored.When all elements of
inputalong the given dimensiondimare 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 ofoutputtensor.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
masktensor and theinputtensor don’t need to match, but they must be broadcastable under the standard broadcasting rules and the dimensionality of themasktensor must not be greater than of theinputtensor.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)
Help improve this page
Found an error, an unclear step, or a missing example?

