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?
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.
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.
Pytorch implements an extension of sparse tensors with scalar values to sparse tensors with (contiguous) tensor values. Such tensors are called hybrid tensors.
torch.sparse. mm () Performs a matrix multiplication of the sparse matrix mat1 and the (sparse or strided) matrix mat2 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With