scatter_reduce(input: Tensor, dim: int, index: Tensor, src: Tensor, reduce: str = 'sum', include_self: bool = True)Reduce src into input along dim at positions given by index.
Multi-reduction sibling of scatter_add. When several entries
of src target the same position the chosen reduce op decides
how they combine. include_self controls whether the existing
value in input participates in the reduction or is replaced.
Every reduction treats a position no index entry names the same
way: it keeps input's value, and the gradient reaches input
there. With include_self=False a position index does name
takes nothing from input — neither its value nor a gradient.
For 'amax' / 'amin' the gradient of a position splits evenly
among the values tied for its result; with include_self=False
input's value is not among them even when it equals the result,
so the shares still add up to the incoming gradient. This differs
from the reference framework, which counts that value as a tie and
then drops its share.
Parameters
inputTensordimintindex / src are scattered.indexTensorsrc; each entry names
the position along dim of input to update.srcTensorinput at the positions named by
index. Must have input's dtype.reducestr= 'sum'src values collide on
the same target. One of 'sum' (default), 'mean',
'prod', 'amax', 'amin'. 'mean' divides by the
number of values reduced — the scattered ones, plus input's
own when include_self — and floors on an integer dtype.include_selfbool= TrueTrue (default) the existing value in input is part
of the reduction set; when False it is overwritten at every
position index names and only the scattered values count.Returns
TensorSame shape and dtype as input.
Raises
ValueErrorreduce is not one of the five reductions.lucid._C.engine.DtypeMismatchsrc's dtype is not input's.NotImplementedError'amax' / 'amin' on a complex tensor, which has no order.Examples
>>> import lucid
>>> x = lucid.tensor([[1.0, 2.0, 3.0, 4.0]])
>>> index = lucid.tensor([[0, 0, 1]])
>>> src = lucid.tensor([[10.0, 20.0, 30.0]])
>>> lucid.scatter_reduce(x, 1, index, src, "sum", include_self=False)
tensor([[30., 30., 3., 4.]])
>>> lucid.scatter_reduce(x, 1, index, src, "mean")
tensor([[10.33, 16., 3., 4.]])