Challenge - 5 Problems
Model Saving & Loading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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')
Attempts:
2 left
💡 Hint
The model.save() method saves the model in TensorFlow SavedModel format by default if no extension is given.
✗ Incorrect
The code trains a simple model and saves it to the folder 'my_model'. Since no error occurs, the print statement outputs 'Model saved successfully'.
❓ Model Choice
intermediate1: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?
Attempts:
2 left
💡 Hint
Look for the official Keras function to load models.
✗ Incorrect
The correct function to load a saved Keras model is tf.keras.models.load_model(path). Other options are invalid or do not exist.
❓ Hyperparameter
advanced2: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?
Attempts:
2 left
💡 Hint
Custom objects are best preserved in one specific format.
✗ Incorrect
Saving as HDF5 (.h5) preserves custom layers and objects better than SavedModel format, which may require extra steps to load custom objects.
❓ Metrics
advanced2: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?
Attempts:
2 left
💡 Hint
Think about metrics that require custom code to be recognized.
✗ Incorrect
Custom metrics defined outside the model are not saved automatically and require passing custom_objects when loading or recompiling.
🔧 Debug
expert3: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')
Attempts:
2 left
💡 Hint
Custom functions must be provided when loading models that use them.
✗ Incorrect
When loading a model with custom functions, you must pass them in the custom_objects argument to load_model, otherwise it raises an error.