I’m working through some Udacity courses on PyTorch and decided to go the extra mile to extend the
nn.Sequential class. I wanted to automate defining each layer’s activations by just passing a tuple containing the number of nodes in each class.
So normally if I wanted to perform a forward pass with an already initialized
nn.Sequential model, I’d simply use
out = model(x) # OR out = model.forward(x)
Now that I’ve extended the class, I am trying to use
out = self(x) # OR out = self.forward(x)
and am getting the following error:
TypeError: forward() missing 1 required positional argument: 'target'
I’ve done nothing to alter the forward method at all, so I’m quite confused. I’d appreciate any help. Thank you!
The full code for my class is below:
class Network(nn.Sequential): def __init__(self, layers): super().__init__(self.init_modules(layers)) self.criterion = nn.NLLLoss() self.optimizer = optim.Adam(self.parameters(), lr=0.003) def train(self, trainloader, epochs): for e in range(epochs): for x, y in trainloader: x = x.view(x.shape, -1) self.optimizer.zero_grad() loss = self.criterion(self(x), y) loss.backward() self.optimizer.step() def init_modules(self, layers): # Logic unimportant to the question (I think)