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
9c0419db
Commit
9c0419db
authored
Aug 23, 2018
by
rusty1s
Browse files
eye func
parent
8daf945a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
0 deletions
+31
-0
test/test_eye.py
test/test_eye.py
+7
-0
torch_sparse/__init__.py
torch_sparse/__init__.py
+2
-0
torch_sparse/eye.py
torch_sparse/eye.py
+22
-0
No files found.
test/test_eye.py
0 → 100644
View file @
9c0419db
from
torch_sparse
import
eye
def
test_eye
():
index
,
value
=
eye
(
3
)
assert
index
.
tolist
()
==
[[
0
,
1
,
2
],
[
0
,
1
,
2
]]
assert
value
.
tolist
()
==
[
1
,
1
,
1
]
torch_sparse/__init__.py
View file @
9c0419db
from
.coalesce
import
coalesce
from
.transpose
import
transpose
from
.eye
import
eye
from
.spmm
import
spmm
from
.spspmm
import
spspmm
...
...
@@ -9,6 +10,7 @@ __all__ = [
'__version__'
,
'coalesce'
,
'transpose'
,
'eye'
,
'spmm'
,
'spspmm'
,
]
torch_sparse/eye.py
0 → 100644
View file @
9c0419db
import
torch
def
eye
(
m
,
dtype
=
None
,
device
=
None
):
"""Returns a sparse matrix with ones on the diagonal and zeros elsewhere.
Args:
m (int): The first dimension of sparse matrix.
dtype (`torch.dtype`, optional): The desired data type of returned
value vector. (default is set by `torch.set_default_tensor_type()`)
device (`torch.device`, optional): The desired device of returned
tensors. (default is set by `torch.set_default_tensor_type()`)
:rtype: (:class:`LongTensor`, :class:`Tensor`)
"""
row
=
torch
.
arange
(
m
,
dtype
=
torch
.
long
,
device
=
device
)
index
=
torch
.
stack
([
row
,
row
],
dim
=
0
)
value
=
torch
.
ones
(
m
,
dtype
=
dtype
,
device
=
device
)
return
index
,
value
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