C.W.K.
Stream
Lesson 03 of 06 · published

Full Manual Loop

~9 min · custom-train

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

When you own the loop

A full manual loop is the second rung: you stop letting fit() drive and write the epoch and batch iteration yourself. This buys total control over loop structure — you can interleave evaluation, branch on a custom schedule, or run anything between batches — but you also inherit nothing. No progress bar, no callbacks, no checkpointing, no distribution. Every convenience becomes a line you write.

The skeleton

The shape is always the same: outer loop over epochs, inner loop over the dataset, and inside each step a forward pass, a loss, a gradient computation, and an optimizer update. The forward-and-loss part is portable, but the gradient step is backend-native — the example below uses TensorFlow's tf.GradientTape. On PyTorch you'd call loss.backward(); on JAX you'd transform with jax.grad. That single block is the whole reason there's no one true backend-agnostic loop.

Code

Manual training loop (TensorFlow backend)·python
import tensorflow as tf
import keras

optimizer = keras.optimizers.Adam(1e-3)
loss_fn = keras.losses.SparseCategoricalCrossentropy()

for epoch in range(10):
    for step, (x_batch, y_batch) in enumerate(train_dataset):
        with tf.GradientTape() as tape:
            # Forward pass with gradient tracking
            y_pred = model(x_batch, training=True)
            loss = loss_fn(y_batch, y_pred)

        # Compute gradients and update weights (TF-native)
        grads = tape.gradient(loss, model.trainable_variables)
        optimizer.apply_gradients(
            zip(grads, model.trainable_variables)
        )

    print(f"Epoch {epoch}, Loss: {float(loss):.4f}")

External links

Exercise

Write a full manual training loop (no fit) for MNIST. Include validation pass and best-model save. Compare to your fit-based version — note where you spent effort that fit gave you for free.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.