Skip to content

Instantly share code, notes, and snippets.

@aesuli
Last active November 3, 2022 03:06
Show Gist options
  • Save aesuli/319d71707a5ee96086aa2439b87d4e38 to your computer and use it in GitHub Desktop.
Save aesuli/319d71707a5ee96086aa2439b87d4e38 to your computer and use it in GitHub Desktop.
csr matrix to pytorch sparse
import numpy as np
from scipy.sparse import csr_matrix
import torch
__author__ = 'Andrea Esuli'
Acsr = csr_matrix([[1, 2, 0], [0, 0, 3], [4, 0, 5]])
print('Acsr',Acsr)
Acoo = Acsr.tocoo()
print('Acoo',Acoo)
Apt = torch.sparse.LongTensor(torch.LongTensor([Acoo.row.tolist(), Acoo.col.tolist()]),
torch.LongTensor(Acoo.data.astype(np.int32)))
print('Apt',Apt)
@xysscn
Copy link

xysscn commented Oct 25, 2019

This helps,thx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment