Iterating all layers in network

Is there any way to recursively iterate over all layers in a nn.Module instance including sublayers in nn.Sequential module

I’ve tried .modules() and .children(), both of them seem not be able to unfold nn.Sequential module. It requires me to write some recursive function call to achieve this. I wonder is there any elegant way to do it.

It seems to work for me:

class MyModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.seq1 = nn.Sequential(
            nn.Sequential(
                nn.Linear(1, 2),
                nn.ReLU(),
                nn.Linear(2, 3)
            ),
            nn.ReLU(),
            nn.Linear(3, 4)
        )
        self.out = nn.Linear(4, 5)
        
model = MyModel()
for name, module in model.named_modules():
    print(name, type(module))

#  <class '__main__.MyModel'>
# seq1 <class 'torch.nn.modules.container.Sequential'>
# seq1.0 <class 'torch.nn.modules.container.Sequential'>
# seq1.0.0 <class 'torch.nn.modules.linear.Linear'>
# seq1.0.1 <class 'torch.nn.modules.activation.ReLU'>
# seq1.0.2 <class 'torch.nn.modules.linear.Linear'>
# seq1.1 <class 'torch.nn.modules.activation.ReLU'>
# seq1.2 <class 'torch.nn.modules.linear.Linear'>
# out <class 'torch.nn.modules.linear.Linear'>