Dynamic module architecture using nn.Sequential

Hi everyone,

Is there a way to construct modules using nn.Sequential dynamically?

For example, I would like to implement a [[CONV -> RELU] * N -> POOL] * M -> [FC -> RELU] * K -> FC architecture where I can loop through different values of N, M, and K on the fly to see which architecture works best.


yes, feel free to do this in your python code. You dont need an nn.Sequential, you can construct this as a few for-loops in your forward function of an nn.Module subclass, for example you can insert a few for-loops here: https://github.com/pytorch/examples/blob/master/mnist/main.py#L62

1 Like

Great thanks, I’ll have a look through these examples!

Question: in this way, we are looping through the same layer, i.e. same weight, multiple times, right?

And what if I want the layers to be different. Can we do that in a loop?


yes you can do that as well

Could you kindly provide a sample code for generating different layers in a loop?