프로그래밍/에러 처리
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
상어군
2022. 12. 6. 17:26
반응형
Pytorch Lightning에서 model의 모든 layer를 freeze하는 테스트를 진행했다.
이때 freeze 사용된 코드는 아래와 같다.
for params in model.parameters():
params.requires_grad = False
그러나 위의 코드를 실행했을 경우
Trainer.fit 부분에서 아래의 에러가 발생했다.
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
이리저리 살펴본 결과
결국 pytorch lightning에서 모든 레이어가 params.requires_grad = False일 경우 evaluate 모드와 다름이 없어 에러가 발생하는 것으로 보인다.
이후 일부의 레이어를 params.requires_grad = True로 변경하니 정상적으로 학습이 진행됨을 확인했다.
for name,child in model.named_children():
if name == 'model':
for name2, param in child.named_parameters():
if name2[:2] != 'fc':
param.requires_grad = False
좀더 깔끔하게 fc 레이어만 빼고 params.requires_grad = False를 먹이는 방법이 있을 것 같은데,
나중에 찾게되면 업로드하겠다.
반응형