728x90
반응형
for name, param in model.named_parameters():
if [LAYER_NAME] in name:
param.requires_grad = True
else:
param.requires_grad = False
ex)
for name, param in model.named_parameters():
if 'rec_head' in name:
param.requires_grad = True
else:
param.requires_grad = False
Pytorch Model 일부 Layer만 Freeze 하기
task-specific한 Model training을 할 때, 기존의 pretrained model weight를 가져와서 하는 경우가 많이 있다. 여러 논문들에서도 BERT와 같이 pretrained된 대형 모델에서, layer 몇 개만 추가해주면 어떤 NLP task던 fr
soyoung97.github.io
optimizer에서 grad 에러 뜰 경우,
optimizer = torch.optim.Adam([param for param in model.parameters() if param.requires_grad == True], lr=cfg.train_cfg.lr)
https://gist.github.com/L0SG/2f6d81e4ad119c4f798ab81fa8d62d3f
PyTorch example: freezing a part of the net (including fine-tuning)
PyTorch example: freezing a part of the net (including fine-tuning) - freeze_example.py
gist.github.com
https://github.com/pytorch/pytorch/issues/679
728x90
반응형