How to automate layers addition to neural network models in pytorch


I am dealing with a model in pytorch and I want to automate the layers and activations addition to the model. This code is my simple model:

import torch
from torch import nn
import torch.nn.functional as F
class NeuralNetwork(nn.Module):
    def __init__(self, n_inputs, n_hidden_unit, n_output):
        l1 = nn.Linear(n_inputs, n_hidden_unit)
        a1 = nn.Sigmoid()
        l2 = nn.Linear(n_hidden_unit, n_output)
        l = [l1, a1, l2]
        self.module_list = nn.ModuleList(l)

    def forward(self, x):
        for f in self.module_list:
            x = f(x)
        return x

model = NeuralNetwork(n_inputs=10, n_hidden_unit=30, n_output=2)

As you see two layers and one activation is added manually but i want to for example have two lists or numpy arrays of them and then call the lists into my model. Lists will look like the following:

connections = [(10, 30), (30, 2)]
activation = [nn.Sigmoid()]

A similar thing I did using Sequential model:

layers = []
layers.append(nn.Linear(10, 30))
layers.append(nn.Linear(30, 2))

model = nn.Sequential(*layers)
Asked By: Ali_d



You can just use a loop:

def __init__(self, connections, activation):
    l = []
    for layer_idx, (n_input, n_output) in enumerate(connections):
        l.append(nn.Linear(n_input, n_output))
        if layer_idx < len(activation):
    self.module_list = nn.ModuleList(l)
Answered By: GoodDeeds