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
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’