'function' object has no attribute `dim

So i just started learning programming and i wanted to try out a really simple NN which i then made with help of a YT video.Which means that this code is a 100 percent copied… for what ever reason i get that error message and i dont know how to fix it :confused:

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

class MeinNetz(nn.Module):

def __init__(self):              
    super(MeinNetz, self).__init__()
    self.lin1 = nn.Linear(10, 10)
    self.lin2 = nn.Linear(10, 10)

def forward(self, x):
    x = F.relu(self.lin1(x)) 
    x = self.lin2(x)
    return x

def num_flat_features(self,x):
    size = x.size()[1:]          
    num = 1
    for i in size:
        num *= i
        return num

netz = MeinNetz()

for i in range (100):
x = [1,0,0,0,1,0,0,0,1,1]
target = Variable(torch.Tensor([x for _ in range(10)]))

out = netz(input)

x= [0,1,1,1,0,1,1,1,0,0]
target = Variable(torch.Tensor([x for _ in range(10)]))
criterion = nn.MSELoss()  
loss = criterion(out,target)
print(loss)

netz.zero_grad()
loss.backward()  
optimizer = optim.SGD(netz.parameters(), lr=0.01)
optimizer.step()

This is my Error:

Traceback (most recent call last):

File “”, line 1, in
runfile(‘G:/Erstes NN(6.11.19).py’, wdir=‘G:’)

File “E:\Programmieren\Anaconda\lib\site-packages\spyder_kernels\customize\spydercustomize.py”, line 827, in runfile
execfile(filename, namespace)

File “E:\Programmieren\Anaconda\lib\site-packages\spyder_kernels\customize\spydercustomize.py”, line 110, in execfile
exec(compile(f.read(), filename, ‘exec’), namespace)

File “G:/Erstes NN(6.11.19).py”, line 43, in
out = netz(input)

File “E:\Programmieren\Anaconda\lib\site-packages\torch\nn\modules\module.py”, line 489, in call
result = self.forward(*input, **kwargs)

File “G:/Erstes NN(6.11.19).py”, line 24, in forward
x = F.relu(self.lin1(x)) #letzes mus hier immer ein return sein#

File “E:\Programmieren\Anaconda\lib\site-packages\torch\nn\modules\module.py”, line 489, in call
result = self.forward(*input, **kwargs)

File “E:\Programmieren\Anaconda\lib\site-packages\torch\nn\modules\linear.py”, line 67, in forward
return F.linear(input, self.weight, self.bias)

File “E:\Programmieren\Anaconda\lib\site-packages\torch\nn\functional.py”, line 1350, in linear
if input.dim() == 2 and bias is not None:

AttributeError: ‘function’ object has no attribute ‘dim’

Hi Malte!

Your problem is that (in your code) input is not defined – or, more
precisely, it is defined by python to be a function. (This kind of thing is
a common source of errors in weakly-type languages such as python.)

Python’s input function doesn’t have a dim attribute, hence the
error.

Here are some results from a python session that illustrate this:

>>> type (input)
<class 'builtin_function_or_method'>
>>> input.dim()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'dim'
>>> import torch
>>> input = torch.randn (3, 4)
>>> type (input)
<class 'torch.FloatTensor'>
>>> input.dim()
2

When the python session first starts, input exists and is defined by
python to be a ‘builtin_function_or_method’.

Then I import torch and assign a tensor to input. (Python is very
permissive – you are allowed to assign things to variable names that
are used for python builtins.) Now input is a ‘torch.FloatTensor’, and
you can happily call its dim() method.

Note, input in:

File “G:/Erstes NN(6.11.19).py”, line 43, in
out = netz(input)

and in:

File “E:\Programmieren\Anaconda\lib\site-packages\torch\nn\functional.py”, line 1350, in linear
if input.dim() == 2 and bias is not None:

are not, in general, the same thing. They just happen to be labelled
with the same character string.

In the first case, input is whatever it is when you call netz(input),
namely the python builtin. In the second case it is the name of an
argument to a function inside of functional.py. In your situation, it
is the same as the first input because it was passed as an
argument through a series of functions until it gets to that line of
code in functional.py that actually tries to do something with it, at
which point python discovers that it isn’t what you thought it was.

(In pytorch, “input” is often used as the name of the tensor that
is passed to a model. You could argue that pytorch shouldn’t be
using names of python builtins for these sorts of things, but that
ship has long since sailed.)

How to fix it? You need to pass some kind of sensible data to your
model. If you copied your code from a tutorial, either the tutorial
is buggy, or you missed copying the part of the code where “input”
is defined to be something meaningful for training the model.

Good luck.

K. Frank

Thank you very much mate :slight_smile: im gonna try that out tomorrow :smiley:

Did you get this right? I am trying to use the forward method of pytorch after creating a fastai model, and it throws the same error.

Could you check which operation throws this error as shown in the stacktrace and make sure you are not hitting the same issue as described by @KFrank?

Found solution: