PyTorch torch.sparse_coo_tensor Function
Pytorch torch Reference Manual
torch.sparse_coo_tensoris a function in PyTorch used to construct sparse COO (coordinate format) tensors.
Function Definition
torch.sparse_coo_tensor(indices, values, size=None, dtype=None, device=None, requires_grad=False)
Usage Example
Example
import torch
# Create 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 sparse COO tensor
x = torch.sparse_coo_tensor(indices, values, size)
print(x)
print("Dense tensor:")
print(x.to_dense())
# Create 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 sparse COO tensor
x = torch.sparse_coo_tensor(indices, values, size)
print(x)
print("Dense tensor:")
print(x.to_dense())
Other Extensions