What does 'saving weights only' mean in TensorFlow?
It means saving just the learned parameters (weights and biases) of a model, without saving the model's architecture or optimizer state.
Click to reveal answer
beginner
How do you save only the weights of a TensorFlow model?
Use the method model.save_weights('path') to save only the weights to the specified path.
Click to reveal answer
beginner
How do you load saved weights back into a TensorFlow model?
Use model.load_weights('path') to load the saved weights into the model with the same architecture.
Click to reveal answer
intermediate
Why might you want to save only weights instead of the full model?
Saving only weights is smaller in size and useful when you want to recreate the model architecture separately or share just the learned parameters.
Click to reveal answer
intermediate
What file formats can TensorFlow use to save weights?
TensorFlow can save weights in TensorFlow checkpoint format (default) or in HDF5 format if the filename ends with .h5.
Click to reveal answer
Which TensorFlow method saves only the weights of a model?
Amodel.save_weights()
Bmodel.save()
Cmodel.load_weights()
Dmodel.compile()
✗ Incorrect
model.save_weights() saves only the weights. model.save() saves the full model.
What must you do before loading saved weights into a model?
ASave the model again
BCompile the model with a new optimizer
CCall model.fit() first
DDefine the model architecture exactly the same
✗ Incorrect
The model architecture must match to load weights correctly.
What file extension saves weights in HDF5 format in TensorFlow?
A.h5
B.ckpt
C.json
D.pb
✗ Incorrect
Using .h5 extension saves weights in HDF5 format.
Why might saving only weights be preferred over saving the full model?
AWeights save the model architecture
BWeights include optimizer state
CWeights files are smaller and easier to share
DWeights save training history
✗ Incorrect
Weights files are smaller because they exclude architecture and optimizer info.
Which of these is NOT saved when using model.save_weights()?
AWeights and biases
BModel architecture
CLearned parameters
DTensorFlow checkpoint files
✗ Incorrect
model.save_weights() does not save the model architecture.
Explain the process and benefits of saving only weights in TensorFlow models.
Think about what is saved and why it might be useful.
You got /5 concepts.
Describe how to load saved weights into a TensorFlow model and what conditions must be met.
Consider what is needed before loading weights.
You got /4 concepts.
Practice
(1/5)
1. What does saving weights only in TensorFlow mean?
easy
A. Saving the entire model including architecture and optimizer
B. Saving just the learned parameters without the model structure
C. Saving only the model architecture without weights
D. Saving the training dataset along with the model
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 B
Quick Check:
Saving weights only = learned parameters only [OK]
Hint: Weights only means parameters, not architecture or optimizer [OK]
Common Mistakes:
Confusing weights with full model saving
Thinking architecture is saved with weights only
Assuming dataset is saved with weights
2. Which of the following is the correct syntax to save only weights of a TensorFlow model named model to a file weights.h5?
easy
A. model.save_weights(weights='weights.h5')
B. model.save('weights.h5')
C. model.save_weights('weights.h5')
D. tf.save_weights(model, 'weights.h5')
Solution
Step 1: Recall TensorFlow method to save weights
The method to save weights is model.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 C
Quick Check:
Use model.save_weights(filepath) to save weights [OK]
Hint: Use model.save_weights('filename') to save weights only [OK]
Common Mistakes:
Using model.save() instead of save_weights()
Incorrect argument names or order
Trying to use non-existent TensorFlow functions
3. Consider this code snippet:
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?
medium
A. (3, 5)
B. (5, 3)
C. (5,)
D. Error: weights shape mismatch
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).
4. You saved weights using 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?
medium
A. TensorFlow does not support loading weights from .h5 files
B. The weights file is corrupted
C. You forgot to compile the new model before loading weights
D. The new model architecture is different from the original model
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 D
Quick Check:
Model architectures must match to load weights [OK]
Hint: Check model architectures match before loading weights [OK]
Common Mistakes:
Assuming file corruption without checking architecture
Thinking compile is needed before loading weights
Believing .h5 files are unsupported for weights
5. You want to share your trained model's learned parameters with a friend, but not the full model code. Which approach is best to save and share only the weights, and what must your friend do to use them?
hard
A. Save weights with model.save_weights(); friend recreates model architecture then loads weights
B. Save full model with model.save(); friend loads full model directly
C. Save weights as a numpy file; friend loads weights without model code
D. Save model architecture as JSON; friend loads architecture and trains from scratch
Solution
Step 1: Identify saving weights only method
Use model.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 with load_weights().
Final Answer:
Save weights with model.save_weights(); friend recreates model architecture then loads weights -> Option A
Quick Check:
Save weights only + recreate model to load weights [OK]
Hint: Save weights only, friend rebuilds model then loads weights [OK]
Common Mistakes:
Thinking weights can be loaded without model code
Confusing full model save with weights only
Assuming JSON architecture saves weights
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.