TensorFlow Model Training and Optimization 2 — Questions and Answers
Question 1: What does the tf.function decorator do in TensorFlow 2?
- Compiles a Python function into a TensorFlow graph for faster execution (Correct answer)
- Marks a function as deprecated
- Wraps a function for distributed training
- Saves a function to disk
Correct answer: Compiles a Python function into a TensorFlow graph for faster execution
tf.function traces the Python function and compiles it into a static computation graph, enabling performance optimizations like XLA.
Question 2: What is the purpose of model.evaluate() in Keras?
- Computes loss and metrics on test data without updating weights (Correct answer)
- Validates model architecture
- Fine-tunes model weights
- Generates predictions
Correct answer: Computes loss and metrics on test data without updating weights
model.evaluate() runs forward passes on provided data and returns the configured loss and metric values.
Question 3: Which regularization technique randomly drops neurons during forward pass?
- Dropout (Correct answer)
- L1 regularization
- L2 regularization
- Batch normalization
Correct answer: Dropout
Dropout randomly sets a fraction of neuron outputs to zero during training, forcing the network to learn redundant representations.
Question 4: What is the purpose of the ModelCheckpoint callback?
- Saves the model at specified intervals or when performance improves (Correct answer)
- Checkpoints GPU memory
- Validates model at each epoch
- Saves training logs
Correct answer: Saves the model at specified intervals or when performance improves
ModelCheckpoint saves model weights (or the full model) periodically, allowing recovery from the best or most recent state.
Question 5: What does class_weight parameter in model.fit() do?
- Assigns higher loss weight to underrepresented classes (Correct answer)
- Sets weight initializers per class
- Controls per-class learning rate
- Freezes specific class layers
Correct answer: Assigns higher loss weight to underrepresented classes
class_weight applies a multiplier to the loss for each class, helping the model pay more attention to minority classes in imbalanced datasets.
Question 6: What is the purpose of the TensorBoard callback in Keras?
- Logs training metrics for visualization in TensorBoard (Correct answer)
- Runs TensorBoard server automatically
- Exports model to TensorBoard format
- Profiles GPU usage
Correct answer: Logs training metrics for visualization in TensorBoard
The TensorBoard callback writes logs (loss, metrics, histograms, images) to a directory that TensorBoard can read and visualize.
What does the tf.function decorator do in TensorFlow 2?