Convert a scipy sparse matrix to a torch sparse tensor.
(sparse_mx)
| 24 | return adj, features |
| 25 | |
| 26 | def sparse_mx_to_torch_sparse_tensor(sparse_mx): |
| 27 | """Convert a scipy sparse matrix to a torch sparse tensor.""" |
| 28 | sparse_mx = sparse_mx.tocoo().astype(np.float32) |
| 29 | indices = torch.from_numpy( |
| 30 | np.vstack((sparse_mx.row, sparse_mx.col)).astype(np.int64)) |
| 31 | values = torch.from_numpy(sparse_mx.data) |
| 32 | shape = torch.Size(sparse_mx.shape) |
| 33 | return torch.sparse.FloatTensor(indices, values, shape) |
| 34 | |
| 35 | |
| 36 |
no outgoing calls
no test coverage detected