TensorFlow Saving and Loading Models 1 β Questions and Answers
Question 1: Which method saves a Keras model in HDF5 format?
- model.save('model.h5') (Correct answer)
- model.export('model.h5')
- model.serialize('model.h5')
- model.write('model.h5')
Correct answer: model.save('model.h5')
Calling model.save() with an .h5 extension tells Keras to serialize the model in HDF5 format, storing architecture, weights, and optimizer state.
Question 2: Which function is used to reload a previously saved Keras model?
- tf.keras.models.load_model() (Correct answer)
- tf.keras.models.restore()
- tf.keras.models.import_model()
- tf.keras.models.deserialize()
Correct answer: tf.keras.models.load_model()
tf.keras.models.load_model() is the standard API for loading a saved Keras model and reconstructing its architecture, weights, and optimizer.
Question 3: What does the SavedModel format store that HDF5 does not?
- Only the optimizer state
- Only the model weights
- The full TensorFlow computation graph and serving signatures (Correct answer)
- Only the model architecture as JSON
Correct answer: The full TensorFlow computation graph and serving signatures
SavedModel persists the complete TensorFlow graph (including traced tf.function endpoints and serving signatures) alongside weights, making it suitable for serving without Python.
Question 4: Which TensorFlow class is the modern API for saving and restoring model variables as checkpoints?
- tf.train.Checkpoint (Correct answer)
- tf.train.Saver
- tf.train.ModelSaver
- tf.train.VariableStore
Correct answer: tf.train.Checkpoint
tf.train.Checkpoint is the TensorFlow 2.x mechanism for tracking and checkpointing variables, replacing the legacy tf.train.Saver.
Question 5: Which Keras callback automatically saves the model during training at configurable intervals?
- tf.keras.callbacks.ModelCheckpoint (Correct answer)
- tf.keras.callbacks.SaveModel
- tf.keras.callbacks.CheckpointSaver
- tf.keras.callbacks.TrainingSnapshot
Correct answer: tf.keras.callbacks.ModelCheckpoint
tf.keras.callbacks.ModelCheckpoint monitors training and saves the model (or just its weights) whenever a specified condition is met, such as improvement in validation loss.
Question 6: Which method saves only a model's weight values without the architecture or optimizer?
- model.save_weights() (Correct answer)
- model.export_weights()
- model.write_weights()
- model.checkpoint_weights()
Correct answer: model.save_weights()
model.save_weights() serializes only the weight tensors, which is useful when you want to transfer weights to a separately defined model.
Question 7: What file extensions does TensorFlow create for checkpoint index and data files respectively?
- .ckpt.index and .ckpt.data-00000-of-00001 (Correct answer)
- .checkpoint and .checkpointdata
- .tfstate.index and .tfstate.data
- .weights.index and .weights.data
Correct answer: .ckpt.index and .ckpt.data-00000-of-00001
TensorFlow checkpoints consist of a .ckpt.index file (variable name map) and one or more .ckpt.data shards (actual tensor values).
Which method saves a Keras model in HDF5 format?