Here is an even clearer example

  
      gist.github.com
  
  
    https://gist.github.com/shchur/9428f6f00dd8e6ea617640d51fc7c8a9 151
sparse_dense_mm.py
import numpy as np
import scipy.sparse as sp
import torch

def to_torch_sparse_tensor(M):
    """Convert Scipy sparse matrix to torch sparse tensor."""
    M = M.tocoo().astype(np.float32)
    indices = torch.from_numpy(np.vstack((M.row, M.col))).long()
    values = torch.from_numpy(M.data)
    shape = torch.Size(M.shape)
This file has been truncated. show original