Concatenate two Sequential blocks?

If I have two nn.Sequential blocks how can I make a new nn.Sequential block that is the concatenation of both of them?

block1 = nn.Sequential(stuff)
block2 = nn.Sequential(other_stuff)

block3 = nn.Sequential (block1,block2) ?? <- like this?

Assume that block1 can feed directly into block2.

1 Like

Haven’t tried this, but should work.

list_of_layers = list(block1.children())
list_of_layers.extend(list(block2.children()))
block3 = nn.Sequential (*list_of_layers)

Hope it helps!

4 Likes

Since nn.Sequential is a subclass of nn.Module, you can concatenate multiple instances of nn.Sequential as you described.

@allenye0119 Thanks!

That worked, thank you!

1 Like

I don’t understand. Can you put some example code ?

Hi @ThaiThien,

Lets consider this model from https://github.com/leeyeehoo/CSRNet-pytorch

which has a forward function as following

def forward(self,x):
        x = self.frontend(x)
        x = self.backend(x)
        x = self.output_layer(x)
        return x

You can concatenate these three blocks (last one is a single layer for output) as below:

model_cat = torch.nn.Sequential(*model.frontend.children(),
                                *model.backend.children(),
                                model.output_layer)
1 Like