div.py 2.88 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
from torch.autograd import Function

rusty1s's avatar
rusty1s committed
3
4
from torch_scatter.utils.ext import get_func
from torch_scatter.utils.gen import gen
rusty1s's avatar
rusty1s committed
5
6
7
8
9
10


class ScatterDiv(Function):
    @staticmethod
    def forward(ctx, out, src, index, dim):
        func = get_func('scatter_div', src)
rusty1s's avatar
rusty1s committed
11
        func(src, index, out, dim)
rusty1s's avatar
rusty1s committed
12
13
14

        ctx.mark_dirty(out)
        ctx.save_for_backward(out, src, index)
rusty1s's avatar
rusty1s committed
15
        ctx.dim = dim
rusty1s's avatar
rusty1s committed
16
17
18
19
20

        return out

    @staticmethod
    def backward(ctx, grad_out):
21
        out, src, index = ctx.saved_tensors
rusty1s's avatar
rusty1s committed
22
23
24

        grad_src = None
        if ctx.needs_input_grad[1]:
rusty1s's avatar
rusty1s committed
25
            grad_src = -(out * grad_out).gather(ctx.dim, index) / src
rusty1s's avatar
rusty1s committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

        return None, grad_src, None, None


def scatter_div(src, index, dim=-1, out=None, dim_size=None, fill_value=1):
    r"""
    |

    .. image:: https://raw.githubusercontent.com/rusty1s/pytorch_scatter/
            master/docs/source/_figures/div.svg?sanitize=true
        :align: center
        :width: 400px

    |

    Divides all values from the :attr:`src` tensor into :attr:`out` at the
rusty1s's avatar
typo  
rusty1s committed
42
    indices specified in the :attr:`index` tensor along a given axis
rusty1s's avatar
rusty1s committed
43
44
45
46
47
48
49
50
51
    :attr:`dim`.If multiple indices reference the same location, their
    **contributions divide** (`cf.` :meth:`~torch_scatter.scatter_add`).

    For one-dimensional tensors, the operation computes

    .. math::
        \mathrm{out}_i = \mathrm{out}_i \cdot \prod_j
        \frac{1}{\mathrm{src}_j}

rusty1s's avatar
rusty1s committed
52
    where :math:`\prod_j` is over :math:`j` such that
rusty1s's avatar
rusty1s committed
53
54
55
56
57
58
59
60
61
62
63
64
65
    :math:`\mathrm{index}_j = i`.

    Args:
        src (Tensor): The source tensor.
        index (LongTensor): The indices of elements to scatter.
        dim (int, optional): The axis along which to index.
            (default: :obj:`-1`)
        out (Tensor, optional): The destination tensor. (default: :obj:`None`)
        dim_size (int, optional): If :attr:`out` is not given, automatically
            create output with size :attr:`dim_size` at dimension :attr:`dim`.
            If :attr:`dim_size` is not given, a minimal sized output tensor is
            returned. (default: :obj:`None`)
        fill_value (int, optional): If :attr:`out` is not given, automatically
rusty1s's avatar
typo  
rusty1s committed
66
            fill output tensor with :attr:`fill_value`. (default: :obj:`1`)
rusty1s's avatar
rusty1s committed
67
68
69
70
71
72
73
74
75
76

    :rtype: :class:`Tensor`

    .. testsetup::

        import torch

    .. testcode::

        from torch_scatter import scatter_div
rusty1s's avatar
rusty1s committed
77
78

        src = torch.tensor([[2, 1, 1, 4, 2], [1, 2, 1, 2, 4]]).float()
rusty1s's avatar
rusty1s committed
79
80
        index = torch.tensor([[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]])
        out = src.new_ones((2, 6))
rusty1s's avatar
rusty1s committed
81

rusty1s's avatar
rusty1s committed
82
        out = scatter_div(src, index, out=out)
rusty1s's avatar
rusty1s committed
83

rusty1s's avatar
rusty1s committed
84
85
86
87
        print(out)

    .. testoutput::

rusty1s's avatar
rusty1s committed
88
89
       tensor([[1.0000, 1.0000, 0.2500, 0.5000, 0.5000, 1.0000],
               [0.5000, 0.2500, 0.5000, 1.0000, 1.0000, 1.0000]])
rusty1s's avatar
rusty1s committed
90
91
    """
    src, out, index, dim = gen(src, index, dim, out, dim_size, fill_value)
92
93
    if src.size(dim) == 0:  # pragma: no cover
        return out
rusty1s's avatar
rusty1s committed
94
    return ScatterDiv.apply(out, src, index, dim)