Torch.repeat for sparse matrix

I want to copy a 2D sparse matrix along a new third axis. With a dense matrix, I’d use unsqueeze followed by repeat, but with a sparse matrix, I get the following error:

RuntimeError: sparse tensors do not have strides

How do I fix this?

Any help would be greatly appreciated. Thank you!

Would you mind sharing the code?

As you said unsqueeze and then repeat don’t work,

matrix=torch.sparse_coo_tensor(size=(3,3))
matrix.unsqueeze(2).repeat(1,1,4)
#RuntimeError: sparse tensors do not have strides

However, you can get around this via using list comprehension and torch.stack, by,

matrix=torch.sparse_coo_tensor(size=(3,3))
new_matrix = torch.stack([matrix for _ in range(4)], dim=2)
#returns 
#tensor(indices=tensor([], size=(3, 0)),
#       values=tensor([], size=(0,)),
#       size=(3, 3, 4), nnz=0, layout=torch.sparse_coo)

I hope this helps! :slight_smile:

1 Like