[SOLVED] Backward() in F.cosine_similarity() throws an error

When I try to do a backward pass through a F.cosine_similarity(), the following error appears:

RuntimeError: invalid argument 1: the number of sizes provided must be greater or equal to the number of dimensions in the tensor at /pytorch/torch/lib/TH/generic/THTensor.c:299

I am running PyTorch version ‘0.3.0.post4’ (which I think is the latest one available as a pip package). The following piece of code should reproduce the error:

import torch
import torch.nn.functional as F
from torch.autograd import Variable

x1 = Variable(torch.FloatTensor([1, 2]), requires_grad=True)
x2 = Variable(torch.FloatTensor([3, 4]), requires_grad=True)
y = F.cosine_similarity(x1, x2, dim=0)

Is this a bug or am I doing something wrong? If it’s a bug, is it fixed in PyTorch master version?

Thanks in advance

It’s probably a bug. I ran the above code on master and it works, so it’s fixed in the master version.


Thanks a lot! I’ll probably have to install PyTorch from source, then.

Yeah, it’s fixed in master version. Marking issue as solved.