Deep Learning/tensorflow

[Deep Learning] learning rate decay, learning rate scheduler

heedy 2022. 11. 16. 11:26
728x90

Learning Rate Decay

기존의 learning rate가 높은 경우 loss 값을 빠르게 내릴 수는 있지만, 오차가 0인 지점을 벗어날 수 있고, 
낮은 경우는 최적의 학습이 가능하지만 너무 오랜 시간이 걸리게 됩니다.

따라서 정적인 learning rate로 학습을 하는 것이 아닌,
epoch마다 동적으로 learning rate를 변화시켜 최적의 학습을 하도록 합니다.


tf.keras.optimizers.schedules.CosineDecay

tf.keras.optimizers.schedules.CosineDecay(
    initial_learning_rate, decay_steps, alpha=0.0, name=None
)

 

  • initial_learning_rate: 초기 lr
  • decay_steps: lr을 조절할 step 설정
  • alpha: 최종 lr(최소한으로 감소시킬 minimum lr)

decay learning rate 수식

def decayed_learning_rate(step):
  step = min(step, decay_steps)
  cosine_decay = 0.5 * (1 + cos(pi * step / decay_steps))
  decayed = (1 - alpha) * cosine_decay + alpha
  return initial_learning_rate * decayed

Example usage

decay_steps = 1000
lr_decayed_fn = tf.keras.optimizers.schedules.CosineDecay(
    initial_learning_rate, decay_steps)

Cosine Decay?

수식에서 나오는 각 변수들 :

  •  = learning_rate에 초기 값
  • T : 전체 Train epoch의 수
  • t : 현재 epoch

위의 총 3가지의 parameter에서 t는 epoch가 반복함에 따라 값이 자동으로 증가되니 사용자는 와 T 이 2가지 요소에 대해서만 learning rate를 생각하면 됩니다.

위의 Cosine Decay의 loss 그래프를 보면 위의 Step Decay와는 달리 안정적으로 끊김없이 loss가 감소하는 것을 볼 수 있습니다.

그리고 cosine decay 수식 에서  이 2개의 요소만 사용자가 세팅을 하면 되어서 Step Decay 보다 Hyper Parameter에 대해 덜 생각할 수 있게 되었습니다.

Hyper Paremeters를 좀 덜 사용하고 learning rate도 연속적으로 사용하니 Cosine Decay를 Learning Rate Decay로 좀 더 고려를 많이 한다고 합니다.

- cosine decay 출처: https://velog.io/@good159897/Learning-rate-Decay%EC%9D%98-%EC%A2%85%EB%A5%98

 

Learning rate Decay의 종류

Michigan 대학의 CS231n 강의를 보고 Learning rate Decay에 대해 정리를 했습니다.

velog.io

 

728x90

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

[tensorflow] model save, load  (0) 2022.11.29
[Deep Learning]tensorflow로 Dnn 모델 쌓기  (0) 2022.11.07