티스토리 뷰

PyTorch에서 제공하는 AutoMixedPrecision 기능을 활용하여 모델을 학습하다 보면 학습이 잘 안되는 경우가 발생한다. 구글링을 해보면 이런 경우를 심심치 않게 찾아볼 수 있다.

 

https://github.com/pytorch/pytorch/issues/40497

 

Mixed precision causes NaN loss · Issue #40497 · pytorch/pytorch

🐛 Bug I'm using autocast with GradScaler to train on mixed precision. For small dataset, it works fine. But when I trained on bigger dataset, after few epochs (3-4), the loss turns to nan. It i...

github.com

 

이러한 오류는 대개 float16 자료형으로 변환하면서 값의 표현범위가 변하면서 발생하는 경우가 흔한 것으로 보인다.

 

torch.finfo(torch.float16) 

 

를 출력해보면 torch.float16 자료형의 값의 표현범위를 확인할 수 있다.

 

finfo(resolution=0.001, min=-65504, max=65504, eps=0.000976562, tiny=6.10352e-05, dtype=float16)

 

위 값의 범위를 생각하여 epsilon 값, min, max값에 따라 float32로 upcast 해줘야할 부분은 upcast를 해줘야하고 epsilon값도 키워주는등의 작업이 필요하다

'Deep Learning > PyTorch' 카테고리의 다른 글

PyTorch Contribution!  (0) 2022.08.11
YOLOv5 Contribution! Weight Decay  (2) 2022.07.17
[PyTorch] Batchnorm running_var 계산시 주의할 점  (0) 2022.03.23
[PyTorch] Batchnorm Implementation  (0) 2022.03.21
[PyTorch] BN Layer Freeze  (0) 2022.03.19
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함