Liang Yi
Liang Yi

Reputation: 11

Failed to compute dot product of torch.cuda.FloatTensor

I used a GPU to compute the dot product of the output of neural networks and a torch.cuda.FloatTensor (both of them are stored in GPU) but got an error saying:

TypeError: dot received an invalid combination of arguments - got (torch.cuda.FloatTensor) but expected (torch.FloatTensor tensor).

the codes are like

p = torch.exp(vector.dot(ht))

here vector is a torch FloatTensor and ht is the output of neural networks.

I've struggled with these things for days but still got no idea. Thanks in advance for any possible solution!

Upvotes: 1

Views: 944

Answers (1)

Wasi Ahmad
Wasi Ahmad

Reputation: 37681

What does the following error message mean?

TypeError: dot received an invalid combination of arguments - got (torch.cuda.FloatTensor) but expected (torch.FloatTensor tensor).

It means dot function expected cpu tensor but you are providing a gpu (cuda) tensor.


So, how to solve the problem of your code?

p = torch.exp(vector.dot(ht))

As you mentioned vector is a FloatTensor, so ht should be a FloatTensor as well but ht is a cuda.FloatTensor (because, your neural network model is in gpu memory).

So, you should convert vector to cuda.FloatTensor by doing the following.

vector = vector.cuda()

OR, you can convert the cuda.FloatTensor to cpu tensor by doing the following. Please note, .cpu() method is not applicable for Variable. In that case, you can use .data.cpu().

ht = ht.cpu()

It should solve your problem.

Upvotes: 2

Related Questions