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.
Haven’t tried this, but should work.
list_of_layers = list(block1.children())
block3 = nn.Sequential (*list_of_layers)
Hope it helps!
nn.Sequential is a subclass of
nn.Module, you can concatenate multiple instances of
nn.Sequential as you described.
I don’t understand. Can you put some example code ?
Lets consider this model from https://github.com/leeyeehoo/CSRNet-pytorch
which has a forward function as following
x = self.frontend(x)
x = self.backend(x)
x = self.output_layer(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(),