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