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