1

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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
ilovewt
  • 911
  • 2
  • 10
  • 18

1 Answers1

1

setattr(net, "head.fc", torch.nn.Linear(1111, 2))

will create head.fc attribute on net.

I guess you want to create
setattr(net.head, "fc", torch.nn.Linear(1111, 2))

or dynamically:

setattr(getattr(net, "head"), "fc", 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.

Lior Cohen
  • 5,570
  • 2
  • 14
  • 30