How to save sparse_coo_tensor?


I’m trying to save a sparse_coo_tensor, here’s an exmaple (using the torch docs for sparse_coo_tensor):

nnz = 3
dims = [5,5,2,3]
I =[torch.randint(0, dims[0], size=(nnz,)),
torch.randint(0, dims[1], size=(nnz,))], 0).reshape(2, nnz)
V = torch.randn(nnz, dims[2], dims[3])
size = torch.Size(dims)
S = torch.sparse_coo_tensor(I, V, size), '/path/to/file.pth')

What I get is RuntimeError: sparse tensors do not have storage.
Any idea how to save it? thanks

We have an open feature request for this here:

For now as a workaround, you can save tensor.indices(), tensor.values() and tensor.size() separately and reconstruct a sparse tensor from these three things.

1 Like