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[0], indices[1]] # modify this based on dimensionality
torch.sparse.FloatTensor(indices, values, dense.size())

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[0]))]
    return sparse_tensortype(indices, values, x.size())

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?