How do i add an layer to an Nural network in pytorch
i want to add a layer to a Nural network programicaly it returned this error TypeError: forward() missing 1 required positional argument: 'x'


class Net(nn.Module):


    def __init__(self):
        super(Net, self).__init__()

        self.fc1 = nn.Linear(1, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  
        num_features = 1
        for s in size:
            num_features *= s
        return num_features


netz =Net()
print(netz)



netz = nn.Sequential([nn.Linear(10, 120), netz()])



print(netz)
`


The same error happend when i was loading it with netz=torch.load()

the line which seems to cause the error is :netz = nn.Sequential([nn.Linear(10, 120), netz()])

how do i make it work?

Ok so there are several things.

Beginning with why are you calling netz(), you already instiantiated the object earlier with netz =Net(), so this make no sense. 

Second thing, nn.Sequential expects *args as "constructor" argument, so you directly pass subclasses of modules: netz = nn.Sequential(Net(), nn.Linear(100,100)) or you unpack them: nn.Sequential(*[nn.Linear(100,100), Net()]).

You can also add multiple modules using an OrderedDict as is well documented in the PyTorch docs (which you should consult by the way - it's there for a reason!) 

model = nn.Sequential(OrderedDict([
          ('conv1', nn.Conv2d(1,20,5)),
          ('relu1', nn.ReLU()),
          ('conv2', nn.Conv2d(20,64,5)),
          ('relu2', nn.ReLU())
        ]))


You can also add a module with my_modules.add_module("my_module_name", Net()) to an existing collection of ordered modules.
