Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
torch-scatter
Commits
7f712d86
Commit
7f712d86
authored
Dec 16, 2017
by
rusty1s
Browse files
scatter add function
parent
a95ac7f5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
0 deletions
+64
-0
test/test_add.py
test/test_add.py
+44
-0
test/test_grad.py
test/test_grad.py
+20
-0
No files found.
test/test_add.py
View file @
7f712d86
from
nose.tools
import
assert_equal
from
nose.tools
import
assert_equal
import
torch
import
torch
from
torch.autograd
import
Variable
from
torch_scatter._ext
import
ffi
from
torch_scatter._ext
import
ffi
class
ScatterAdd
(
torch
.
autograd
.
Function
):
def
__init__
(
self
,
dim
):
super
(
ScatterAdd
,
self
).
__init__
()
self
.
dim
=
dim
def
forward
(
self
,
output
,
index
,
input
):
assert
not
self
.
needs_input_grad
[
1
],
'Can
\'
t differentiate the index'
self
.
mark_dirty
(
output
)
self
.
save_for_backward
(
index
)
ffi
.
scatter_add_Float
(
output
,
index
,
input
,
self
.
dim
)
return
output
def
backward
(
self
,
grad
):
index
,
=
self
.
saved_variables
grad_output
=
grad_input
=
None
if
self
.
needs_input_grad
[
0
]:
grad_output
=
grad
if
self
.
needs_input_grad
[
2
]:
grad_input
=
grad
.
gather
(
self
.
dim
,
index
.
data
)
return
grad_output
,
None
,
grad_input
def
test_scatter_add
():
def
test_scatter_add
():
input
=
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]]
input
=
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]]
index
=
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]]
index
=
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]]
...
@@ -14,3 +41,20 @@ def test_scatter_add():
...
@@ -14,3 +41,20 @@ def test_scatter_add():
ffi
.
scatter_add_Float
(
output
,
index
,
input
,
1
)
ffi
.
scatter_add_Float
(
output
,
index
,
input
,
1
)
assert_equal
(
output
.
tolist
(),
expected_output
)
assert_equal
(
output
.
tolist
(),
expected_output
)
output
=
Variable
(
output
)
index
=
Variable
(
index
)
input
=
Variable
(
input
,
requires_grad
=
True
)
# a = input * 2
# b = output * 2
a
=
input
*
2
b
=
output
*
2
ScatterAdd
(
1
)(
b
,
index
,
a
)
# b.scatter_add_(1, index, a)
c
=
b
.
sum
()
c
.
backward
()
print
(
input
.
grad
)
print
(
output
.
grad
)
test/test_grad.py
0 → 100644
View file @
7f712d86
import
torch
from
torch.autograd
import
Variable
def
test_grad
():
input
=
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]]
index
=
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]]
input
=
torch
.
FloatTensor
(
input
)
index
=
torch
.
LongTensor
(
index
)
output
=
input
.
new
(
2
,
6
).
fill_
(
0
)
output
=
Variable
(
output
)
index
=
Variable
(
index
)
input
=
Variable
(
input
,
requires_grad
=
True
)
output
.
scatter_add_
(
1
,
index
,
input
)
c
=
output
.
mean
()
c
.
backward
()
# print(index.grad)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment