# How to convert a dense matrix to a sparse one?

Hello. I need to construct a sparse matrix, can anyone tell me how to convert a dense matrix to a sparse one, or getting the index from a dense matrix? Thanks!

This should have a library function to handle this, but here’s how you can do it:

``````dense = torch.randn(3,3)
dense[[0,0,1], [1,2,0]] = 0 # make sparse
indices = torch.nonzero(dense).t()
values = dense[indices, indices] # modify this based on dimensionality
torch.sparse.FloatTensor(indices, values, dense.size())``````
2 Likes

To reformat ezyang’s answer as a simple function,

``````def to_sparse(x):
""" converts dense tensor x to sparse format """
x_typename = torch.typename(x).split('.')[-1]
sparse_tensortype = getattr(torch.sparse, x_typename)

indices = torch.nonzero(x)
if len(indices.shape) == 0:  # if all elements are zeros
return sparse_tensortype(*x.shape)
indices = indices.t()
values = x[tuple(indices[i] for i in range(indices.shape))]
return sparse_tensortype(indices, values, x.size())
``````
5 Likes

This might seem like a silly question but is there anyway we could convert a tensor to sparse and retain it’s `requires_grad` property? This is to ensure it can be backpropped within a network `.backward()` call

1 Like

Why has no one mentioned the to_sparse method?

torch.Tensor.to_sparse.html