0
0
TensorFlowml~20 mins

Why model persistence enables deployment in TensorFlow - Experiment to Prove It

Choose your learning style9 modes available
Experiment - Why model persistence enables deployment
Problem:You have trained a TensorFlow model to classify images, but each time you restart your program, you lose the trained model and have to train it again.
Current Metrics:Training accuracy: 92%, Validation accuracy: 89%, but model must be retrained every time the program runs.
Issue:Without saving the model, you cannot reuse the trained model for deployment or real-world use, causing inefficiency and delays.
Your Task
Save the trained TensorFlow model to disk and then load it back to make predictions without retraining.
Use TensorFlow's built-in model saving and loading functions.
Do not change the model architecture or training process.
Ensure the loaded model produces the same predictions as the original.
Hint 1
Hint 2
Hint 3
Solution
TensorFlow
import tensorflow as tf
from tensorflow.keras import layers, models
import numpy as np

# Create a simple model
model = models.Sequential([
    layers.Flatten(input_shape=(28, 28)),
    layers.Dense(128, activation='relu'),
    layers.Dense(10, activation='softmax')
])

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

# Dummy data for training
x_train = np.random.random((1000, 28, 28))
y_train = np.random.randint(0, 10, 1000)

# Train the model
model.fit(x_train, y_train, epochs=3, batch_size=32)

# Save the model to disk
model.save('saved_model/my_model')

# Load the model from disk
loaded_model = tf.keras.models.load_model('saved_model/my_model')

# Test prediction with original and loaded model
sample_input = np.random.random((1, 28, 28))
original_pred = model.predict(sample_input)
loaded_pred = loaded_model.predict(sample_input)

print('Original model prediction:', original_pred)
print('Loaded model prediction:', loaded_pred)
Added model.save() after training to save the model to disk.
Used tf.keras.models.load_model() to load the saved model.
Verified that the loaded model produces the same predictions as the original.
Results Interpretation

Before: Model trained but lost after program ends, requiring retraining each time.

After: Model saved to disk and loaded back, predictions remain consistent, no retraining needed.

Saving (persisting) a trained model allows you to reuse it anytime without retraining. This is essential for deploying models in real applications where you want fast predictions without delay.
Bonus Experiment
Try saving the model in the TensorFlow SavedModel format and also in HDF5 format. Compare the file sizes and loading times.
💡 Hint
Use model.save('path', save_format='h5') for HDF5 format and default for SavedModel format.