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
0466dd06
Commit
0466dd06
authored
Dec 17, 2017
by
rusty1s
Browse files
max test
parent
1c4ef780
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
2 deletions
+45
-2
test/test_max.py
test/test_max.py
+36
-0
torch_scatter/functions/__init__.py
torch_scatter/functions/__init__.py
+8
-2
torch_scatter/functions/scatter.py
torch_scatter/functions/scatter.py
+1
-0
No files found.
test/test_max.py
0 → 100644
View file @
0466dd06
import
pytest
import
torch
from
torch.autograd
import
Variable
from
torch_scatter
import
scatter_max_
,
scatter_max
from
.utils
import
tensor_strs
,
Tensor
@
pytest
.
mark
.
parametrize
(
'str'
,
tensor_strs
)
def
test_scatter_mean
(
str
):
input
=
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]]
index
=
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]]
input
=
Tensor
(
str
,
input
)
index
=
torch
.
LongTensor
(
index
)
output
=
input
.
new
(
2
,
6
).
fill_
(
0
)
expected_output
=
[[
0
,
0
,
4
,
3
,
2
,
0
],
[
2
,
4
,
3
,
0
,
0
,
0
]]
expected_output_index
=
[[
-
1
,
-
1
,
3
,
4
,
0
,
1
],
[
1
,
4
,
3
,
-
1
,
-
1
,
-
1
]]
_
,
output_index
=
scatter_max_
(
output
,
index
,
input
,
dim
=
1
)
assert
output
.
tolist
()
==
expected_output
assert
output_index
.
tolist
()
==
expected_output_index
output
,
output_index
=
scatter_max
(
index
,
input
,
dim
=
1
)
assert
output
.
tolist
()
==
expected_output
assert
output_index
.
tolist
()
==
expected_output_index
output
=
Variable
(
output
).
fill_
(
0
)
index
=
Variable
(
index
)
input
=
Variable
(
input
,
requires_grad
=
True
)
_
,
output_index
=
scatter_max_
(
output
,
index
,
input
,
dim
=
1
)
grad_output
=
[[
0
,
1
,
2
,
3
,
4
,
5
],
[
0
,
1
,
2
,
3
,
4
,
5
]]
grad_output
=
Tensor
(
str
,
grad_output
)
output
.
backward
(
grad_output
)
assert
index
.
data
.
tolist
()
==
input
.
grad
.
data
.
tolist
()
torch_scatter/functions/__init__.py
View file @
0466dd06
...
...
@@ -62,7 +62,10 @@ def scatter_mean(index, input, dim=0, max_index=None, fill_value=0):
def
scatter_max_
(
output
,
index
,
input
,
dim
=
0
):
output_index
=
index
.
new
(
output
.
size
()).
fill_
(
-
1
)
if
torch
.
is_tensor
(
input
):
output_index
=
index
.
new
(
output
.
size
()).
fill_
(
-
1
)
else
:
output_index
=
Variable
(
index
.
data
.
new
(
output
.
size
()).
fill_
(
-
1
))
scatter
(
'max'
,
dim
,
output
,
index
,
input
,
output_index
)
return
output
,
output_index
...
...
@@ -73,7 +76,10 @@ def scatter_max(index, input, dim=0, max_index=None, fill_value=0):
def
scatter_min_
(
output
,
index
,
input
,
dim
=
0
):
output_index
=
index
.
new
(
output
.
size
()).
fill_
(
-
1
)
if
torch
.
is_tensor
(
input
):
output_index
=
index
.
new
(
output
.
size
()).
fill_
(
-
1
)
else
:
output_index
=
Variable
(
index
.
data
.
new
(
output
.
size
()).
fill_
(
-
1
))
scatter
(
'min'
,
dim
,
output
,
index
,
input
,
output_index
)
return
output
,
output_index
...
...
torch_scatter/functions/scatter.py
View file @
0466dd06
...
...
@@ -33,6 +33,7 @@ class _Scatter(Function):
if
self
.
needs_input_grad
[
0
]:
grad_output
=
data
[
0
]
if
self
.
needs_input_grad
[
2
]:
# TODO: max and min
grad_input
=
data
[
0
].
gather
(
self
.
dim
,
index
.
data
)
return
(
grad_output
,
None
,
grad_input
)
+
(
None
,
)
*
(
self
.
len
-
3
)
...
...
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