0
0
ML Pythonml~20 mins

Saving and loading models in ML Python - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Saving and loading models
Problem:You trained a simple neural network on the MNIST dataset. The model achieves good accuracy, but you want to save it to disk and load it later to make predictions without retraining.
Current Metrics:Training accuracy: 98%, Validation accuracy: 97%
Issue:Currently, the model is trained every time you run the code, which wastes time and resources.
Your Task
Save the trained model to a file and load it back to make predictions. Verify that the loaded model gives the same accuracy as the original.
Use TensorFlow/Keras for model saving and loading.
Do not retrain the model after loading.
Use the MNIST dataset provided by Keras.
Hint 1
Hint 2
Hint 3
Solution
ML Python
import tensorflow as tf
from tensorflow import keras

# Load MNIST data
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# Normalize data
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255

# Flatten images
x_train = x_train.reshape(-1, 28*28)
x_test = x_test.reshape(-1, 28*28)

# Build a simple model
model = keras.Sequential([
    keras.layers.Dense(128, activation='relu', input_shape=(28*28,)),
    keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train the model
model.fit(x_train, y_train, epochs=5, batch_size=32, validation_split=0.1)

# Save the model
model.save('mnist_model.h5')

# Load the model
loaded_model = keras.models.load_model('mnist_model.h5')

# Evaluate loaded model
loss, accuracy = loaded_model.evaluate(x_test, y_test, verbose=0)
print(f'Loaded model accuracy: {accuracy * 100:.2f}%')
Added model.save('mnist_model.h5') to save the trained model to disk.
Used keras.models.load_model('mnist_model.h5') to load the saved model.
Evaluated the loaded model on the test set to verify accuracy without retraining.
Results Interpretation

Before saving, the model had a validation accuracy of 97%. After saving and loading, the model's accuracy on the test set remains about 97%, confirming the model was saved and loaded correctly.

Saving and loading models allows you to reuse trained models without retraining, saving time and resources while keeping performance intact.
Bonus Experiment
Try saving the model in TensorFlow's SavedModel format instead of HDF5 and load it back.
💡 Hint
Use model.save('folder_name') without the .h5 extension to save in SavedModel format, and load it with keras.models.load_model('folder_name').