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
d367c0b5
Commit
d367c0b5
authored
Apr 28, 2018
by
rusty1s
Browse files
scatter div
parent
4b654c29
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
121 additions
and
14 deletions
+121
-14
test/test_backward.py
test/test_backward.py
+1
-1
test/test_forward.py
test/test_forward.py
+14
-0
torch_scatter/__init__.py
torch_scatter/__init__.py
+3
-1
torch_scatter/add.py
torch_scatter/add.py
+2
-1
torch_scatter/div.py
torch_scatter/div.py
+89
-0
torch_scatter/mean.py
torch_scatter/mean.py
+2
-2
torch_scatter/mul.py
torch_scatter/mul.py
+8
-8
torch_scatter/sub.py
torch_scatter/sub.py
+2
-1
No files found.
test/test_backward.py
View file @
d367c0b5
...
@@ -7,7 +7,7 @@ import torch_scatter
...
@@ -7,7 +7,7 @@ import torch_scatter
from
.utils
import
devices
from
.utils
import
devices
funcs
=
[
'add'
,
'sub'
,
'mul'
,
'mean'
]
funcs
=
[
'add'
,
'sub'
,
'mul'
,
'div'
,
'mean'
]
indices
=
[
2
,
0
,
1
,
1
,
0
]
indices
=
[
2
,
0
,
1
,
1
,
0
]
...
...
test/test_forward.py
View file @
d367c0b5
...
@@ -48,6 +48,20 @@ tests = [{
...
@@ -48,6 +48,20 @@ tests = [{
'dim'
:
0
,
'dim'
:
0
,
'fill_value'
:
1
,
'fill_value'
:
1
,
'expected'
:
[[
5
,
6
],
[
8
,
15
]]
'expected'
:
[[
5
,
6
],
[
8
,
15
]]
},
{
'name'
:
'div'
,
'src'
:
[[
2
,
1
,
1
,
4
,
2
],
[
1
,
2
,
1
,
2
,
4
]],
'index'
:
[[
4
,
5
,
4
,
2
,
3
],
[
0
,
0
,
2
,
2
,
1
]],
'dim'
:
-
1
,
'fill_value'
:
1
,
'expected'
:
[[
1
,
1
,
0.25
,
0.5
,
0.5
,
1
],
[
0.5
,
0.25
,
0.5
,
1
,
1
,
1
]]
},
{
'name'
:
'div'
,
'src'
:
[[
4
,
2
],
[
2
,
1
],
[
4
,
2
],
[
1
,
2
]],
'index'
:
[[
0
,
0
],
[
1
,
1
],
[
1
,
1
],
[
0
,
0
]],
'dim'
:
0
,
'fill_value'
:
1
,
'expected'
:
[[
0.25
,
0.25
],
[
0.125
,
0.5
]]
},
{
},
{
'name'
:
'mean'
,
'name'
:
'mean'
,
'src'
:
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]],
'src'
:
[[
2
,
0
,
1
,
4
,
3
],
[
0
,
2
,
1
,
3
,
4
]],
...
...
torch_scatter/__init__.py
View file @
d367c0b5
from
.add
import
scatter_add
from
.add
import
scatter_add
from
.sub
import
scatter_sub
from
.sub
import
scatter_sub
from
.mul
import
scatter_mul
from
.mul
import
scatter_mul
from
.div
import
scatter_div
from
.mean
import
scatter_mean
from
.mean
import
scatter_mean
__version__
=
'1.0.0'
__version__
=
'1.0.0'
__all__
=
[
__all__
=
[
'scatter_add'
,
'scatter_sub'
,
'scatter_mul'
,
'scatter_mean'
,
'__version__'
'scatter_add'
,
'scatter_sub'
,
'scatter_mul'
,
'scatter_div'
,
'scatter_mean'
,
'__version__'
]
]
torch_scatter/add.py
View file @
d367c0b5
...
@@ -50,7 +50,8 @@ def scatter_add(src, index, dim=-1, out=None, dim_size=None, fill_value=0):
...
@@ -50,7 +50,8 @@ def scatter_add(src, index, dim=-1, out=None, dim_size=None, fill_value=0):
.. math::
.. math::
\mathrm{out}_i = \mathrm{out}_i + \sum_j \mathrm{src}_j
\mathrm{out}_i = \mathrm{out}_i + \sum_j \mathrm{src}_j
where sum is over :math:`j` such that :math:`\mathrm{index}_j = i`.
where :math:`\sum` is over :math:`j` such that
:math:`\mathrm{index}_j = i`.
Args:
Args:
src (Tensor): The source tensor.
src (Tensor): The source tensor.
...
...
torch_scatter/div.py
0 → 100644
View file @
d367c0b5
from
torch.autograd
import
Function
from
.utils.ffi
import
get_func
from
.utils.gen
import
gen
class
ScatterDiv
(
Function
):
@
staticmethod
def
forward
(
ctx
,
out
,
src
,
index
,
dim
):
func
=
get_func
(
'scatter_div'
,
src
)
func
(
dim
,
out
,
index
,
src
)
ctx
.
mark_dirty
(
out
)
ctx
.
save_for_backward
(
out
,
src
,
index
)
return
out
@
staticmethod
def
backward
(
ctx
,
grad_out
):
out
,
src
,
index
=
ctx
.
saved_variables
grad_src
=
None
if
ctx
.
needs_input_grad
[
1
]:
grad_src
=
-
(
out
*
grad_out
)[
index
]
/
src
return
None
,
grad_src
,
None
,
None
def
scatter_div
(
src
,
index
,
dim
=-
1
,
out
=
None
,
dim_size
=
None
,
fill_value
=
1
):
r
"""
|
.. image:: https://raw.githubusercontent.com/rusty1s/pytorch_scatter/
master/docs/source/_figures/div.svg?sanitize=true
:align: center
:width: 400px
|
Divides all values from the :attr:`src` tensor into :attr:`out` at the
indices specified in the :attr:`index` tensor along an given axis
:attr:`dim`.If multiple indices reference the same location, their
**contributions divide** (`cf.` :meth:`~torch_scatter.scatter_add`).
For one-dimensional tensors, the operation computes
.. math::
\mathrm{out}_i = \mathrm{out}_i \cdot \prod_j
\frac{1}{\mathrm{src}_j}
where :math:`\prod` is over :math:`j` such that
:math:`\mathrm{index}_j = i`.
Args:
src (Tensor): The source tensor.
index (LongTensor): The indices of elements to scatter.
dim (int, optional): The axis along which to index.
(default: :obj:`-1`)
out (Tensor, optional): The destination tensor. (default: :obj:`None`)
dim_size (int, optional): If :attr:`out` is not given, automatically
create output with size :attr:`dim_size` at dimension :attr:`dim`.
If :attr:`dim_size` is not given, a minimal sized output tensor is
returned. (default: :obj:`None`)
fill_value (int, optional): If :attr:`out` is not given, automatically
fill output tensor with :attr:`fill_value`. (default: :obj:`0`)
:rtype: :class:`Tensor`
.. testsetup::
import torch
.. testcode::
from torch_scatter import scatter_div
src = torch.tensor([[2, 0, 3, 4, 3], [2, 3, 4, 2, 4]])
index = torch.tensor([[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]])
out = src.new_ones((2, 6))
out = scatter_div(src, index, out=out)
print(out)
.. testoutput::
1.0000 1.0000 0.2500 0.3333 0.2500 1.0000
0.5000 0.2500 0.1667 1.0000 1.0000 1.0000
[torch.FloatTensor of size 2x6]
"""
src
,
out
,
index
,
dim
=
gen
(
src
,
index
,
dim
,
out
,
dim_size
,
fill_value
)
return
ScatterDiv
.
apply
(
out
,
src
,
index
,
dim
)
torch_scatter/mean.py
View file @
d367c0b5
...
@@ -51,8 +51,8 @@ def scatter_mean(src, index, dim=-1, out=None, dim_size=None, fill_value=0):
...
@@ -51,8 +51,8 @@ def scatter_mean(src, index, dim=-1, out=None, dim_size=None, fill_value=0):
\mathrm{out}_i = \mathrm{out}_i + \frac{1}{N_i} \cdot
\mathrm{out}_i = \mathrm{out}_i + \frac{1}{N_i} \cdot
\sum_j \mathrm{src}_j
\sum_j \mathrm{src}_j
where sum is over :math:`j` such that :math:`\mathrm{index}_j = i`
and
where
:math:`\
sum
`
is over :math:`j` such that :math:`\mathrm{index}_j = i`
:math:`N_i` indicates the number of indices referencing :math:`i`.
add
:math:`N_i` indicates the number of indices referencing :math:`i`.
Args:
Args:
src (Tensor): The source tensor.
src (Tensor): The source tensor.
...
...
torch_scatter/mul.py
View file @
d367c0b5
...
@@ -10,7 +10,6 @@ class ScatterMul(Function):
...
@@ -10,7 +10,6 @@ class ScatterMul(Function):
func
=
get_func
(
'scatter_mul'
,
src
)
func
=
get_func
(
'scatter_mul'
,
src
)
func
(
dim
,
out
,
index
,
src
)
func
(
dim
,
out
,
index
,
src
)
ctx
.
dim
=
dim
ctx
.
mark_dirty
(
out
)
ctx
.
mark_dirty
(
out
)
ctx
.
save_for_backward
(
out
,
src
,
index
)
ctx
.
save_for_backward
(
out
,
src
,
index
)
...
@@ -48,7 +47,8 @@ def scatter_mul(src, index, dim=-1, out=None, dim_size=None, fill_value=1):
...
@@ -48,7 +47,8 @@ def scatter_mul(src, index, dim=-1, out=None, dim_size=None, fill_value=1):
.. math::
.. math::
\mathrm{out}_i = \mathrm{out}_i \cdot \prod_j \mathrm{src}_j
\mathrm{out}_i = \mathrm{out}_i \cdot \prod_j \mathrm{src}_j
where sum is over :math:`j` such that :math:`\mathrm{index}_j = i`.
where :math:`\prod` is over :math:`j` such that
:math:`\mathrm{index}_j = i`.
Args:
Args:
src (Tensor): The source tensor.
src (Tensor): The source tensor.
...
@@ -71,17 +71,17 @@ def scatter_mul(src, index, dim=-1, out=None, dim_size=None, fill_value=1):
...
@@ -71,17 +71,17 @@ def scatter_mul(src, index, dim=-1, out=None, dim_size=None, fill_value=1):
.. testcode::
.. testcode::
from torch_scatter import scatter_m
ean
from torch_scatter import scatter_m
ul
src = torch.tensor([[2, 0,
1
, 4, 3], [
0
,
2
,
1
,
3
, 4]])
src = torch.tensor([[2, 0,
3
, 4, 3], [
2
,
3
,
4
,
2
, 4]])
index = torch.tensor([[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]])
index = torch.tensor([[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]])
out = src.new_
zero
s((2, 6))
out = src.new_
one
s((2, 6))
out = scatter_m
ean
(src, index, out=out)
out = scatter_m
ul
(src, index, out=out)
print(out)
print(out)
.. testoutput::
.. testoutput::
0.0000 0.0000 4.0000 3.0000 1.5000 0.000
0
1 1 4 3 6
0
1.0000 4.0000 2.0000 0.0000 0.0000 0.0000
6 4 8 1 1 1
[torch.FloatTensor of size 2x6]
[torch.FloatTensor of size 2x6]
"""
"""
src
,
out
,
index
,
dim
=
gen
(
src
,
index
,
dim
,
out
,
dim_size
,
fill_value
)
src
,
out
,
index
,
dim
=
gen
(
src
,
index
,
dim
,
out
,
dim_size
,
fill_value
)
...
...
torch_scatter/sub.py
View file @
d367c0b5
...
@@ -22,7 +22,8 @@ def scatter_sub(src, index, dim=-1, out=None, dim_size=None, fill_value=0):
...
@@ -22,7 +22,8 @@ def scatter_sub(src, index, dim=-1, out=None, dim_size=None, fill_value=0):
.. math::
.. math::
\mathrm{out}_i = \mathrm{out}_i - \sum_j \mathrm{src}_j
\mathrm{out}_i = \mathrm{out}_i - \sum_j \mathrm{src}_j
where sum is over :math:`j` such that :math:`\mathrm{index}_j = i`.
where :math:`\sum` is over :math:`j` such that
:math:`\mathrm{index}_j = i`.
Args:
Args:
src (Tensor): The source tensor.
src (Tensor): The source tensor.
...
...
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