Reputation: 129
I have the following error in my training loop and I don't really understand what the issue is. I am currently in the process of writing this code so stuff isn't final but I cannot figure out what this problem is.
I have tried googling the error and read some of the answers but still couldn't seem to understand the crux of the issue.
Dataset and Dataloader (X and Y are already given to me, they are both [2000, 40, 1] tensors)
class TrainingDataset(data.Dataset):
def __init__(self, X, y):
self.X = X
self.y = y
def __len__(self):
return Nf
# returns corresponding input/output pairs
def __getitem__(self, t):
X = self.X[t]
y = self.y[t]
#print(X.shape, y.shape)
return X, y
# prints torch.Size([2000, 40, 1]) torch.Size([2000, 40, 1])
print(x.size(), y.size())
dataset = TrainingDataset(x,y)
batchSize = 20
dataIter = data.DataLoader(dataset, batchSize)
Model:
class Encoder(nn.Module):
def __init__(self, num_inputs = 40, num_outputs = 40):
super(Encoder, self).__init__()
self.num_inputs = num_inputs
self.num_hidden = num_hidden
self.num_outputs = num_outputs
self.layers = nn.Sequential(
nn.Linear(num_inputs, num_outputs),
nn.ReLU(),
nn.Linear(num_outputs, num_outputs),
nn.ReLU(),
nn.Linear(num_outputs, num_outputs)
)
def forward(self, x_c, y_c):
return self.layers(x_c, y_c)
Training Loop:
for epoch in range(epochs):
for batch in dataIter:
optimiser.zero_grad()
l = loss(encoder(x_c=batch[0], y_c=batch[1]), batch[1])
l.backward()
optimiser.step()
Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-aa1c60616d82> in <module>()
6 for batch in dataIter:
7 optimiser.zero_grad()
----> 8 l = loss(encoder(x_c=batch[0], y_c=batch[1]), batch[1])
9 l.backward()
10 optimiser.step()
2 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
--> 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
TypeError: forward() takes 2 positional arguments but 3 were given
Can anyone point me in the right direction? I have just started to learn and do pytorch so I am not good at any of this yet.
Upvotes: 4
Views: 18652
Reputation: 1
I had a similar issue and solved it with this:
class SequentialDecoder(nn.Sequential):
def forward(self, *inputs):
x, y = inputs
for module in self._modules.values():
x = module(x, y)
return x
Upvotes: 0
Reputation: 2271
def forward(self, x_c, y_c):
return self.layers(x_c, y_c)
Your error lies here, this function should have only 1 argument apart from self
.
Upvotes: 1