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-sparse
Commits
266dffd3
Commit
266dffd3
authored
Jul 16, 2020
by
rusty1s
Browse files
allow indexing via Python lists
parent
049416bc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
0 deletions
+23
-0
test/test_tensor.py
test/test_tensor.py
+21
-0
torch_sparse/tensor.py
torch_sparse/tensor.py
+2
-0
No files found.
test/test_tensor.py
0 → 100644
View file @
266dffd3
from
itertools
import
product
import
pytest
import
torch
from
torch_sparse
import
SparseTensor
from
.utils
import
grad_dtypes
,
devices
@
pytest
.
mark
.
parametrize
(
'dtype,device'
,
product
(
grad_dtypes
,
devices
))
def
test_getitem
(
dtype
,
device
):
mat
=
torch
.
randn
(
50
,
40
,
dtype
=
dtype
,
device
=
device
)
mat
=
SparseTensor
.
from_dense
(
mat
)
idx1
=
torch
.
randint
(
0
,
50
,
(
10
,
),
dtype
=
torch
.
long
,
device
=
device
)
idx2
=
torch
.
randint
(
0
,
40
,
(
10
,
),
dtype
=
torch
.
long
,
device
=
device
)
assert
mat
[:
10
,
:
10
].
sizes
()
==
[
10
,
10
]
assert
mat
[...,
:
10
].
sizes
()
==
[
50
,
10
]
assert
mat
[
idx1
,
idx2
].
sizes
()
==
[
10
,
10
]
assert
mat
[
idx1
.
tolist
()].
sizes
()
==
[
10
,
40
]
torch_sparse/tensor.py
View file @
266dffd3
...
@@ -442,6 +442,8 @@ def __getitem__(self: SparseTensor, index: Any) -> SparseTensor:
...
@@ -442,6 +442,8 @@ def __getitem__(self: SparseTensor, index: Any) -> SparseTensor:
out
=
self
out
=
self
while
len
(
index
)
>
0
:
while
len
(
index
)
>
0
:
item
=
index
.
pop
(
0
)
item
=
index
.
pop
(
0
)
if
isinstance
(
item
,
(
list
,
tuple
)):
item
=
torch
.
tensor
(
item
,
dtype
=
torch
.
long
,
device
=
self
.
device
())
if
isinstance
(
item
,
int
):
if
isinstance
(
item
,
int
):
out
=
out
.
select
(
dim
,
item
)
out
=
out
.
select
(
dim
,
item
)
dim
+=
1
dim
+=
1
...
...
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