Sparse Matrix Autograd


(Carsten Stoll) #1

Hi,

I know that Sparse Matrix multiplications are now supported in autograd. However, I’ve come across a scenario where this doesn’t work for me. Here’s a minimal example:

params = torch.rand((20), requires_grad=True)
A = torch.sparse.FloatTensor(20,20)
x1 = params.reshape([params.size()[0], 1])
x2 = params.unsqueeze(0)
b1 = torch.mm(A, x1)
b2 = torch.mm(A, x2.t())
r1 = torch.mm(b1.t(), b1)
r2 = torch.mm(b2.t(), b2)

# r1.backward() <-- this would work just fine
r2.backward()

Fails with
RuntimeError: Expected object of type torch.FloatTensor but found type torch.sparse.FloatTensor for argument #2 ‘mat2’

Any help? This is on PyTorch 0.4.1

Thanks!
Carsten


(Carsten Stoll) #2

You can work around this by adding an identity multiplication :

b2 = torch.mm(A, x2.t() * 1.0)