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