'torch.FloatTensor' object has no attribute 'requires_grad'

Hello, I am a beginner with PyTorch and just running some basic example code in my jupiter notebook. When I was trying to un the code:
import torch
a = torch.ones(2,2,requires_grad=True);

It reported the error that: TypeError: torch.ones received an invalid combination of arguments - got (int, int, requires_grad=bool), but expected one of:

  • (int … size)
    didn’t match because some of the keywords were incorrect: requires_grad
  • (torch.Size size)

I know this is not supposed to happen and there might be some version related issue. I am using conda python 3 and installed the PyTorch following the page suggestion. Thanks in advance.

Try to wrap the size: torch.ones((2, 2), requires_grad=True).

1 Like

Hi! Thank you for the response!
But it will report that
torch.ones received an invalid combination of arguments - got (tuple, requires_grad=bool), but expected one of

I am wondering I am using a different PyTorch:(

Which version are you using?
0.4.0 was released just yesterday merging Variables and Tensors.
Did you update?

Yeah I have updated and the issue was solved. Thanks!

Hi @ptrblck, I’m using pytorch 0.4.0 (py36_cuda9.0.176_cudnn7.1.2_1 [cuda90]) trying to run the NN tutorial. I ran it from home a week ago on my laptop no problem, but having difficulties running it on a ubuntu machine with cuda. Has it been updated for the newest version of pytorch, or was it just not meant to support pytorch with cuda?

I first got an error on line: outputs = net(inputs) : TypeError: argument 0 is not a Variable
Then I wrapped inputs in Variable() and got an error on the next line loss = criterion(outputs, labels) : AttributeError: 'torch.LongTensor' object has no attribute 'requires_grad' Any idea how to fix?

The first error indicates an older version. Could you check it with print(torch.__version__)?

1 Like

Ah weird. Conda says I have pytorch pytorch: 0.4.0-py36_cuda9.0.176_cudnn7.1.2_1 (just downloaded today), but although I’m using the Anaconda kernel in jupyter it prints out version 0.3.1.

Try to uninstall it using conda and pip.
Just call conda uninstall pytorch and pip uninstall torch and reinstall it afterwards.
Maybe you have different versions installed.

Even though I just dled pytorch I tried conda update pytorch and it showed pytorch: 0.4.0-py36_cuda9.0.176_cudnn7.1.2_1 pytorch [cuda90] --> 0.4.0-py36hdf912b8_0 and now it prints the correct version in jupyter. strange. Thanks for identifying the problem!