|
| 1 | +from torch.autograd import Function |
| 2 | + |
| 3 | +from .utils.ffi import get_func |
| 4 | +from .utils.gen import gen |
| 5 | + |
| 6 | + |
| 7 | +class ScatterMul(Function): |
| 8 | + @staticmethod |
| 9 | + def forward(ctx, out, src, index, dim): |
| 10 | + func = get_func('scatter_mul', src) |
| 11 | + func(dim, out, index, src) |
| 12 | + |
| 13 | + ctx.dim = dim |
| 14 | + ctx.mark_dirty(out) |
| 15 | + ctx.save_for_backward(out, src, index) |
| 16 | + |
| 17 | + return out |
| 18 | + |
| 19 | + @staticmethod |
| 20 | + def backward(ctx, grad_out): |
| 21 | + out, src, index = ctx.saved_variables |
| 22 | + |
| 23 | + grad_src = None |
| 24 | + if ctx.needs_input_grad[1]: |
| 25 | + grad_src = (grad_out * out)[index] / src |
| 26 | + |
| 27 | + return None, grad_src, None, None |
| 28 | + |
| 29 | + |
| 30 | +def scatter_mul(src, index, dim=-1, out=None, dim_size=None, fill_value=1): |
| 31 | + r""" |
| 32 | + | |
| 33 | +
|
| 34 | + .. image:: https://raw.githubusercontent.com/rusty1s/pytorch_scatter/ |
| 35 | + master/docs/source/_figures/mul.svg?sanitize=true |
| 36 | + :align: center |
| 37 | + :width: 400px |
| 38 | +
|
| 39 | + | |
| 40 | +
|
| 41 | + Multiplies all values from the :attr:`src` tensor into :attr:`out` at the |
| 42 | + indices specified in the :attr:`index` tensor along an given axis |
| 43 | + :attr:`dim`.If multiple indices reference the same location, their |
| 44 | + **contributions multiply** (`cf.` :meth:`~torch_scatter.scatter_add`). |
| 45 | +
|
| 46 | + For one-dimensional tensors, the operation computes |
| 47 | +
|
| 48 | + .. math:: |
| 49 | + \mathrm{out}_i = \mathrm{out}_i \cdot \prod_j \mathrm{src}_j |
| 50 | +
|
| 51 | + where sum is over :math:`j` such that :math:`\mathrm{index}_j = i`. |
| 52 | +
|
| 53 | + Args: |
| 54 | + src (Tensor): The source tensor. |
| 55 | + index (LongTensor): The indices of elements to scatter. |
| 56 | + dim (int, optional): The axis along which to index. |
| 57 | + (default: :obj:`-1`) |
| 58 | + out (Tensor, optional): The destination tensor. (default: :obj:`None`) |
| 59 | + dim_size (int, optional): If :attr:`out` is not given, automatically |
| 60 | + create output with size :attr:`dim_size` at dimension :attr:`dim`. |
| 61 | + If :attr:`dim_size` is not given, a minimal sized output tensor is |
| 62 | + returned. (default: :obj:`None`) |
| 63 | + fill_value (int, optional): If :attr:`out` is not given, automatically |
| 64 | + fill output tensor with :attr:`fill_value`. (default: :obj:`0`) |
| 65 | +
|
| 66 | + :rtype: :class:`Tensor` |
| 67 | +
|
| 68 | + .. testsetup:: |
| 69 | +
|
| 70 | + import torch |
| 71 | +
|
| 72 | + .. testcode:: |
| 73 | +
|
| 74 | + from torch_scatter import scatter_mean |
| 75 | + src = torch.tensor([[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]]) |
| 76 | + index = torch.tensor([[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]]) |
| 77 | + out = src.new_zeros((2, 6)) |
| 78 | + out = scatter_mean(src, index, out=out) |
| 79 | + print(out) |
| 80 | +
|
| 81 | + .. testoutput:: |
| 82 | +
|
| 83 | + 0.0000 0.0000 4.0000 3.0000 1.5000 0.0000 |
| 84 | + 1.0000 4.0000 2.0000 0.0000 0.0000 0.0000 |
| 85 | + [torch.FloatTensor of size 2x6] |
| 86 | + """ |
| 87 | + src, out, index, dim = gen(src, index, dim, out, dim_size, fill_value) |
| 88 | + return ScatterMul.apply(out, src, index, dim) |
0 commit comments