NN Tutorial: Argument 0 is not a Variable

I just started with PyTorch and tried working my way through the tutorials.
When I run the standard neural_networks_tutorial.py I get the following error:

Traceback (most recent call last):
  File "src/pytorch_tests/neural_networks_tutorial.py", line 98, in <module>
    out = net(input)
  File "/home/ssudholt/checkouts/pytorch-tests/env/local/lib/python2.7/site-packages/torch/nn/modules/module.py", line 206, in __call__
    result = self.forward(*input, **kwargs)
  File "src/pytorch_tests/neural_networks_tutorial.py", line 61, in forward
    x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
  File "/home/ssudholt/checkouts/pytorch-tests/env/local/lib/python2.7/site-packages/torch/nn/modules/module.py", line 206, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/ssudholt/checkouts/pytorch-tests/env/local/lib/python2.7/site-packages/torch/nn/modules/conv.py", line 237, in forward
    self.padding, self.dilation, self.groups)
  File "/home/ssudholt/checkouts/pytorch-tests/env/local/lib/python2.7/site-packages/torch/nn/functional.py", line 40, in conv2d
    return f(input, weight, bias)
TypeError: argument 0 is not a Variable

I ran the tutorial without any modifications:

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


class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        # 1 input image channel, 6 output channels, 5x5 square convolution
        # kernel
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)
        # an affine operation: y = Wx + b
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        # Max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # If the size is a square you can only specify a single number
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(-1, self.num_flat_features(x))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features


net = Net()
net
print(net)

input = Variable(torch.randn(1, 1, 32, 32))
out = net(input)
print(out)

PyTorch was installed in a clean virtualenv through pip (version 0.1.12_2).
Google did non know anything about this error so I was hoping that maybe someone here can point me in the right direction.

[bytstander comemnt] Thats pretty weird. Whats ht output of print(input), just before the out = net(input) line?

what happens if you insert a new line, just before current line 40, in /home/ssudholt/checkouts/pytorch-tests/env/local/lib/python2.7/site-packages/torch/nn/functional.py, that says print(input), and rerun?

Thanks for the quick reply.
The output in both cases is as I would expect it:

Variable containing:
(0 ,0 ,.,.) = 
 -0.0318 -0.3270  1.0696  ...   1.0775 -0.6827 -0.2995
  2.4808  0.6244  1.3501  ...  -0.4092 -0.5541  1.2641
 -1.5480  0.8170 -0.2698  ...  -0.1328 -0.0383  0.3028
           ...             ⋱             ...          
 -1.7280 -1.4230  0.8473  ...  -1.1345 -1.4235 -0.5488
  0.2920  1.7177  0.0864  ...  -0.2760  2.3767  0.3261
  0.3887  1.6059  0.5631  ...  -0.7719  0.8481  0.0843
[torch.FloatTensor of size 1x1x32x32]

The code works okay on my machine. you may need to update your pytorch.

Is there a newer version on pip than 0.1.12_2? I was under the impression that this is the latest

Right now, 0.1.12_2 is the latest version.

Python 3.6.0 |Anaconda custom (x86_64)| (default, Dec 23 2016, 13:19:00) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.__version__
'0.1.12_2'

$ python neural_networks_tutorial.py gives no error with this setup.

So weird.

So, the error message is a result of the following evaluating to false, in the C code, I think:

inline bool THPVariable_Check(PyObject *obj)
{
  return THPVariableClass && PyObject_IsInstance(obj, THPVariableClass);
}

(python_variable.h)

So, seems like it’s not checking some attribute, but looking at the actual class.

The type of the class is being obtained, in the C code like:

  if (PyType_Ready(&THPVariableType) < 0)
    return false;
  Py_INCREF(&THPVariableType);
  PyModule_AddObject(module, "_VariableBase", (PyObject *)&THPVariableType);

(python_variable.cpp)

Then, that _VariableBase bit seems to be defined on the C side. from variable.py:

class Variable(_C._VariableBase):

… though I’m not sure where the breadcrumbs lead after this…

grep -ir '_VariableBase' torch
torch/autograd/variable.py:class Variable(_C._VariableBase):
torch/csrc/autograd/python_variable.cpp:  "torch._C._VariableBase",              /* tp_name */
torch/csrc/autograd/python_variable.cpp:  PyModule_AddObject(module, "_VariableBase", (PyObject *)&THPVariableType);

However, as others point out, I think reinstalling via conda would be a good place to start. conda provides binaries, unlike pip, which provides source code.

Thanks for all the help, I’ll follow your hints and get back later when I find the error

I also encounter this error. So the only way to solve it is update the torch version?

Yes, would recommend that you update your torch. Instructions on how to install the latest can be found here: http://pytorch.org/

Hi I am also facing the same issue with pytorch 0.3.0 and I am also feeding a FloatTensor to squezeNet, I have attached the source code to the message.

import torchvision
from torchvision import transforms
import torch 
import torch.nn as nn
from PIL import Image
import requests
import io

squeeze = torchvision.models.squeezenet1_1(pretrained=True)

IMG_URL = 'https://s3.amazonaws.com/outcome-blog/wp-content/uploads/2017/02/25192225/cat.jpg'
response = requests.get(IMG_URL)
img_pil = Image.open(io.BytesIO(response.content))


normalize = transforms.Normalize(
   mean=[0.485, 0.456, 0.406],
   std=[0.229, 0.224, 0.225]
)
preprocess = transforms.Compose([
   transforms.Resize(256),
   transforms.CenterCrop(224),
   transforms.ToTensor(),
   normalize
])
    
img_tensor = preprocess(img_pil)
data = img_tensor.unsqueeze(0)
squeeze(data)

You are feeding a Tensor to your model instead of a Variable
Warp data in a Variable and your code should run. :slight_smile:

from torch.autograd import Variable
squeeze(Variable(data))
8 Likes

I’m getting the same error on the CIFAR-10 tutorial with a fresh install of pytorch conda install pytorch torchvision cuda90 -c pytorch

In the current pytorch version (0.4.0) Variables and tensors are merged, so that shouldn’t be an error.
Could you check your version with:

print(torch.__version__)

Got the same error with the CIFAR-10 tutorial.
print(torch.version)
0.3.1.post2

You could update to 0.4.0, where Variables and tensors are merged or warp your tensor into a Variable or did the aforementioned approaches not work in your case?

1 Like

updated to 0.4.0 and it worked. thanks!