Last active
November 3, 2022 03:06
-
-
Save aesuli/319d71707a5ee96086aa2439b87d4e38 to your computer and use it in GitHub Desktop.
csr matrix to pytorch sparse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This helps,thx.