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

Hi,
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!

5 Likes

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)
37 Likes

Ah that’s neat, thanks :slight_smile:

Great example here:

20 Likes

Thanks a lot! That’s really helpful!

Thanks, worked like a charm.