full model 보다 state_dict — 매번
model 저장 두 방법:
- state_dict (추천) — Python OrderedDict 로 parameter + buffer 만. 로드 위해 model 클래스 instantiate 하고
load_state_dict. code 변경, framework version 차이, 클래스 rename 에 robust. - torch.save(model) — 전체 Python 객체 pickle. fragile: 정확한 클래스 정의 필요, rename / refactor 깨뜨림.
항상 state_dict 사용.
'training' checkpoint 에 들어가는 것
inference 만이면 model state_dict 만. training 재개 위해선 필요:
- Model state_dict
- Optimizer state_dict (Adam 의 running moment 가 trivial 하게 재생성 안 됨)
- Scheduler state_dict (현재 step / last_lr / 등)
- 현재 epoch / step 번호
- 지금까지 best validation metric (early stopping 위)
- 정확 reproducibility 신경 쓰면 RNG state
weights_only=True
로드 시 weights_only=True 넘기기. PyTorch 2.x 가 결국 default 로 할 거지만, 명시적이 좋음. 임의 Python deserialize 거부 — tensor data 만. 악성 .pt 파일이 로드 시 코드 실행하는 (실재) 공격 클래스 방어.
Early stopping
val loss 개선 시 'best so far' checkpoint 저장; patience 추적, N epoch 개선 없이 지나면 중단. 다섯 줄 클래스 state, 낭비된 compute 방지.