Scatter Mul¶
-
torch_scatter.
scatter_mul
(src, index, dim=-1, out=None, dim_size=None, fill_value=1)[source]¶ Multiplies all values from the
src
tensor intoout
at the indices specified in theindex
tensor along a given axisdim
.If multiple indices reference the same location, their contributions multiply (cf.scatter_add()
).For one-dimensional tensors, the operation computes
\[\mathrm{out}_i = \mathrm{out}_i \cdot \prod_j \mathrm{src}_j\]where \(\prod_j\) is over \(j\) such that \(\mathrm{index}_j = i\).
Parameters: - src (Tensor) – The source tensor.
- index (LongTensor) – The indices of elements to scatter.
- dim (int, optional) – The axis along which to index.
(default:
-1
) - out (Tensor, optional) – The destination tensor. (default:
None
) - dim_size (int, optional) – If
out
is not given, automatically create output with sizedim_size
at dimensiondim
. Ifdim_size
is not given, a minimal sized output tensor is returned. (default:None
) - fill_value (int, optional) – If
out
is not given, automatically fill output tensor withfill_value
. (default:1
)
Return type: Tensor
from torch_scatter import scatter_mul src = torch.Tensor([[2, 0, 3, 4, 3], [2, 3, 4, 2, 4]]) index = torch.tensor([[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]]) out = src.new_ones((2, 6)) out = scatter_mul(src, index, out=out) print(out)
tensor([[1., 1., 4., 3., 6., 0.], [6., 4., 8., 1., 1., 1.]])