Model Pipeline - Why training optimizes model weights
This pipeline shows how training helps a model learn by adjusting its weights to make better predictions over time.
Jump into concepts and practice - no test required
This pipeline shows how training helps a model learn by adjusting its weights to make better predictions over time.
Loss
0.7 |*
0.6 | *
0.5 | *
0.4 | *
0.3 | *
+---------
1 2 3 4 5
Epochs| Epoch | Loss ↓ | Accuracy ↑ | Observation |
|---|---|---|---|
| 1 | 0.65 | 0.60 | Model starts with random weights; loss is high, accuracy low |
| 2 | 0.50 | 0.70 | Weights adjust; loss decreases, accuracy improves |
| 3 | 0.40 | 0.78 | Model learns patterns; better predictions |
| 4 | 0.35 | 0.82 | Loss keeps decreasing; accuracy rises |
| 5 | 0.30 | 0.85 | Training converges; model weights optimized |
apply_gradients directly updates weights using gradients.compile sets training config, fit runs training loop, and tf.Variable creates variables but does not update weights.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())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())apply_gradients, so weights stay the same.