PyTorch torch.sparse_csr_tensor function
PyTorch torch reference manual
torch.sparse_csr_tensor is a function in PyTorch used to construct a sparse CSR (compressed sparse row) tensor.
Function definition
torch.sparse_csr_tensor(crow_indices, col_indices, values, size=None, dtype=None, device=None, requires_grad=False)
Usage examples
Example
import
torch
Create a sparse CSR tensor
Row pointers
crow_indices
=
torch.
tensor
(
[
0
,
2
,
3
]
)
Column indices
col_indices
=
torch.
tensor
(
[
0
,
1
,
1
]
)
Value
values
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
]
)
Tensor size
size
=
(
2
,
3
)
Construct a sparse CSR tensor
x
=
torch.
sparse_csr_tensor
(
crow_indices
,
col_indices
,
values
,
size
)
(
x
)
(
"Dense tensor:"
)
(
x.
to_dense
(
)
)