Reputation: 45
I searched StackOverflow and visited other websites for help, but I can´t find a solution to my problem. I will leave the whole code to make it understandable for you. It´s about 110 lines, written with PyTorch.
Each time, I will compile and calculate a prediction, this error-code will show up:
Traceback (most recent call last):
File "/Users/MacBookPro/Dropbox/01 GST h_da Privat/BA/06_KNN/PyTorchV1/BesucherV5.py", line 108, in <module>
result = Network(test_exp).data[0][0].item()
TypeError: __init__() takes 1 positional argument but 2 were given
I know, other users had this too, but none of their solutions helped me out. I guess the mistake is either in my class "Network" or in variable "result". I hope that someone of you had this problem and know how to fix it or can help me in a different way.
Short information about the Dataset:
My Dataset has 10 columns and gets splitted into two sets. X and Y. X has 9 columns, Y just one. These are then used to train the network.
Thank you in advance!
Kind regards Christian Richter
My Code:
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torch.optim as optim
import pandas as pd
### Dataset ###
dataset = pd.read_csv('./data/train_data_csv.csv')
x_temp = dataset.iloc[:, :-1].values
print(x_temp)
print()
print(x_temp.size)
print()
y_temp = dataset.iloc[:, 9:].values
print(y_temp)
print()
print(y_temp.size)
print()
x_train_tensor = torch.FloatTensor(x_temp)
y_train_tensor = torch.FloatTensor(y_temp)
### Network Architecture ###
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.linear1 = nn.Linear(9, 9) #10 Input-Neurons, 10 Output-Neurons, Linearer Layer
self.linear2 = nn.Linear(9, 1)
def forward(self, x):
pax_predict = F.relu(self.linear1(x))
pax_predict = self.linear2(x)
return pax_predict
def num_flat_features(self, pax_predict):
size = pax_predict.size()[1:]
num = 1
for i in size:
num *= i
return num
network = Network()
print(network)
criterion = nn.MSELoss()
target = Variable(y_train_tensor)
optimizer = torch.optim.SGD(network.parameters(), lr=0.0001)
### Training
for epoch in range(200):
input = Variable(x_train_tensor)
y_pred = network(input)
loss = criterion(y_pred, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
test_exp = torch.Tensor([[40116]])
result = Network(test_exp).data[0][0].item()
print('Result is: ', result)
Upvotes: 1
Views: 15999
Reputation: 63
I was getting the same error, and then I realized that I missed parenthesis when passing ToTensor() transform.
Wrong:
trainData = ImageFolder('data/in/train/', transform=ToTensor)
Right:
trainData = ImageFolder('data/in/train/', transform=ToTensor())
In general, if a function name is used but then the prototype is not as expected, it is likely to face such errors.
Upvotes: 4
Reputation: 13601
The problem is quite simple and is in this line, I suppose:
result = Network(test_exp).data[0][0].item()
Here you should use network
(the object) instead of Network
(the class). As you defined, Network
takes only 1 argument (i.e., self
), but you are passing 2: self
and test_exp
.
Perhaps if you had chosen another name for your object (e.g., net
), you'd have spotted this error more easily. Take that into consideration :)
And please, always post the full traceback.
Upvotes: 4