Challenge - 5 Problems
Custom Training Step Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of a simple custom training step
What will be the printed loss value after running this custom training step once on a batch of data?
TensorFlow
import tensorflow as tf class SimpleModel(tf.keras.Model): def __init__(self): super().__init__() self.dense = tf.keras.layers.Dense(1) def call(self, inputs): return self.dense(inputs) @tf.function def train_step(model, inputs, targets, optimizer): with tf.GradientTape() as tape: predictions = model(inputs) loss = tf.reduce_mean(tf.square(targets - predictions)) gradients = tape.gradient(loss, model.trainable_variables) optimizer.apply_gradients(zip(gradients, model.trainable_variables)) return loss model = SimpleModel() optimizer = tf.keras.optimizers.SGD(learning_rate=0.1) inputs = tf.constant([[1.0], [2.0], [3.0]]) targets = tf.constant([[2.0], [4.0], [6.0]]) loss_value = train_step(model, inputs, targets, optimizer) print(float(loss_value))
Attempts:
2 left
💡 Hint
Think about the initial random weights and how the mean squared error is calculated.
✗ Incorrect
The model starts with random weights, so predictions are initially far from targets. The mean squared error loss is calculated as the average of squared differences. Given the inputs and targets, the initial loss is approximately 4.6666665.
❓ Model Choice
intermediateChoosing the correct custom training step for classification
Which custom training step correctly implements a single training iteration for a binary classification model using sigmoid activation and binary cross-entropy loss?
Attempts:
2 left
💡 Hint
Remember that sigmoid_cross_entropy_with_logits expects raw logits, not probabilities.
✗ Incorrect
Option A correctly uses raw logits from the model and applies tf.nn.sigmoid_cross_entropy_with_logits, which expects logits and labels. Option A and C apply sigmoid before loss, which is incorrect for this function. Option A uses predictions as logits but predictions are already sigmoid outputs, causing wrong loss calculation.
❓ Hyperparameter
advancedEffect of learning rate in a custom training step
If you increase the learning rate too much in a custom training step using SGD optimizer, what is the most likely effect on training?
Attempts:
2 left
💡 Hint
Think about what happens when steps are too large in gradient descent.
✗ Incorrect
A very high learning rate causes the optimizer to take too large steps, overshooting minima. This leads to oscillations or divergence in loss, preventing convergence.
🔧 Debug
advancedIdentify the error in this custom training step
What error will this custom training step code raise when executed?
TensorFlow
def train_step(model, inputs, targets, optimizer): with tf.GradientTape() as tape: predictions = model(inputs) loss = tf.reduce_mean(tf.square(targets - predictions)) grads = tape.gradient(loss) optimizer.apply_gradients(zip(grads, model.trainable_variables)) return loss
Attempts:
2 left
💡 Hint
Check the arguments passed to tape.gradient and what it returns.
✗ Incorrect
The tape.gradient call is missing the variables to compute gradients with respect to, so it returns None. Passing None to zip causes optimizer.apply_gradients to raise ValueError about no gradients provided.
🧠 Conceptual
expertWhy use a custom training step instead of model.fit?
Which is the best reason to implement a custom training step in TensorFlow instead of using the built-in model.fit method?
Attempts:
2 left
💡 Hint
Think about flexibility and customization needs in training.
✗ Incorrect
Custom training steps let you control every detail of training, such as applying different losses, multiple optimizers, or complex logic. model.fit is simpler but less flexible. model.fit supports GPU and backpropagation internally. Custom steps do not guarantee accuracy improvements automatically.
