0
0
TensorFlowml~20 mins

Why training optimizes model weights in TensorFlow - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Training Optimization
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do we update model weights during training?

Imagine you are teaching a robot to recognize apples and oranges. Why do we change the robot's internal settings (weights) during training?

ATo make the robot guess randomly each time
BTo keep the robot's settings fixed so it doesn't forget
CTo help the robot improve its guesses by learning from mistakes
DTo make the robot slower at recognizing fruits
Attempts:
2 left
💡 Hint

Think about how learning from errors helps improve skills.

Predict Output
intermediate
2:00remaining
What is the loss value after one training step?

Given a simple model and one training step, what is the loss value printed?

TensorFlow
import tensorflow as tf

# Simple linear model y = wx + b
model = tf.keras.Sequential([tf.keras.layers.Dense(1, input_shape=(1,))])

# Mean squared error loss
loss_fn = tf.keras.losses.MeanSquaredError()

# Optimizer
optimizer = tf.keras.optimizers.SGD(learning_rate=0.1)

# Input and true output
x = tf.constant([[1.0]])
y_true = tf.constant([[2.0]])

with tf.GradientTape() as tape:
    y_pred = model(x)
    loss = loss_fn(y_true, y_pred)

grads = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(grads, model.trainable_variables))

print(round(float(loss), 3))
A4.0
B1.0
C0.25
D0.0
Attempts:
2 left
💡 Hint

Check the initial prediction before weight update.

Model Choice
advanced
2:00remaining
Which model architecture best learns complex patterns?

You want to train a model to recognize handwritten digits with many details. Which model type is best to learn complex patterns?

AA single-layer linear model
BA deep neural network with multiple layers
CA simple decision tree with depth 1
DA model that always predicts the most common digit
Attempts:
2 left
💡 Hint

Think about which model can capture complex features.

Metrics
advanced
2:00remaining
Which metric shows model improvement during training?

During training, which metric should decrease to show the model is learning better?

ATraining loss
BTraining accuracy
CNumber of epochs
DBatch size
Attempts:
2 left
💡 Hint

Think about what measures error size.

🔧 Debug
expert
2:00remaining
Why does this training code not update weights?

Look at this TensorFlow training code. Why do the model weights not change after training?

TensorFlow
import tensorflow as tf

model = tf.keras.Sequential([tf.keras.layers.Dense(1, input_shape=(1,))])
optimizer = tf.keras.optimizers.SGD(learning_rate=0.1)
loss_fn = tf.keras.losses.MeanSquaredError()

x = tf.constant([[1.0]])
y_true = tf.constant([[2.0]])

with tf.GradientTape() as tape:
    y_pred = model(x)
    loss = loss_fn(y_true, y_pred)

gradients = tape.gradient(loss, model.trainable_variables)
# Missing optimizer.apply_gradients call here

print(model.trainable_variables[0].numpy())
AThe model has no trainable variables
BThe loss function is incorrect, so gradients are zero
CThe input data x is constant, so no update happens
DThe optimizer.apply_gradients() call is missing, so weights are not updated
Attempts:
2 left
💡 Hint

Check if the code applies gradients to update weights.