-
[HandsOn]11. 심층 신경망 훈련하기 - 내용 정리 3[도서완독]Hands On Machine Learning 2022. 7. 8. 16:48
11.4 규제를 사용해 과대적합 피하기
11.4.1 L1과 L2 규제
신경망의 연결 가중치를 제한하기 위해 l2 규제를 사용하거나 희소 모델을 만들기 위해 l1 규제를 적용
layer=keras.layers.Dense(100, activation="elu", kernel_initializer="he_normal", kernel_regularizer=keras.regularizers.l2(0.01))
일반적으로 네트워크의 모든 은닉층에 동일한 활성화 함수, 동일한 초기화 전략을 사용하거나 하기 때문에 똑같은 코드를 반복하는 경우가 많음...
이는 코드를 읽기 어렵게 만듬.
코드를 리팩터링 하거나, 파이썬의 functools.partial() 함수를 사용
from functools import partial RegularizedDense = partial(keras.layers.Dense, activation="elu", kernel_initializer="he_normal", kenel_regularizer=keras.regularizers.l2(0.01)) model=keras.models.Sequential([ keras.layers.Flatten(input_shape=[28,28]), RegularizedDense(300), RegularizedDense(100), RegularizedDense(10,activation="softmax", kernel_initializer="glorot_uniform") ])
11.4.2 드롭아웃
- 가장 인기 있는 규제 기법
- 각 뉴런( 입력뉴런 포함, 출력뉴런 제외 )는 임시적으로 드롭아웃될 확률 p를 가짐.
- 이번 훈련 스텝에는 완전히 무시되지만 다음 스텝에는 활성화 될 수 있음
- p: 드롭 아웃 비율(통상 10~50%)
- 훈련이 끝난 후에는 뉴런에 더는 드롭아웃을 적용하지 않음
훈련이 끝난 뒤 각 입력의 연결 가중치에 보존 확률 (1-p) 를 곱해야 합니다.
model=keras.models.Sequential([ keras.layers.Flatten(input_shape=[28,28]), keras.layers.Dropout(rate=0.2), keras.layers.Dense(300,activation="elu",kernel_initializer="he_normal"), keras.layers.Dropout(rate=0.2), keras.layers.Dense(100,activation="elu",kernel_initializer="he_normal"), keras.layers.Dropout(rate=0.2), keras.layers.Dense(10,activation="softmax") ])
❗ 드롭아웃은 훈련하는 동안에만 활성화되므로 훈련 손실과 검증 손실을 비교하면 오해를 일으키기 쉽다.
특히 비슷한 훈련 손실과 검증 손실을 얻었더라도 모델이 훈련세트에 과대적합될 수 있다.
따라서 (예를 들어 훈련이 끝난 후) 드롭아웃을 빼고 훈련 손실을 평가해야 함.'[도서완독]Hands On Machine Learning' 카테고리의 다른 글
[HandsOn]12. 텐서플로를 사용한 사용자 정의 모델과 훈련 - 내용 정리 2 (0) 2022.07.20 [HandsOn]12. 텐서플로를 사용한 사용자 정의 모델과 훈련 - 내용 정리 1 (0) 2022.07.19 [HandsOn]11. 심층 신경망 훈련하기 - 내용 정리 2 (0) 2022.07.06 [HandsOn]11. 심층 신경망 훈련하기 - 내용 정리 1 (0) 2022.07.05 [HandsOn]10. 케라스를 사용한 인공 신경망 - 내용 정리 3 (0) 2022.07.01