Skip to content

Commit f25c0e7

Browse files
committed
added sub
1 parent 8b8df25 commit f25c0e7

File tree

6 files changed

+89
-126
lines changed

6 files changed

+89
-126
lines changed

test/test_backward.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from .utils import devices
99

10-
funcs = ['add']
10+
funcs = ['add', 'sub']
1111
indices = [2, 0, 1, 1, 0]
1212

1313

test/test_forward.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,30 @@
1010
'name': 'add',
1111
'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
1212
'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
13+
'dim': -1,
1314
'fill_value': 0,
1415
'expected': [[0, 0, 4, 3, 3, 0], [2, 4, 4, 0, 0, 0]]
16+
}, {
17+
'name': 'add',
18+
'src': [[5, 2], [2, 5], [4, 3], [1, 3]],
19+
'index': [[0, 0], [1, 1], [1, 1], [0, 0]],
20+
'dim': 0,
21+
'fill_value': 0,
22+
'expected': [[6, 5], [6, 8]]
23+
}, {
24+
'name': 'sub',
25+
'src': [[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]],
26+
'index': [[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]],
27+
'dim': -1,
28+
'fill_value': 9,
29+
'expected': [[9, 9, 5, 6, 6, 9], [7, 5, 5, 9, 9, 9]]
30+
}, {
31+
'name': 'sub',
32+
'src': [[5, 2], [2, 2], [4, 2], [1, 3]],
33+
'index': [[0, 0], [1, 1], [1, 1], [0, 0]],
34+
'dim': 0,
35+
'fill_value': 9,
36+
'expected': [[3, 4], [3, 5]]
1537
}]
1638

1739

@@ -21,29 +43,6 @@ def test_forward(test, dtype, device):
2143
index = tensor(test['index'], torch.long, device)
2244

2345
op = getattr(torch_scatter, 'scatter_{}'.format(test['name']))
24-
output = op(src, index, fill_value=test['fill_value'])
46+
output = op(src, index, test['dim'], fill_value=test['fill_value'])
2547

2648
assert output.tolist() == test['expected']
27-
# name = data[i]['name']
28-
# index = torch.LongTensor(data[i]['index'])
29-
# input = Tensor(tensor, data[i]['input'])
30-
# dim = data[i]['dim']
31-
# fill_value = data[i]['fill_value']
32-
# expected = torch.FloatTensor(data[i]['expected']).type_as(input)
33-
# output = expected.new(expected.size()).fill_(fill_value)
34-
35-
# func = getattr(torch_scatter, 'scatter_{}_'.format(name))
36-
# result = func(output, index, input, dim)
37-
# assert output.tolist() == expected.tolist()
38-
# if 'expected_arg' in data[i]:
39-
# expected_arg = torch.LongTensor(data[i]['expected_arg'])
40-
# assert result[1].tolist() == expected_arg.tolist()
41-
42-
# func = getattr(torch_scatter, 'scatter_{}'.format(name))
43-
# result = func(index, input, dim, fill_value=fill_value)
44-
# if 'expected_arg' not in data[i]:
45-
# assert result.tolist() == expected.tolist()
46-
# else:
47-
# expected_arg = torch.LongTensor(data[i]['expected_arg'])
48-
# assert result[0].tolist() == expected.tolist()
49-
# assert result[1].tolist() == expected_arg.tolist()

torch_scatter/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from .add import ScatterAdd, scatter_add
1+
from .add import scatter_add
2+
from .sub import scatter_sub
23

34
__version__ = '1.0.0'
45

5-
__all__ = ['ScatterAdd', 'scatter_add', '__version__']
6+
__all__ = ['scatter_add', 'scatter_sub', '__version__']

torch_scatter/add.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def scatter_add(src, index, dim=-1, out=None, dim_size=None, fill_value=0):
7373
7474
.. testcode::
7575
76-
from torch_scatter import scatter_add_
76+
from torch_scatter import scatter_add
7777
src = torch.tensor([[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]])
7878
index = torch.tensor([[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]])
7979
out = src.new_zeros((2, 6))

torch_scatter/functions/sub.py

Lines changed: 0 additions & 98 deletions
This file was deleted.

torch_scatter/sub.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from .add import scatter_add
2+
3+
4+
def scatter_sub(src, index, dim=-1, out=None, dim_size=None, fill_value=0):
5+
r"""
6+
|
7+
8+
.. image:: https://raw.githubusercontent.com/rusty1s/pytorch_scatter/
9+
master/docs/source/_figures/sub.svg?sanitize=true
10+
:align: center
11+
:width: 400px
12+
13+
|
14+
15+
Subtracts all values from the :attr:`src` tensor into :attr:`out` at the
16+
indices specified in the :attr:`index` tensor along an given axis
17+
:attr:`dim`.If multiple indices reference the same location, their
18+
**negated contributions add** (`cf.` :meth:`~torch_scatter.scatter_add`).
19+
20+
For one-dimensional tensors, the operation computes
21+
22+
.. math::
23+
\mathrm{out}_i = \mathrm{out}_i - \sum_j \mathrm{src}_j
24+
25+
where sum is over :math:`j` such that :math:`\mathrm{index}_j = i`.
26+
27+
Args:
28+
src (Tensor): The source tensor.
29+
index (LongTensor): The indices of elements to scatter.
30+
dim (int, optional): The axis along which to index.
31+
(default: :obj:`-1`)
32+
out (Tensor, optional): The destination tensor. (default: :obj:`None`)
33+
dim_size (int, optional): If :attr:`out` is not given, automatically
34+
create output with size :attr:`dim_size` at dimension :attr:`dim`.
35+
If :attr:`dim_size` is not given, a minimal sized output tensor is
36+
returned. (default: :obj:`None`)
37+
fill_value (int, optional): If :attr:`out` is not given, automatically
38+
fill output tensor with :attr:`fill_value`. (default: :obj:`0`)
39+
40+
:rtype: :class:`Tensor`
41+
42+
.. testsetup::
43+
44+
import torch
45+
46+
.. testcode::
47+
48+
from torch_scatter import scatter_sub
49+
src = torch.tensor([[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]])
50+
index = torch.tensor([[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]])
51+
out = src.new_zeros((2, 6))
52+
out = scatter_sub(src, index, out=out)
53+
print(out)
54+
55+
.. testoutput::
56+
57+
0 0 -4 -3 -3 0
58+
-2 -4 -4 0 0 0
59+
[torch.FloatTensor of size 2x6]
60+
"""
61+
return scatter_add(src.neg(), index, dim, out, dim_size, fill_value)

0 commit comments

Comments
 (0)