Convert Model to Sequential

I have a model which has some submodules, which again have normal layers as submodules and the torch.nn.functional API.
To use a library which iterates over the model, I have to convert it into a Sequential model or make it iterable another way.

To do this, I wrote this function:

def model_to_list(model, modules):
    if len(list(model.children())) == 0:
    for module in model.children():
        model_to_list(module, modules)

modules = []
model_to_list(model, modules)
model = nn.Sequential(*modules)

This includes all all nn.Modules but not functional parts of the model. Is it possible to make a model iterable including the functional parts, or do I have to replace them in my model by nn.Modules? While pooling and dropout can be done using nn.Modules, the view operation can not (afaik).

Additionally, my general approach seems flawed, as the modules list contains the Modules in the order of initialization, not usage in the forward pass. Is there maybe a different approach to solving this problem?

you have to replace them. for things like view, you can use a custom module.

it’s also worth noting that .children() might not give you the correct execution order.

If .children() is not the correct way to get the execution order, then what is the correct way to get it ? @SimonW