Is there a way to set attribute of attribute?
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 &quot;head&quot; and &quot;fc&quot;, but it seems that you cannot do something like:
setattr(net, &quot;head.fc&quot;, torch.nn.Linear(1111, 2)) 

as it will create one new attribute &quot;head.fc&quot; 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.

setattr(net, &quot;head.fc&quot;, torch.nn.Linear(1111, 2))
will create head.fc attribute on net.
I guess you want to create
setattr(net.head, &quot;fc&quot;, torch.nn.Linear(1111, 2))
or dynamically:
setattr(getattr(net, &quot;head&quot;), &quot;fc&quot;, torch.nn.Linear(1111, 2)).
If you want an even more general nested dynamic atrribute, you'll add to build some helper functions for this, like described here.
