얼리 스토핑: 최적화를 더 똑똑하게
이제 최적화 과정 전반에 걸쳐 모델 성능을 모니터링하는 방법을 알게 되었으니, 더 이상 도움이 되지 않을 때는 얼리 스토핑으로 최적화를 중단할 수 있어요. 최적화가 자동으로 멈추므로, 동영상에서 Dan이 보여준 것처럼 .fit() 호출에서 epochs 값을 크게 설정해도 괜찮습니다.
최적화할 모델은 model로 이미 정의되어 있습니다. 이전과 마찬가지로 데이터는 predictors와 target으로 미리 로드되어 있어요.
이 연습은 강의의 일부입니다
Python으로 시작하는 Deep Learning
연습 안내
tensorflow.keras.callbacks에서EarlyStopping을 가져오세요.- 모델을 컴파일하세요.
optimizer는'adam', 손실 함수는'categorical_crossentropy', 그리고 각 epoch의 정확도를 확인할 수 있도록metrics=['accuracy']를 사용합니다. early_stopping_monitor라는 이름의EarlyStopping객체를 만드세요.EarlyStopping()의patience매개변수를2로 지정해, 검증 손실이 2 epoch 동안 개선되지 않으면 최적화를 중단합니다.predictors와target을 사용해 모델을 학습시키세요.epochs는30으로 지정하고, 검증 분할은0.3을 사용하세요. 또한callbacks매개변수에[early_stopping_monitor]를 전달하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Import EarlyStopping
____
# Save the number of columns in predictors: n_cols
n_cols = predictors.shape[1]
input_shape = (n_cols,)
# Specify the model
model = Sequential()
model.add(Dense(100, activation='relu', input_shape = input_shape))
model.add(Dense(100, activation='relu'))
model.add(Dense(2, activation='softmax'))
# Compile the model
____
# Define early_stopping_monitor
early_stopping_monitor = ____
# Fit the model
____