When saving and loading models, the key metric is model performance consistency. This means the model should give the same predictions and accuracy after loading as it did before saving. We check metrics like accuracy, loss, or any task-specific score before saving and after loading to make sure the model was saved correctly without losing information.
Saving and loading models in ML Python - Model Metrics & Evaluation
Imagine a classification model saved and loaded. We compare the confusion matrix before saving and after loading:
Before saving:
TP=50 FP=10
FN=5 TN=35
After loading:
TP=50 FP=10
FN=5 TN=35
If these numbers match exactly, the model was saved and loaded correctly.
Saving and loading models does not directly affect precision or recall tradeoffs. But if the model changes after loading, these metrics can shift unexpectedly. For example, if a cancer detection model's recall drops after loading, it means the model lost some ability to find cancer cases. This shows the importance of verifying metrics after loading.
Good: The model's accuracy, precision, recall, and loss before saving and after loading are the same or very close (differences within a tiny margin like 0.001). This means the model was saved and loaded without damage.
Bad: Metrics change a lot after loading. For example, accuracy drops from 90% to 70%, or loss increases significantly. This means the saved model file is corrupted or incomplete.
- Not verifying metrics after loading: Assuming the model works without checking can hide problems.
- Data leakage: If the test data leaks into training before saving, metrics look good but are misleading.
- Overfitting indicators: If the model performs perfectly on training data but poorly on test data after loading, it may be overfitting.
- Version mismatch: Saving a model in one software version and loading in another can cause errors or metric changes.
Your model has 98% accuracy before saving but after loading, accuracy is 85%. Is it good for production? Why or why not?
Answer: No, it is not good. The large drop in accuracy means the model was not saved or loaded correctly. You must fix this before using the model in production to avoid wrong predictions.
Practice
Solution
Step 1: Understand model saving
Saving a model stores its learned parameters so it can be reused later.Step 2: Identify the main benefit
This avoids retraining, saving time and resources.Final Answer:
To reuse the model later without retraining -> Option BQuick Check:
Saving = reuse without retraining [OK]
- Thinking saving improves accuracy automatically
- Confusing saving with speeding up training
- Believing saving deletes data
model to a file called my_model.h5?Solution
Step 1: Recall Keras save syntax
Keras models are saved usingmodel.save(filename).Step 2: Match correct syntax
model.save('my_model.h5') matches this syntax exactly.Final Answer:
model.save('my_model.h5') -> Option AQuick Check:
model.save(filename) saves model [OK]
- Using incorrect function names like save_model or save
- Swapping argument order
- Calling save_model as a method on model
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)Solution
Step 1: Understand load_model usage
load_model loads a saved Keras model from a file.Step 2: Predict method output
model.predict returns a numpy array of prediction probabilities for input data.Final Answer:
A numpy array of prediction probabilities -> Option DQuick Check:
load_model + predict = numpy array output [OK]
- Thinking load_model only loads from directories
- Expecting printed model architecture from predict
- Assuming predict returns class labels directly
load_model('model.h5') but get an error: OSError: Unable to open file (file signature not found). What is the most likely cause?Solution
Step 1: Analyze the error message
OSError about file signature means the file is not recognized as a valid HDF5 model file.Step 2: Identify common causes
This usually happens if the file is corrupted or not a saved model file.Final Answer:
The file 'model.h5' is corrupted or not a valid model file -> Option CQuick Check:
File signature error = corrupted or invalid file [OK]
- Assuming Python version mismatch causes this error
- Forgetting to import but error message differs
- Thinking file size causes signature errors
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?Solution
Step 1: Load the saved model correctly
Useload_model('model_v1.h5')from tensorflow.keras.models to load the model.Step 2: Continue training and save updated model
Callmodel.fit()with new data, then save withmodel.save('model_v2.h5').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 AQuick 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]
- Using undefined functions like load or save_model
- Forgetting to import load_model
- Not calling fit before saving updated model
Ready to prove it?
Pass the test and this pattern moves unknown
