How to convert a ModuleList of Sequential to an entire Sequential

What I want to do is like this, for example:
I have each layer = nn.Sequential(GRU(), LayerNorm()), and totally 4 layers.
And finally a classifier which can also be a Sequential(Linear(), Softmax())

I know that I can put these totally 5 Sequentials into an nn.ModuleList(), and use a for loop in the forward() to make the input pass through all the layers. But I want to ask if there is some function that can directly convert a ModuleList that contains multiple Sequentials to an entire Sequential.

More generally it may look like this:
func(Seq(A,B), Seq(C,D), Seq(A,E)) → Seq(A, B, C, D, A, E)

You can just use

res = nn.Sequential(
seq_ab,
seq_cd,
seq_ae
)

. It works as what you want.

Thank you for the reply. But when I use the way you suggest, it gives me
Seqential(
Seqential(A,B),
Seqential(C,D),
Seqential(A,E)
)
instead of Seqential(A,B,C,D,A,E) that I favor.

You can use * to unpack a Sequential object. Regarding what you asked for, something along these lines should work:

all_layers = [*Seq(A,B), *Seq(C,D), *Seq(A,E)]
new_sequential = nn.Sequential(*all_layers)
1 Like

Thanks, worked for me.