Module.children() vs Module.modules()

If you want to recursively iterate over modules, then you want to use .modules()
For example, in the following network

m = nn.Sequential(nn.Linear(2,2), 
                  nn.ReLU(),
                  nn.Sequential(nn.Sigmoid(), nn.ReLU()))

calling m.children() will return

[Linear (2 -> 2), ReLU (), Sequential (
   (0): Sigmoid ()
   (1): ReLU ()
 )]

which means that it does not go inside the second Sequential, and thus does not print individually Sigmoid. On the other hand, m.modules() recursively walks into all modules in the network, and yields

[Sequential (
   (0): Linear (2 -> 2)
   (1): ReLU ()
   (2): Sequential (
     (0): Sigmoid ()
     (1): ReLU ()
   )
 ), Linear (2 -> 2), ReLU (), Sequential (
   (0): Sigmoid ()
   (1): ReLU ()
 ), Sigmoid (), ReLU ()]
76 Likes