Why can't I do so?

May be my question is naive and it has an answer in the python documentation. But so
I am trting to build such a network structure

N = 5
class Net(nn.Module):
    def __init__(self, input_size):
        super(Net, self).__init__()
        self.input_size = input_size
        self.in_out_size = [input_size + N - i for i in range(input_size)]
        self.line = [nn.Linear(self.in_out_size[i], self.in_out_size[i+1]) for i in range(input_size-1)]

 def forward(self, x):
        for i in range(self.input_size):
            x = self.line[i](x)
        return x

But I can’t do it because I have empty the parameters list.

if __name__ == '__main__':
    model = Net(INPUT_SIZE)

Can I use a cycles of kind self.line = [nn.Linear/nn.GRU/nn.CNN for i in range()] for bilding of network?

It is possible but you’ll need to changes two lines of code:

self.line = nn.ModuleList([nn.Linear(self.in_out_size[i], self.in_out_size[i+1]) for i in range(input_size-1)])

You need to wrap the list in a ModuleList wrapper. You can also apply other types of list operations after you’ve done so, such as self.line.append(...).

And in your forward pass, should match the same range as in the ModuleList, or you’ll get an error at run time:

for i in range(self.input_size - 1):
            x = self.line[i](x)

Thank you! It really works!