pytorch冻结模型部分层
Published on Aug. 22, 2023, 12:06 p.m.
<h1>冻结模型部分层</h1>
for name ,param in model.named_parameters():
param.requires_grad = False
for ele in unfreeze_layers:
if ele in name:
param.requires_grad = True
break
#过滤掉requires_grad = False的参数
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=0.00001)
最后在做训练的时候,优化器中一定要添加过滤器filter把requires_grad = False的参数过滤掉,在训练的时候,不会更新这些参数。