TensorFlow Advanced TensorFlow Techniques 1 β Questions and Answers
Question 1: What is the purpose of tf.keras.Model subclassing?
- Creating custom model architectures with arbitrary forward pass logic (Correct answer)
- Subclassing pre-trained models for fine-tuning
- Extending Keras with C++ layers
- Inheriting optimizer behavior
Correct answer: Creating custom model architectures with arbitrary forward pass logic
Model subclassing lets you define the forward pass in a call() method, enabling architectures with conditional logic, loops, and multiple paths.
Question 2: How do you create a custom Keras layer in TensorFlow?
- Subclass tf.keras.layers.Layer and implement build() and call() (Correct answer)
- Define a function and wrap with tf.keras.layer()
- Add layers using tf.custom_layer()
- Use tf.keras.layers.Lambda()
Correct answer: Subclass tf.keras.layers.Layer and implement build() and call()
Custom layers inherit from tf.keras.layers.Layer; build() creates weights and call() defines the computation.
Question 3: What does tf.Variable differ from tf.constant?
- Variables are mutable and track gradients; constants are immutable (Correct answer)
- Variables are stored on CPU; constants on GPU
- Variables hold integers; constants hold floats
- Variables are shared across models; constants are local
Correct answer: Variables are mutable and track gradients; constants are immutable
tf.Variable holds mutable state whose values can be updated during training, while tf.constant creates an immutable tensor.
Question 4: Which TensorFlow module provides pre-built reusable model components called modules?
- TensorFlow Hub (Correct answer)
- TensorFlow Zoo
- TF Model Garden only
- Keras Applications only
Correct answer: TensorFlow Hub
TensorFlow Hub hosts pre-trained models and reusable SavedModel components that can be downloaded and integrated into custom models.
Question 5: What is the role of tf.keras.losses.Reduction in custom loss functions?
- Specifies how per-sample losses are aggregated (sum, mean, or none) (Correct answer)
- Controls weight reduction during pruning
- Defines gradient reduction strategy
- Sets learning rate reduction behavior
Correct answer: Specifies how per-sample losses are aggregated (sum, mean, or none)
The Reduction enum controls whether the loss over a batch is summed, averaged, or left as a per-sample array.
Question 6: What is tf.keras.layers.Lambda used for?
- Wrapping arbitrary Python/TF expressions as a Keras layer (Correct answer)
- Creating lambda functions in TF graphs
- Defining anonymous loss functions
- Applying regularization functions
Correct answer: Wrapping arbitrary Python/TF expressions as a Keras layer
Lambda layers allow any TensorFlow expression or Python function to be used as a layer without writing a full custom layer class.
What is the purpose of tf.keras.Model subclassing?