PyTorch torch.sparse_csr_tensor Function


Pytorch torch 参考手册Pytorch torch Reference Manual

torch.sparse_csr_tensorIt is a function in PyTorch used to construct sparse CSR (Compressed Sparse Row) tensors.

Function Definition

torch.sparse_csr_tensor(crow_indices, col_indices, values, size=None, dtype=None, device=None, requires_grad=False)

Usage Example

Example

import torch

# Create sparse CSR tensor
# Row pointers
crow_indices = torch.tensor([0, 2, 3])
# Column indices
col_indices = torch.tensor([0, 1, 1])
# Values
values = torch.tensor([1.0, 2.0, 3.0])
# Tensor size
size = (2, 3)

# Construct sparse CSR tensor
x = torch.sparse_csr_tensor(crow_indices, col_indices, values, size)

print(x)
print("Dense tensor:")
print(x.to_dense())

Pytorch torch 参考手册Pytorch torch Reference Manual

Other Extensions