Keras-callbacks

Introductie tot Deep Learning met Keras

Miguel Esteban

Data Scientist & Founder

Wat is een callback?

Introductie tot Deep Learning met Keras

Callbacks in Keras

Introductie tot Deep Learning met Keras

Een gemiste callback

# Een model trainen en de history opslaan
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]
Introductie tot Deep Learning met Keras

Een gemiste callback

# Een model trainen en de history opslaan
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]
Introductie tot Deep Learning met Keras

History-plots

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

# Gebruik de history-metrics plt.plot(history.history['accuracy']) plt.plot(history.history['val_accuracy'])
# Maak het netjes plt.title('Model accuracy') plt.ylabel('Accuracy') plt.xlabel('Epoch') plt.legend(['Train', 'Test']) plt.show()

Introductie tot Deep Learning met Keras

History-plots

Introductie tot Deep Learning met Keras

Early stopping

# Early stopping importeren uit keras callbacks
from tensorflow.keras.callbacks import EarlyStopping

# Maak een early stopping-callback early_stopping = EarlyStopping(monitor='val_loss', patience=5)
# Train je model met de callback model.fit(X_train, y_train, epochs=100, validation_data=(X_test, y_test), callbacks = [early_stopping])
Introductie tot Deep Learning met Keras

Model checkpoint

# Model checkpoint importeren uit keras callbacks
from keras.callbacks import ModelCheckpoint

# Maak een model checkpoint-callback model_save = ModelCheckpoint('best_model.hdf5', save_best_only=True)
# Train je model met de callback model.fit(X_train, y_train, epochs=100, validation_data=(X_test, y_test), callbacks = [model_save])
Introductie tot Deep Learning met Keras

Laten we oefenen!

Introductie tot Deep Learning met Keras

Preparing Video For Download...