Hi there,
I got a problem when I’m trying to convert a SciPy sparse matrix to torch form. Here is my code:
when I execute the following code:
w_csr = ss.load_npz(f'../data/w_csr.npz')
w_csr_tensor = torch.sparse_csr_tensor(torch.from_numpy(w_csr.indptr),
torch.from_numpy(w_csr.indices),
torch.from_numpy(w_csr.data))
where w_csr
is a SciPy
sparse matrix with shape 6400 x 6400
, everything work well. However, I need to manipulate the w_csr
by doing a matrix multiplication:
import scipy.sparse as ss
def re_order_w(w_csr):
n_sample = w_csr.shape[0]
I = ss.eye(n_sample, format='coo')
# for debug, I didn't apply any change on the eye matrix
w = I @ w_csr
return w
Then when I try to create the sparse tensor by the same way:
w = re_order_w(w_csr)
w_csr_tensor = torch.sparse_csr_tensor(torch.from_numpy(w.indptr),
torch.from_numpy(w.indices),
torch.from_numpy(w.data))
The program return following RunTimeError
:
RuntimeError: `col_indices[..., crow_indices[..., i - 1]:crow_indices[..., i]] for all i = 1, ..., nrows are sorted and distinct along the last dimension values` is not satisfied.
Which is weird, since I basically did nothing on the Scipy
sparse matrix. Does anyone can explain the reason?
Appreciate any reply.