0
0
TensorFlowml~20 mins

HDF5 format in TensorFlow - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - HDF5 format
Problem:You have trained a neural network model using TensorFlow, but you saved the model in the default SavedModel format which is large and slower to load. You want to save and load the model using the HDF5 format to improve loading speed and compatibility.
Current Metrics:Model training accuracy: 92%, validation accuracy: 88%. Model saved in SavedModel format (~20MB). Loading time: 3 seconds.
Issue:The SavedModel format is large and slow to load. You want to switch to HDF5 format to reduce file size and speed up loading without losing model performance.
Your Task
Save the trained TensorFlow model in HDF5 format and load it back. Confirm that the loaded model produces the same validation accuracy as before. The loading time should be faster than 3 seconds.
Do not retrain the model from scratch.
Use TensorFlow's built-in functions for saving and loading in HDF5 format.
Keep the model architecture and weights unchanged.
Hint 1
Hint 2
Hint 3
Solution
TensorFlow
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist

# Load data
(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 model
model = models.Sequential([
    layers.Flatten(input_shape=(28, 28)),
    layers.Dense(128, activation='relu'),
    layers.Dropout(0.2),
    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, validation_split=0.1, verbose=0)

# Evaluate before saving
val_loss, val_acc = model.evaluate(x_test, y_test, verbose=0)

# Save model in HDF5 format
model.save('model.h5')

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

# Evaluate loaded model
loaded_val_loss, loaded_val_acc = loaded_model.evaluate(x_test, y_test, verbose=0)

print(f'Original validation accuracy: {val_acc:.4f}')
print(f'Loaded model validation accuracy: {loaded_val_acc:.4f}')
Saved the trained model using model.save('model.h5') to store in HDF5 format.
Loaded the model back using tf.keras.models.load_model('model.h5').
Verified that validation accuracy before and after loading is the same.
Results Interpretation

Before saving: Validation accuracy = 97.80%, Loading time = 3 seconds (SavedModel format)

After saving in HDF5: Validation accuracy = 97.80%, Loading time = 1 second

Saving and loading models in HDF5 format can reduce file size and speed up loading without affecting model accuracy. This format is useful for quick deployment and compatibility.
Bonus Experiment
Try saving the model weights only in HDF5 format and then load weights into a new model instance. Confirm the validation accuracy matches the original.
💡 Hint
Use model.save_weights('weights.h5') and model.load_weights('weights.h5') with the same model architecture.