PyTorch torch.sparse_coo_tensor function
PyTorch torch reference manual
torch.sparse_coo_tensor is a function in PyTorch used to construct a sparse COO (coordinate format) tensor.
Function definition
torch.sparse_coo_tensor(indices, values, size=None, dtype=None, device=None, requires_grad=False)
Usage examples
Example
import
torch
Create a sparse COO tensor
Index coordinates
indices
=
torch.
tensor
(
[
[
0
,
1
,
2
]
,
[
0
,
2
,
1
]
]
)
Corresponding values
values
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
]
)
Tensor size
size
=
(
3
,
3
)
Construct a sparse COO tensor
x
=
torch.
sparse_coo_tensor
(
indices
,
values
,
size
)
(
x
)
(
"Dense tensor:"
)
(
x.
to_dense
(
)
)