When saving only the weights of a model, the key metric to watch is the model's performance metrics before and after loading the weights. This ensures the saved weights correctly capture the learned knowledge. Common metrics include loss and accuracy on validation data. If these metrics stay consistent, the weights were saved and loaded properly.
Saving weights only in TensorFlow - Model Metrics & Evaluation
For classification models, a confusion matrix helps check if the loaded weights produce the same predictions as before saving. For example, a binary classifier confusion matrix:
Predicted
| 1 | 0 |
---+-----+-----+
1 | TP | FN |
0 | FP | TN |
After loading weights, the counts of TP, FP, TN, FN should be similar to before saving.
Saving weights only does not directly affect precision or recall, but if weights are corrupted or mismatched, model predictions can degrade, causing precision and recall to drop. For example:
- If weights are saved and loaded correctly, precision and recall remain stable.
- If weights are partially saved or loaded incorrectly, recall might drop, missing positive cases.
- Or precision might drop, causing more false alarms.
Thus, verifying metrics after loading weights is crucial.
Good: After loading weights, validation loss and accuracy remain close to values before saving. Confusion matrix counts are stable. Precision and recall do not drop significantly.
Bad: After loading weights, validation loss increases sharply, accuracy drops, or confusion matrix shows many more errors. This means weights were not saved or loaded properly.
- Accuracy paradox: High accuracy after loading weights might be misleading if the dataset is imbalanced.
- Data leakage: If validation data leaks into training, metrics before saving weights may be unrealistically high.
- Overfitting: If weights are saved from an overfitted model, metrics may look good on training but poor on new data.
- Mismatch in model architecture: Loading weights into a different model structure causes errors or poor metrics.
No, this is not good for fraud detection. The model misses 88% of fraud cases (low recall), which is dangerous. Saving and loading weights correctly is important, but also ensure the model is trained to detect fraud well. High accuracy alone can be misleading if the data is imbalanced.
Practice
Solution
Step 1: Understand what weights represent
Weights are the learned parameters of a model after training.Step 2: Differentiate saving weights only vs full model
Saving weights only means storing these parameters without saving the model's structure or optimizer state.Final Answer:
Saving just the learned parameters without the model structure -> Option BQuick Check:
Saving weights only = learned parameters only [OK]
- Confusing weights with full model saving
- Thinking architecture is saved with weights only
- Assuming dataset is saved with weights
model to a file weights.h5?Solution
Step 1: Recall TensorFlow method to save weights
The method to save weights ismodel.save_weights(filepath).Step 2: Check syntax correctness
model.save_weights('weights.h5')uses correct method and argument format.model.save('weights.h5')saves full model, C is invalid TensorFlow syntax, A uses wrong argument name.Final Answer:
model.save_weights('weights.h5') -> Option CQuick Check:
Use model.save_weights(filepath) to save weights [OK]
- Using model.save() instead of save_weights()
- Incorrect argument names or order
- Trying to use non-existent TensorFlow functions
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(5, input_shape=(3,))
])
model.save_weights('w.h5')
new_model = tf.keras.Sequential([
tf.keras.layers.Dense(5, input_shape=(3,))
])
new_model.load_weights('w.h5')
print(new_model.get_weights()[0].shape)What will be the printed output?
Solution
Step 1: Understand model architecture
The model has one Dense layer with 5 units and input shape (3,), so weights shape is (input_dim, units) = (3, 5).Step 2: Loading weights into identical architecture
Since new_model has the same architecture, loading weights works and weights shape remains (3, 5).Final Answer:
(3, 5) -> Option AQuick Check:
Weights shape = (input_dim, units) = (3, 5) [OK]
- Confusing input and output dimensions order
- Assuming error when architectures match
- Expecting bias shape instead of weights
model.save_weights('w.h5'). When loading weights with new_model.load_weights('w.h5'), you get an error about mismatched shapes. What is the most likely cause?Solution
Step 1: Understand weight loading requirements
Weights can only be loaded if the new model has the same architecture as the original model.Step 2: Analyze error cause
Mismatched shapes error usually means layer sizes differ between saved and new model.Final Answer:
The new model architecture is different from the original model -> Option DQuick Check:
Model architectures must match to load weights [OK]
- Assuming file corruption without checking architecture
- Thinking compile is needed before loading weights
- Believing .h5 files are unsupported for weights
Solution
Step 1: Identify saving weights only method
Usemodel.save_weights()to save only learned parameters without model structure.Step 2: Explain how friend uses weights
Friend must recreate the exact model architecture in code, then load weights withload_weights().Final Answer:
Save weights with model.save_weights(); friend recreates model architecture then loads weights -> Option AQuick Check:
Save weights only + recreate model to load weights [OK]
- Thinking weights can be loaded without model code
- Confusing full model save with weights only
- Assuming JSON architecture saves weights
Ready to prove it?
Pass the test and this pattern moves unknown
