Sparse Matrix Autograd


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 =, x1)
b2 =, x2.t())
r1 =, b1)
r2 =, b2)

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

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


You can work around this by adding an identity multiplication :

b2 =, x2.t() * 1.0)