[Deep Learning] 더 나은 신경망 학습을 위한 방법들

2022. 4. 29. 22:46AI/Codestates

728x90
반응형

신경망 학습이 더 잘되도록 하는 방법

▶ 학습률 감소 / 계획법 ( Learning rate Decay / Scheduling )

→ 학습률 ( Learning rate ) 

매 가중치에 대해 구해진 기울기 값을 얼마나 경사 하강법에 적용할지를 결정하는 하이퍼파라미터

  • 학습률이 너무 낮으면 최적점에 이르기까지 너무 오래 걸리거나, 주어진 Iteration 내에서 최적점에 도달하는데 실패하기도 함
  • 학습률이 너무 높으면 경사 하강 과정에서 발산하면서 모델이 최적값을 찾을 수 없게 되어버림

→ 학습률 감소 ( Learning rate Decay )

model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001, beta_1 = 0.89)
             , loss='sparse_categorical_crossentropy'
             , metrics=['accuracy'])

→ 학습률 계획법 ( Learning rate Scheduling )

lr_decayed_fn = (tf.keras.experimental.CosineDecayRestarts(0.001, 1000))
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=lr_decayed_fn),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

▶ 가중치 초기화 ( Weight Initialization )

초기 가중치 설정과 관련된 부분이기 때문에 신경망에서 매우 중요한 요소임

→ 표준편차를 1인 정규분포로 가중치 초기화

  • 활성값이 고르지 못할 경우 학습이 제대로 이루어 지지 않음
  • 잘 사용 되지 않음
  •  

→ Xavier 초기화

  • 위에 문제점을 해결하기 위해 등장한 방법
  • 활성함수 시그모이드를 사용하는 것이 유리
  • Xavier 초기화는 활성화 함수가 시그모이드인 신경망에서는 잘동작 하지만 ReLU 일 경우에는 층이 지날 수록 활성값이 고르지 못함

→ He 초기화

  • 위 문제를 해결하기 위해 등장한 방법
  • 활성함수 ReLU를 사용하는것이 유리

 

과적합 ( Overfitting ) 방지를 위한 방법

▶ 가중치 감소 ( Weight Decay )

→ 과적합은 주로 가중치의 값이 클 경우 발생하게됨 그래서 가중치가 너무 커지지 않도록 조건을 추가함

조건을 어떻게 적용할지에 따라  L1 Regularization(LASSO), L2 Regularization(Ridge) 으로 나뉨

Dense(128,
      kernel_regularizer=regularizers.l2(0.001),
      activity_regularizer=regularizers.l1(0.001))

 드롭 아웃 ( Dropout )

Iteration 마다 레이어 노드 중 일부를 사용하지 않으면서 학습을 진행하는 방법

Dense(128,
      kernel_regularizer=regularizers.l2(0.001),
      activity_regularizer=regularizers.l1(0.001))
Dropout(0.7)

 조기 종료( Early Stopping )

학습 데이터에 대한 손실은 계속 줄어들지만 검증 데이터셋에 대한 손실이 증가한다면 학습을 조기에 종료하도록 설정하는 방법

# 파라미터 저장 경로를 설정하는 코드입니다.
checkpoint_filepath = "FMbest.hdf5"

early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1)

save_best = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_filepath, monitor='val_loss', verbose=1, save_best_only=True,
    save_weights_only=True, mode='auto', save_freq='epoch', options=None)


model.fit(X_train, y_train, batch_size=32, epochs=30, verbose=1, 
          validation_data=(X_test,y_test), 
          callbacks=[early_stop, save_best])

 

728x90
반응형

'AI > Codestates' 카테고리의 다른 글

[Day 71] Sprint Review  (0) 2022.05.04
[Deep Learning] Hyperparameter Tuning  (0) 2022.05.02
[Deep Learning] Training Neural Network  (0) 2022.04.28
[Deep Learning] Artificial Neural Network  (0) 2022.04.27
[Day 66] Section3 Review  (0) 2022.04.27