Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to multiply a dense matrix by a sparse matrix element-wise in pytorch

I can use torch.sparse.mm() or torch.spmm() to do multiplication between sparse matrix and dense matrix directly, but which function should I choose to do element-wise multiplication?

like image 570
Liu Avatar asked Jul 04 '19 03:07

Liu


People also ask

How does PyTorch multiply matrices?

For matrix multiplication in PyTorch, use torch.mm() . Numpy's np. dot() in contrast is more flexible; it computes the inner product for 1D arrays and performs matrix multiplication for 2D arrays.

How do you multiply sparse matrices?

To Multiply the matrices, we first calculate transpose of the second matrix to simplify our comparisons and maintain the sorted order. So, the resultant matrix is obtained by traversing through the entire length of both matrices and summing the appropriate multiplied values.

Does PyTorch support sparse tensors?

Pytorch implements an extension of sparse tensors with scalar values to sparse tensors with (contiguous) tensor values. Such tensors are called hybrid tensors.

What is Torch SPMM?

torch.sparse. mm () Performs a matrix multiplication of the sparse matrix mat1 and the (sparse or strided) matrix mat2 .


1 Answers

You can implement this multiplication yourself

def sparse_dense_mul(s, d):
  i = s._indices()
  v = s._values()
  dv = d[i[0,:], i[1,:]]  # get values from relevant entries of dense matrix
  return torch.sparse.FloatTensor(i, v * dv, s.size())

Note that thanks to the linearity of the multiplication operation you do not need to worry if s is coalesced or not.

like image 85
Shai Avatar answered Oct 13 '22 19:10

Shai