I was actually trying to finetune a resnet18
. I wanted to set the gradients of all layers to zero except the last linear layer! the isinstance
simply doesn’t work!
I wrote :
resnet18 = models.resnet18(pretrained=True)
resnet18.fc = nn.Linear(512, 10)
for module in resnet18.modules():
if not isinstance(module, nn.Linear):
for param in module.parameters():
param.requires_grad = False
What seems to work is to compare the names which is :
for module in resnet18.modules():
if module._get_name() != 'Linear':
print('layer: ',module._get_name())
for param in module.parameters():
param.requires_grad_(False)
elif module._get_name() == 'Linear':
for param in module.parameters():
param.requires_grad_(True)
something like nn.Linear.__class__.__name__
doesnt have the proper name! it contains type
as the name which is weird!
Thats why I’m asking.
on a side note :
Something weird happens, if I omit the second elseif block, the resnet18.fc parameters gradient will be False! while they are initially True, and the first if clause clearly checks for all layers except ‘Linear’.
I’d like to know why this is happening as well, and if this is a bug!?