Unwrapping layers from a nested Sequential object with sub-Modules?

Supposing I have a complex model, built up of individual Sequentials with their own Modules. In such nested condition, I wish to extract a simple list of all individual layers with arbitrary depth. Entire network is sequential so the flow of tensors wouldn’t be interruped:

blocks = [*newmodel.children()]
interm = [*blocks[0].children()]
#.....And we go on, iterating modules appending layers to list - getting their children() etc.

something like this is progress but is too cumbersome for complex nets.

I don’t really mind using this, but if anyone has a more neater solution, I’m all years :slight_smile: :rocket:

1 Like

This is what Codex suggests:-

def get_leaf_layers(m):
    children = list(m.children())
    if not children:
        return [m]
    leaves = []
    for l in children:
    return leaves

I wonder if anyone can come up with a neater, more elegant solution :wink:

1 Like

You should use self.modules()