Accessing the intermediate layer of timm model in pytorch

I am currently working on a timm model where I want to access some specific layers of the model.

import torch
import timm

model=timm.create_model('timm/vit_base_patch16_224', pretrained=True)

for i in range(6):
    strn='blocks.'+str([i])+'.mlp'
    container=model.strn

This code is showing error. Although coddes like the following works

model.blocks[7].mlp

It also looks like the model is not subscriptable i.e. approaches like model[8]
will not work. Is there any way to access the modules of a model using automated approach?

You could use getattr and pass the attribute name to it as I assume model.strn fails as it’s not a valid expression.

1 Like

Thanks a lot for your reply. The following works,

container=getattr(getattr(self.model,'blocks')[i],'mlp')

But I cannot repace the modules with custom modules. Can you suggest any solution to the following problem?

getattr(getattr(self.model,'blocks')[i],'mlp')=nn.Linear(10,20)

I am getting following error,

SyntaxError: cannot assign to function call

Try setattr after accessing the layer via getattr.

1 Like