I don't know why it is throwing an error? (Beginner)

问题描述 投票:0回答:1
import torch.nn as nn
import torch.nn.functional as F

## TODO: Define the NN architecture
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # linear layer (784 -> 1 hidden node)
        self.fc1 = nn.Linear(28 * 28, 512)
        self.fc2 = nn.Linear(512 * 512)
        self.fc3 = nn.Linear(512 * 10)

    def forward(self, x):
        # flatten image input
        x = x.view(-1, 28 * 28)
        # add hidden layer, with relu activation function
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        return x

# initialize the NN
model = Net()
print(model)

When I run this, it throws this error. Why?

TypeError: __ init __() missing 1 required positional argument: 'out_features'

pytorch mnist cnn
1个回答
3
投票

This error is because you have not provided the output size of the fully connected layer in your fc2 and fc3.Below is the modified code. I added the output size, I am not sure if this is the output size architecture you want. But for the demonstration, I put the output size. Please edit the code and add the output size as per your requirement.

Remember that the output size of the previous fully connected layer should be the input size of the next FC layer. Else it will throw size mismatch error.

© www.soinside.com 2019 - 2024. All rights reserved.