Hi Malte!



 Malte5768:

This is my Error:
Traceback (most recent call last):
…
File “G:/Erstes NN(6.11.19).py”, line 43, in 
out = netz(input)
…
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’


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