Keras 콜백

Keras로 시작하는 딥 러닝

Miguel Esteban

Data Scientist & Founder

콜백이란?

Keras로 시작하는 딥 러닝

Keras의 콜백

Keras로 시작하는 딥 러닝

놓치고 있던 콜백

# Training a model and saving its history
history = model.fit(X_train, y_train,
                    epochs=100,
                    metrics=['accuracy'])

print(history.history['loss'])
[0.6753975939750672, ..., 0.3155936544282096]
print(history.history['accuracy'])
[0.7030952412741525, ..., 0.8604761900220599]
Keras로 시작하는 딥 러닝

놓치고 있던 콜백

# Training a model and saving its history
history = model.fit(X_train, y_train,
                    epochs=100,
                    validation_data=(X_test, y_test),
                    metrics=['accuracy'])        

print(history.history['val_loss'])
[0.7753975939750672, ..., 0.4155936544282096]
print(history.history['val_accuracy'])
[0.6030952412741525, ..., 0.7604761900220599]
Keras로 시작하는 딥 러닝

히스토리 플롯

# Plot train vs test accuracy per epoch
plt.figure()

# Use the history metrics plt.plot(history.history['accuracy']) plt.plot(history.history['val_accuracy'])
# Make it pretty plt.title('Model accuracy') plt.ylabel('Accuracy') plt.xlabel('Epoch') plt.legend(['Train', 'Test']) plt.show()

Keras로 시작하는 딥 러닝

히스토리 플롯

Keras로 시작하는 딥 러닝

조기 종료(Early Stopping)

# Import early stopping from keras callbacks
from tensorflow.keras.callbacks import EarlyStopping

# Instantiate an early stopping callback early_stopping = EarlyStopping(monitor='val_loss', patience=5)
# Train your model with the callback model.fit(X_train, y_train, epochs=100, validation_data=(X_test, y_test), callbacks = [early_stopping])
Keras로 시작하는 딥 러닝

모델 체크포인트

# Import model checkpoint from keras callbacks
from keras.callbacks import ModelCheckpoint

# Instantiate a model checkpoint callback model_save = ModelCheckpoint('best_model.hdf5', save_best_only=True)
# Train your model with the callback model.fit(X_train, y_train, epochs=100, validation_data=(X_test, y_test), callbacks = [model_save])
Keras로 시작하는 딥 러닝

연습해 봅시다!

Keras로 시작하는 딥 러닝

Preparing Video For Download...