TensorFlow Saving and Loading Models 2 β Questions and Answers
Question 1: What is the default save format when calling model.save('my_model') without a file extension in TensorFlow 2.x?
- HDF5 format
- SavedModel directory format (Correct answer)
- Pickle format
- Protocol Buffer frozen graph
Correct answer: SavedModel directory format
In TensorFlow 2.x, omitting a file extension causes model.save() to default to SavedModel format, creating a directory with assets, variables, and a saved_model.pb file.
Question 2: Which ModelCheckpoint argument ensures only the best-performing checkpoint is kept on disk?
- save_best_only=True (Correct answer)
- best_only=True
- monitor_best=True
- keep_best=True
Correct answer: save_best_only=True
Setting save_best_only=True in ModelCheckpoint overwrites the checkpoint only when the monitored metric improves, keeping disk usage minimal.
Question 3: What is the primary role of tf.train.CheckpointManager?
- Optimizing checkpoint write speed
- Limiting the number of checkpoints kept with max_to_keep (Correct answer)
- Distributing checkpoints across GPUs
- Validating checkpoint file integrity
Correct answer: Limiting the number of checkpoints kept with max_to_keep
tf.train.CheckpointManager wraps a tf.train.Checkpoint and enforces a max_to_keep limit, automatically deleting older checkpoints.
Question 4: Which method on tf.train.Checkpoint restores saved variable values?
- .restore() (Correct answer)
- .load()
- .import_variables()
- .recover()
Correct answer: .restore()
Calling .restore(checkpoint_path) on a tf.train.Checkpoint object reassigns saved tensor values back to the tracked variables.
Question 5: What does the save_freq parameter in ModelCheckpoint control?
- The learning rate at which weights are saved
- How often (in epochs or batches) a checkpoint is written (Correct answer)
- The compression ratio of saved files
- How many total checkpoints are allowed
Correct answer: How often (in epochs or batches) a checkpoint is written
save_freq accepts 'epoch' to save after each epoch, or an integer to save every N batches, giving fine-grained control over checkpoint cadence.
Question 6: What must be true about a model before calling model.load_weights() successfully?
- It must use the identical optimizer
- Its layer shapes must match those of the saved weights (Correct answer)
- It must have been trained for the same number of epochs
- It must use the same batch size
Correct answer: Its layer shapes must match those of the saved weights
load_weights() maps saved tensors to layers by name and shape, so any mismatch in layer dimensions raises an incompatible shape error.
Question 7: Which low-level TensorFlow function explicitly saves a model in SavedModel format?
- tf.saved_model.save() (Correct answer)
- tf.saved_model.export()
- tf.saved_model.convert()
- tf.saved_model.serialize()
Correct answer: tf.saved_model.save()
tf.saved_model.save(model, export_dir) is the low-level API that writes the computation graph and variables to a SavedModel directory.
What is the default save format when calling model.save('my_model') without a file extension in TensorFlow 2.x?