Practical Exercises Chapter 3
Exercise 1: Saving and Loading a Keras Model
Task: Train a simple neural network on the MNIST dataset, save the model using the SavedModel format, and then load the saved model to make predictions on the test set.
Solution:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train, X_test = X_train / 255.0, X_test / 255.0
# Define a simple Sequential model
Flatten(input_shape=(28, 28)),
Dense(128, activation='relu'),
Dense(64, activation='relu'),
Dense(10, activation='softmax')