3. Given this TensorFlow training step code, what will be printed?
import tensorflow as tf
model = tf.keras.Sequential([tf.keras.layers.Dense(1)])
optimizer = tf.keras.optimizers.SGD(learning_rate=0.1)
x = tf.constant([[1.0]])
y = tf.constant([[2.0]])
with tf.GradientTape() as tape:
prediction = model(x)
loss = tf.reduce_mean((y - prediction) ** 2)
grads = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(grads, model.trainable_variables))
print(loss.numpy())
medium
A. A negative number
B. Zero
C. A positive number close to 1.0
D. An error because of missing input
Solution
Step 1: Understand the loss calculation
Loss is mean squared error between prediction and target; initially weights are random, so loss is positive.
Step 2: Check if loss can be zero or negative
Loss is squared difference, so cannot be negative or zero at first step.
Final Answer:
A positive number close to 1.0 -> Option C
Quick Check:
Initial loss positive ~1.0 = A [OK]
Hint: Initial loss is positive because weights start random [OK]
Common Mistakes:
Expecting zero loss before training
Thinking loss can be negative
Assuming code throws error
4. This TensorFlow code tries to update model weights but does not change them. What is the error?
import tensorflow as tf
model = tf.keras.Sequential([tf.keras.layers.Dense(1)])
optimizer = tf.keras.optimizers.Adam(learning_rate=0.01)
x = tf.constant([[1.0]])
y = tf.constant([[2.0]])
with tf.GradientTape() as tape:
prediction = model(x)
loss = tf.reduce_mean((y - prediction) ** 2)
grads = tape.gradient(loss, model.trainable_variables)
# Missing apply_gradients call here
print(model.trainable_variables[0].numpy())
medium
A. The optimizer is not applied to update weights
B. GradientTape is used incorrectly
C. Loss calculation is wrong
D. Model layers are not defined
Solution
Step 1: Check if optimizer updates weights
The code calculates gradients but never calls apply_gradients, so weights stay the same.
Step 2: Verify other parts are correct
GradientTape and loss calculation are correct; model layers exist.
Final Answer:
The optimizer is not applied to update weights -> Option A
Quick Check:
Missing apply_gradients means no weight update = C [OK]
Hint: Always call apply_gradients to update weights [OK]
Common Mistakes:
Forgetting to apply gradients
Thinking GradientTape updates weights
Assuming loss error stops training
5. You want to train a TensorFlow model to predict house prices. Why is it important that the training process updates the model's weights using an optimizer and loss function?
hard
A. Because updating weights makes the training run faster without changing predictions
B. Because updating weights helps the model learn patterns from data to make better predictions
C. Because updating weights changes the input features to match the output
D. Because updating weights increases the model size to handle more data
Solution
Step 1: Understand the role of weights in prediction
Weights control how input features affect the output prediction in the model.
Step 2: Explain why updating weights matters
Updating weights using optimizer and loss reduces prediction errors by learning from data patterns.
Step 3: Eliminate incorrect options
Weights do not increase model size, change inputs, or only speed training without improving predictions.
Final Answer:
Because updating weights helps the model learn patterns from data to make better predictions -> Option B
Quick Check:
Weight updates improve prediction accuracy = D [OK]
Hint: Weights learn data patterns to improve predictions [OK]