Append() for nn.Sequential or directly converting nn.ModuleList to nn.Sequential

maybe I’m missing sth obvious but there does not seem to be an “append()” method for nn.Sequential, cos it would be handy when the layers of the sequential could not be added at once.
Or it would be equivalent if I first added all the layer I need into a ModuleList then there’s a method for directly converting all the modules in a ModuleList to a Sequential.
Thanks in advance!


You can first construct a python list of nn.Modules and unpack it into a nn.Sequential

import torch.nn as nn

modules = []
modules.append(nn.Linear(10, 10))
modules.append(nn.Linear(10, 10))

sequential = nn.Sequential(*modules)

Ah that’s neat, thanks :slight_smile:

Great example here:


Thanks a lot! That’s really helpful!

Thanks, worked like a charm.

I’ve had a similar issue and this discussion was very helpful.
I do have another question - I would like to:

  • extract a copy of the current state of the tensor to a “skip connection” list in a U-shaped cnn.
  • adapt the tensor’s shape (that is the input) as I go (can not determine this ahead because I can’t know the number of convolutions ahead)

how can I do this while I’m adding modules to the list? I would like to make the list as modular as I can and can not skip the “append” stage in a for loop.

Thank you!