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
5ba5c620
Commit
5ba5c620
authored
Dec 18, 2017
by
rusty1s
Browse files
backward bugfix
parent
3472bc29
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
11 additions
and
26 deletions
+11
-26
test/test_max.py
test/test_max.py
+10
-20
torch_scatter/src/generic/cpu.c
torch_scatter/src/generic/cpu.c
+1
-6
No files found.
test/test_max.py
View file @
5ba5c620
...
...
@@ -6,8 +6,7 @@ from torch_scatter import scatter_max_, scatter_max
from
.utils
import
tensor_strs
,
Tensor
# @pytest.mark.parametrize('str', tensor_strs)
@
pytest
.
mark
.
parametrize
(
'str'
,
[
'IntTensor'
])
@
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
]]
...
...
@@ -25,23 +24,14 @@ def test_scatter_mean(str):
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)
#
scatter_max_(output, index, input, dim=1)
output
=
Variable
(
output
).
fill_
(
0
)
index
=
Variable
(
index
)
input
=
Variable
(
input
,
requires_grad
=
True
)
scatter_max_
(
output
,
index
,
input
,
dim
=
1
)
# grad_output = [[10, 20, 30, 40, 50, 60], [15, 25, 35, 45, 55, 65]]
# grad_output = Tensor(str, grad_output)
grad_output
=
[[
10
,
20
,
30
,
40
,
50
,
60
],
[
15
,
25
,
35
,
45
,
55
,
65
]]
grad_output
=
Tensor
(
str
,
grad_output
)
expected_grad_input
=
[[
50
,
60
,
0
,
30
,
40
],
[
0
,
15
,
0
,
35
,
25
]]
# output.backward(grad_output)
# assert index.data.tolist() == input.grad.data.tolist()
# output = Variable(torch.FloatTensor([0, 0, 0, 0, 0]))
index
=
Variable
(
torch
.
LongTensor
([
3
,
4
,
4
,
2
,
1
]))
input
=
Variable
(
torch
.
IntTensor
([
1
,
2
,
3
,
4
,
5
]),
requires_grad
=
True
)
output
,
output_index
=
scatter_max
(
index
,
input
)
# print(output, output_index)
# print(output_index)
output
.
backward
(
torch
.
IntTensor
([
10
,
20
,
30
,
40
]))
# print(input.grad)
output
.
backward
(
grad_output
)
assert
input
.
grad
.
data
.
tolist
()
==
expected_grad_input
torch_scatter/src/generic/cpu.c
View file @
5ba5c620
...
...
@@ -66,14 +66,9 @@ void scatter_(min)(int dim, THTensor *output, THLongTensor *index, THTensor *inp
}
void
index_backward
(
int
dim
,
THTensor
*
output
,
THLongTensor
*
index
,
THTensor
*
grad
,
THLongTensor
*
grad_index
)
{
int64_t
idx
;
TH_TENSOR_DIM_APPLY4
(
real
,
output
,
int64_t
,
index
,
real
,
grad
,
int64_t
,
grad_index
,
dim
,
for
(
int64_t
i
=
0
;
i
<
THLongTensor_size
(
index
,
dim
);
i
++
)
{
idx
=
*
(
index_data
+
i
*
index_stride
);
/* if (grad_index_data[index_data[i]] == i) { */
printf
(
"i: %lli, idx: %lli grad_index: %i grad: %i
\n
"
,
i
,
idx
,
*
(
grad_index_data
+
idx
*
grad_index_stride
),
*
(
grad_data
+
idx
*
grad_stride
));
/* output_data[i] = grad_data[idx]; */
/* } */
if
(
grad_index_data
[
index_data
[
i
]]
==
i
)
output_data
[
i
]
=
grad_data
[
index_data
[
i
]];
})
}
...
...
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