0
0
ML Pythonml~20 mins

Saving and loading models in ML Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Model Saving & Loading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this model saving code?
Consider the following code that trains a simple model and saves it. What will be printed after saving?
ML Python
import tensorflow as tf
from tensorflow.keras import layers

model = tf.keras.Sequential([
    layers.Dense(10, activation='relu', input_shape=(5,)),
    layers.Dense(1)
])
model.compile(optimizer='adam', loss='mse')

import numpy as np
x = np.random.random((10, 5))
y = np.random.random((10, 1))
model.fit(x, y, epochs=1, verbose=0)

model.save('my_model')
print('Model saved successfully')
A
Model saved successfully
Warning: Overwriting existing model
BNo output
CError: model.save() requires a file extension
DModel saved successfully
Attempts:
2 left
💡 Hint
The model.save() method saves the model in TensorFlow SavedModel format by default if no extension is given.
Model Choice
intermediate
1:30remaining
Which method correctly loads a saved Keras model?
You have saved a Keras model using model.save('my_model'). Which code snippet correctly loads this model?
Aloaded_model = tf.keras.models.load_model('my_model')
Bloaded_model = tf.keras.load('my_model')
Cloaded_model = tf.load_model('my_model')
Dloaded_model = tf.keras.models.Model.load('my_model')
Attempts:
2 left
💡 Hint
Look for the official Keras function to load models.
Hyperparameter
advanced
2:00remaining
Which saving format preserves custom objects in Keras models?
You have a Keras model with a custom layer. You want to save and load it without losing the custom layer. Which saving format should you use?
ASave model architecture as JSON only
BSave as HDF5 file with .h5 extension
CSave weights only with model.save_weights()
DSave as TensorFlow SavedModel format (folder)
Attempts:
2 left
💡 Hint
Custom objects are best preserved in one specific format.
Metrics
advanced
2:00remaining
After loading a saved model, which metric will NOT be available automatically?
You save a compiled Keras model with accuracy metric and load it later. Which metric will NOT be available without recompiling?
ACustom metric defined outside the model
BLoss function
CModel weights
DAccuracy metric
Attempts:
2 left
💡 Hint
Think about metrics that require custom code to be recognized.
🔧 Debug
expert
3:00remaining
Why does this code raise an error when loading a model?
You saved a model with a custom activation function but get an error when loading it with tf.keras.models.load_model('my_model'). Why?
ML Python
def custom_activation(x):
    return tf.nn.relu(x) * tf.math.sigmoid(x)

model = tf.keras.Sequential([
    tf.keras.layers.Dense(10, activation=custom_activation, input_shape=(5,)),
    tf.keras.layers.Dense(1)
])
model.save('my_model')

# Later loading
loaded_model = tf.keras.models.load_model('my_model')
AThe model was not compiled before saving
BThe input_shape is missing in the loading code
CThe custom activation function is not passed in custom_objects during loading
DThe model.save() method does not support custom layers
Attempts:
2 left
💡 Hint
Custom functions must be provided when loading models that use them.