Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fchollet
GitHub Repository: fchollet/deep-learning-with-python-notebooks
Path: blob/master/second_edition/chapter13_best-practices-for-the-real-world.ipynb
713 views
Kernel: Python 3

This is a companion notebook for the book Deep Learning with Python, Second Edition. For readability, it only contains runnable code blocks and section titles, and omits everything else in the book: text paragraphs, figures, and pseudocode.

If you want to be able to follow what's going on, I recommend reading the notebook side by side with your copy of the book.

This notebook was generated for TensorFlow 2.6.

Best practices for the real world

Getting the most out of your models

Hyperparameter optimization

Using KerasTuner

!pip install keras-tuner -q

A KerasTuner model-building function

from tensorflow import keras from tensorflow.keras import layers def build_model(hp): units = hp.Int(name="units", min_value=16, max_value=64, step=16) model = keras.Sequential([ layers.Dense(units, activation="relu"), layers.Dense(10, activation="softmax") ]) optimizer = hp.Choice(name="optimizer", values=["rmsprop", "adam"]) model.compile( optimizer=optimizer, loss="sparse_categorical_crossentropy", metrics=["accuracy"]) return model

A KerasTuner HyperModel

import kerastuner as kt class SimpleMLP(kt.HyperModel): def __init__(self, num_classes): self.num_classes = num_classes def build(self, hp): units = hp.Int(name="units", min_value=16, max_value=64, step=16) model = keras.Sequential([ layers.Dense(units, activation="relu"), layers.Dense(self.num_classes, activation="softmax") ]) optimizer = hp.Choice(name="optimizer", values=["rmsprop", "adam"]) model.compile( optimizer=optimizer, loss="sparse_categorical_crossentropy", metrics=["accuracy"]) return model hypermodel = SimpleMLP(num_classes=10)
tuner = kt.BayesianOptimization( build_model, objective="val_accuracy", max_trials=100, executions_per_trial=2, directory="mnist_kt_test", overwrite=True, )
tuner.search_space_summary()
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() x_train = x_train.reshape((-1, 28 * 28)).astype("float32") / 255 x_test = x_test.reshape((-1, 28 * 28)).astype("float32") / 255 x_train_full = x_train[:] y_train_full = y_train[:] num_val_samples = 10000 x_train, x_val = x_train[:-num_val_samples], x_train[-num_val_samples:] y_train, y_val = y_train[:-num_val_samples], y_train[-num_val_samples:] callbacks = [ keras.callbacks.EarlyStopping(monitor="val_loss", patience=5), ] tuner.search( x_train, y_train, batch_size=128, epochs=100, validation_data=(x_val, y_val), callbacks=callbacks, verbose=2, )

Querying the best hyperparameter configurations

top_n = 4 best_hps = tuner.get_best_hyperparameters(top_n)
def get_best_epoch(hp): model = build_model(hp) callbacks=[ keras.callbacks.EarlyStopping( monitor="val_loss", mode="min", patience=10) ] history = model.fit( x_train, y_train, validation_data=(x_val, y_val), epochs=100, batch_size=128, callbacks=callbacks) val_loss_per_epoch = history.history["val_loss"] best_epoch = val_loss_per_epoch.index(min(val_loss_per_epoch)) + 1 print(f"Best epoch: {best_epoch}") return best_epoch
def get_best_trained_model(hp): best_epoch = get_best_epoch(hp) model = build_model(hp) model.fit( x_train_full, y_train_full, batch_size=128, epochs=int(best_epoch * 1.2)) return model best_models = [] for hp in best_hps: model = get_best_trained_model(hp) model.evaluate(x_test, y_test) best_models.append(model)
best_models = tuner.get_best_models(top_n)

The art of crafting the right search space

The future of hyperparameter tuning: automated machine learning

Model ensembling

Scaling-up model training

Speeding up training on GPU with mixed precision

Understanding floating-point precision

import tensorflow as tf import numpy as np np_array = np.zeros((2, 2)) tf_tensor = tf.convert_to_tensor(np_array) tf_tensor.dtype
np_array = np.zeros((2, 2)) tf_tensor = tf.convert_to_tensor(np_array, dtype="float32") tf_tensor.dtype

Mixed-precision training in practice

from tensorflow import keras keras.mixed_precision.set_global_policy("mixed_float16")

Multi-GPU training

Getting your hands on two or more GPUs

Single-host, multi-device synchronous training

TPU training

Using a TPU via Google Colab

Leveraging step fusing to improve TPU utilization

Summary