latest (dev)
Copy
Latest development documentation · Updated 2026-10-08
tensorplay.masked.softmax
- tensorplay.masked.softmax(input, dim, *, dtype=None, mask=None) Tensor[source]
Returns softmax of all the slices in the
inputtensor alongdimwhile theinputelements are masked out according to the boolean tensormask.Let
xbe a sequence of unmasked elements of one-dimensional slice of theinputtensor. Softmax of i-th element inxis defined asexp(x[i])/sum(exp(x)).The boolean tensor
maskdefines the “validity” ofinputtensor elements: ifmaskelement is True then the corresponding element ininputtensor 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
outputtensor.The mask of the softmax output tensor can be computed as
tensorplay.broadcast_to(mask, input.shape).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.]]) >>> 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]])
Help improve this page
Found an error, an unclear step, or a missing example?

