Sure, let's take a simple example. Let's say we have a model with a single weight `w`, a learning rate `lr` of 0.1, and we're using Mean Squared Error (MSE) loss for a regression problem.
1. **Forward Pass**: Let's say our model predicts a value of 10 (`y_pred`) for an input, but the actual value (`y_true`) is 15. The MSE loss would be `(y_pred - y_true)^2 = (10 - 15)^2 = 25`.
2. **Backward Pass**: The gradient of the loss with respect to `w` would be the derivative of the loss with respect to `y_pred` times the derivative of `y_pred` with respect to `w`. Let's assume this comes out to be `0.01`. If we're using gradient scaling with a scale factor of 100, this gradient would be scaled up to `0.01 * 100 = 1` before the backward pass.
3. **Weight Update**: During the weight update step, we would first unscale the gradient back to its original value of `0.01`. Then we would update the weight using the formula `w_new = w_old - lr * gradient = w_old - 0.1 * 0.01`.
In the `scaler.step(optimizer)` call, the unscaled gradients are used to update the weights. The weights themselves are stored in float32 and can represent the small changes due to the gradient update.
4. **Scaler Update**: After the optimizer step, we call `scaler.update()`. This checks if any gradients overflowed to infinity during the backward pass. If they did, it reduces the scale factor for the next iteration to prevent this from happening again. If no gradients overflowed, it might slightly increase the scale factor
예, 간단한 예를 들어 보겠습니다. 단일 가중치 w, 학습률 lr이 0.1인 모델이 있으며 회귀 문제에 평균 제곱 오차(MSE) 손실을 사용한다고 가정합니다.
1. **Forward Pass**: 모델에서 입력에 대한 값을 10('y_pred')으로 예측하지만 실제 값('y_true')은 15라고 가정합니다. MSE 손실은 "(y_delay - y_true)^2 = (10 - 15)^2 = 25"입니다.
2. **역방향 패스**: "w"에 대한 손실의 기울기는 "y_pred"에 "y_pred"의 "w"에 대한 손실의 도함수가 될 것입니다. 이 값이 0.01이라고 가정합니다. 스케일 팩터가 100인 그라데이션 스케일링을 사용하는 경우 이 그라데이션은 역방향 패스 전에 "0.01 * 100 = 1"까지 스케일이 증가합니다.
3. **체중 업데이트**: 체중 업데이트 단계에서 먼저 그라데이션의 크기를 원래 값인 "0.01"로 다시 조정합니다. 그런 다음 'w_new = w_old - lr * gradient = w_old - 0.1 * 0.01' 공식을 사용하여 가중치를 업데이트합니다.
'scaler.step(optimizer)' 호출에서는 눈금이 없는 그라데이션이 가중치를 업데이트하는 데 사용됩니다. 가중치 자체는 float32에 저장되며 기울기 업데이트로 인한 작은 변화를 나타낼 수 있습니다.
4. **Scaler Update**: 옵티마이저 단계 후에 'scaler.update()delindo'를 호출합니다. 이는 후진 패스 중에 그라데이션이 무한대로 오버플로되었는지 확인합니다. 이 경우 다음 반복에 대한 확장 비율을 줄여 다시는 이런 일이 발생하지 않도록 합니다. 그라데이션이 오버플로우되지 않으면 스케일 팩터가 약간 증가할 수 있습니다
'버츄얼유튜버' 카테고리의 다른 글
[확통] 2 (0) | 2023.06.12 |
---|---|
[Detection] 드론 뷰 오브젝트 디텍션 (0) | 2023.06.07 |
CLTR 분석 (0) | 2023.02.22 |
마르코프 체인 / 몬테카를로 예측 / 선형 칼만 필터 (0) | 2023.02.19 |
NVIDIA-SMI 및 기타 환경 구축 (0) | 2022.12.06 |