I am currently trying to assign an attribute, consider the following example:
net = timm.create_model(model_name='regnetx_002', pretrained=False)
net.head.fc = torch.nn.Linear(1111, 2)
This seems to work just fine, however, some models have different attributes, like the following
net.classifier = torch.nn.Linear(1111,2)
Therefore, I am trying to set attribute dynamically. I have already manage to retrieve the names of the attributes, like "head" and "fc", but it seems that you cannot do something like:
setattr(net, "head.fc", torch.nn.Linear(1111, 2))
as it will create one new attribute "head.fc" instead. I think there is a misunderstanding in my knowledge, as I noticed that when I print the model's last layer, it looks like:
(head): ClassifierHead(
(global_pool): SelectAdaptivePool2d (pool_type=avg, flatten=True)
(fc): Linear(in_features=368, out_features=1000, bias=True)
)
)
Is there any way to achieve what I want dynamically.