keras
train_on_batch
machine learning
neural networks
model training

What does train_on_batch do in keras model?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

train_on_batch() is a low-level Keras training API that runs one gradient-update step on one batch of data. It sits below model.fit() and is useful when you need manual control over batching, custom loops, or interactions with external systems during training.

What train_on_batch() Actually Does

When you call model.train_on_batch(x, y), Keras performs the same core work it would do inside fit() for a single batch:

  • Run a forward pass.
  • Compute the loss.
  • Compute gradients.
  • Apply the optimizer update.
  • Return the loss, and optionally metric values.

That means train_on_batch() is not a separate training algorithm. It is one explicit training step.

Here is a minimal example:

python
1import numpy as np
2import tensorflow as tf
3
4x = np.array([[0.0], [1.0], [2.0], [3.0]], dtype=np.float32)
5y = np.array([[0.0], [1.0], [1.0], [1.0]], dtype=np.float32)
6
7model = tf.keras.Sequential(
8    [
9        tf.keras.layers.Dense(4, activation="relu", input_shape=(1,)),
10        tf.keras.layers.Dense(1, activation="sigmoid"),
11    ]
12)
13
14model.compile(
15    optimizer="adam",
16    loss="binary_crossentropy",
17    metrics=["accuracy"],
18)
19
20loss, accuracy = model.train_on_batch(x, y)
21print(loss, accuracy)

That single call updates the model once using exactly those four samples as one batch.

How It Differs From fit()

fit() manages the full training loop for you. It iterates over epochs, batches, callbacks, validation, and progress tracking. train_on_batch() does not. You are responsible for the loop.

For example:

python
1import numpy as np
2import tensorflow as tf
3
4x = np.random.randn(100, 2).astype("float32")
5y = (x[:, 0] + x[:, 1] > 0).astype("float32")
6
7model = tf.keras.Sequential(
8    [
9        tf.keras.layers.Input(shape=(2,)),
10        tf.keras.layers.Dense(8, activation="relu"),
11        tf.keras.layers.Dense(1, activation="sigmoid"),
12    ]
13)
14
15model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
16
17batch_size = 20
18for epoch in range(5):
19    for start in range(0, len(x), batch_size):
20        end = start + batch_size
21        metrics = model.train_on_batch(x[start:end], y[start:end])
22    print(f"epoch={epoch} metrics={metrics}")

This gives you full control over what happens between batches. That is the main reason to use it.

When train_on_batch() Is Useful

Typical use cases include:

  • Reinforcement learning loops.
  • GAN training where generator and discriminator are updated separately.
  • Online learning or streaming data.
  • Training pipelines that fetch data from custom sources outside Keras datasets.
  • Experiments where you need precise per-batch logging or interventions.

If none of those apply, fit() is usually simpler and less error-prone.

What It Returns

The return value depends on the compiled model:

  • If the model has only a loss, you get one scalar.
  • If the model includes metrics, you get the loss plus metric values.
  • The order follows model.metrics_names.

Example:

python
print(model.metrics_names)
result = model.train_on_batch(x[:20], y[:20])
print(result)

Checking metrics_names avoids guessing which number corresponds to which metric.

Be Careful With Metrics State

Keras metrics are stateful across batches unless reset. If you run manual loops, make sure you understand whether you want per-batch numbers or aggregate numbers across an epoch.

A simple pattern is:

python
1for epoch in range(3):
2    model.reset_metrics()
3    for start in range(0, len(x), batch_size):
4        end = start + batch_size
5        model.train_on_batch(x[start:end], y[start:end])
6
7    print(dict(zip(model.metrics_names, model.evaluate(x, y, verbose=0))))

That separates training updates from end-of-epoch evaluation and reduces confusion.

Common Pitfalls

  • Expecting train_on_batch() to handle epochs, shuffling, callbacks, and validation automatically.
  • Forgetting that one call means one optimizer update, not a whole training run.
  • Misreading the returned list because metrics_names was not checked.
  • Using train_on_batch() where fit() would be simpler and easier to maintain.
  • Ignoring metric state and drawing the wrong conclusion from batch-level outputs.

Summary

  • 'train_on_batch() performs one training step on one batch.'
  • It gives manual control over batching and training flow.
  • It is useful for custom or non-standard training loops.
  • 'fit() is usually the better default when you want ordinary supervised training.'
  • Always check model.metrics_names and manage metric state carefully in manual loops.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.