Bird
Raised Fist0
ML Pythonml~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of saving a trained machine learning model?
easy
A. To increase the model's training speed
B. To reuse the model later without retraining
C. To improve the model's accuracy automatically
D. To delete the training data after use

Solution

  1. Step 1: Understand model saving

    Saving a model stores its learned parameters so it can be reused later.
  2. Step 2: Identify the main benefit

    This avoids retraining, saving time and resources.
  3. Final Answer:

    To reuse the model later without retraining -> Option B
  4. Quick Check:

    Saving = reuse without retraining [OK]
Hint: Saving means keep model for later use without retraining [OK]
Common Mistakes:
  • Thinking saving improves accuracy automatically
  • Confusing saving with speeding up training
  • Believing saving deletes data
2. Which of the following is the correct way to save a Keras model named model to a file called my_model.h5?
easy
A. model.save('my_model.h5')
B. save_model('my_model.h5', model)
C. model.save_model('my_model.h5')
D. save('model', 'my_model.h5')

Solution

  1. Step 1: Recall Keras save syntax

    Keras models are saved using model.save(filename).
  2. Step 2: Match correct syntax

    model.save('my_model.h5') matches this syntax exactly.
  3. Final Answer:

    model.save('my_model.h5') -> Option A
  4. Quick Check:

    model.save(filename) saves model [OK]
Hint: Use model.save('filename') to save Keras models [OK]
Common Mistakes:
  • Using incorrect function names like save_model or save
  • Swapping argument order
  • Calling save_model as a method on model
3. What will be the output of the following code snippet?
from tensorflow.keras.models import load_model
model = load_model('saved_model.h5')
predictions = model.predict([[5.1, 3.5, 1.4, 0.2]])
print(predictions)
medium
A. The model architecture printed as text
B. An error because load_model requires a directory, not a file
C. A list of class labels as strings
D. A numpy array of prediction probabilities

Solution

  1. Step 1: Understand load_model usage

    load_model loads a saved Keras model from a file.
  2. Step 2: Predict method output

    model.predict returns a numpy array of prediction probabilities for input data.
  3. Final Answer:

    A numpy array of prediction probabilities -> Option D
  4. Quick Check:

    load_model + predict = numpy array output [OK]
Hint: load_model loads model; predict returns probabilities array [OK]
Common Mistakes:
  • Thinking load_model only loads from directories
  • Expecting printed model architecture from predict
  • Assuming predict returns class labels directly
4. You try to load a saved model using load_model('model.h5') but get an error: OSError: Unable to open file (file signature not found). What is the most likely cause?
medium
A. The model file is too large to load
B. You forgot to import load_model from keras
C. The file 'model.h5' is corrupted or not a valid model file
D. The model was saved with a different Python version

Solution

  1. Step 1: Analyze the error message

    OSError about file signature means the file is not recognized as a valid HDF5 model file.
  2. Step 2: Identify common causes

    This usually happens if the file is corrupted or not a saved model file.
  3. Final Answer:

    The file 'model.h5' is corrupted or not a valid model file -> Option C
  4. Quick Check:

    File signature error = corrupted or invalid file [OK]
Hint: File signature errors mean corrupted or wrong file type [OK]
Common Mistakes:
  • Assuming Python version mismatch causes this error
  • Forgetting to import but error message differs
  • Thinking file size causes signature errors
5. You have a trained model saved as model_v1.h5. You want to load it, continue training on new data, then save the updated model as model_v2.h5. Which code snippet correctly does this?
hard
A. from tensorflow.keras.models import load_model model = load_model('model_v1.h5') model.fit(new_data, new_labels, epochs=5) model.save('model_v2.h5')
B. model = load('model_v1.h5') model.train(new_data, new_labels) save_model(model, 'model_v2.h5')
C. load_model('model_v1.h5') train_model(new_data, new_labels) save('model_v2.h5')
D. from keras import load model = load('model_v1.h5') model.fit(new_data, new_labels) model.save('model_v2.h5')

Solution

  1. Step 1: Load the saved model correctly

    Use load_model('model_v1.h5') from tensorflow.keras.models to load the model.
  2. Step 2: Continue training and save updated model

    Call model.fit() with new data, then save with model.save('model_v2.h5').
  3. Final Answer:

    from tensorflow.keras.models import load_model model = load_model('model_v1.h5') model.fit(new_data, new_labels, epochs=5) model.save('model_v2.h5') -> Option A
  4. Quick Check:

    Load, fit, save = from tensorflow.keras.models import load_model model = load_model('model_v1.h5') model.fit(new_data, new_labels, epochs=5) model.save('model_v2.h5') [OK]
Hint: Load with load_model, fit new data, then save again [OK]
Common Mistakes:
  • Using undefined functions like load or save_model
  • Forgetting to import load_model
  • Not calling fit before saving updated model
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard