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:
modules.append(model)
return
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.Module
s 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.Module
s? While pooling and dropout can be done using nn.Module
s, the view
operation can not (afaik).
Additionally, my general approach seems flawed, as the modules
list contains the Module
s in the order of initialization, not usage in the forward pass. Is there maybe a different approach to solving this problem?