Accessing operations in `forward()`

Let’s say I have the following model

class Model(nn.Module):

    def __init__(self):
        super().__init__()

        self.conv = torch.nn.Conv2d(in_channels=3, out_channels=4, kernel_size=(3, 3))

    def forward(self, x):
        x = self.conv(x)
        x = torch.flatten(x, start_dim=1)
        x = torch.sigmoid(x)
        return x

Now, if I use

for name, module in model.named_children():
    print(type(module))

only the convolutional operations shows up:

<class 'torch.nn.modules.conv.Conv2d'>

I would like to know, why the flatten() and sigmoid() do not show up?

If I, for example, want to replace in an existing model the flatten() or sigmoid() operation, how do I do that in this case? How can I modify the forward pass?

The functional operations used in the forward method are not registered as modules and thus won’t show up in model.modules() or model.children().

You can directly modify the forward method and replace these operations.
In case you want to manipulate the forward of another pre-defined model, you could derive a custom model from it and implement your forward there.