tf.contrib.learn
Estimator
batch_size
input_fn
TensorFlow

setting batch_size when using input_fn for tf.contrib.learn.Estimator

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

In legacy TensorFlow tf.contrib.learn code, batch size is controlled in input_fn, not in most estimator constructor arguments. This often confuses teams reading mixed TensorFlow one and TensorFlow two examples. Correct setup requires explicit batching in input pipelines and separate train and eval input behavior.

Where Batch Size Actually Lives

For tf.contrib.learn.Estimator, batch size is usually applied in queue ops or tf.data pipelines created in input_fn.

That means changing batch size usually means editing input_fn, not estimator config.

Queue-Based Legacy Pattern

Older graph pipelines used queue runners and tf.train.batch.

python
1import tensorflow as tf
2
3def input_fn_train_queue():
4    features = tf.constant([[1.0], [2.0], [3.0], [4.0]])
5    labels = tf.constant([0, 1, 0, 1])
6
7    x_batch, y_batch = tf.train.batch([features, labels], batch_size=2, capacity=10)
8    return {"x": x_batch}, y_batch

This works, but is harder to maintain and debug.

Prefer Dataset-Based Input Function in Later One X Code

A cleaner one x approach is tf.data with explicit batch.

python
1import tensorflow as tf
2
3def make_input_fn(features, labels, batch_size, num_epochs=None, shuffle=True):
4    def _input_fn():
5        ds = tf.data.Dataset.from_tensor_slices(({"x": features}, labels))
6        if shuffle:
7            ds = ds.shuffle(buffer_size=len(features))
8        ds = ds.repeat(num_epochs).batch(batch_size)
9        return ds
10    return _input_fn

Batch size is now obvious and parameterized.

Separate Train and Eval Batch Policies

Training and evaluation usually require different settings.

python
train_input_fn = make_input_fn(train_x, train_y, batch_size=64, num_epochs=None, shuffle=True)
eval_input_fn = make_input_fn(eval_x, eval_y, batch_size=256, num_epochs=1, shuffle=False)

Larger eval batches often improve throughput when memory allows.

Batch Size and Training Dynamics

Batch size impacts:

  • Steps per epoch.
  • Gradient noise.
  • Memory usage.
  • Hardware utilization.

When changing batch size, also review learning rate and step counts. A larger batch with unchanged learning rate may change convergence behavior.

Add Pipeline Performance Enhancements

In tf.data input functions, add prefetch to reduce stalls.

python
1def make_fast_input_fn(features, labels, batch_size):
2    def _input_fn():
3        ds = tf.data.Dataset.from_tensor_slices(({"x": features}, labels))
4        ds = ds.shuffle(len(features)).repeat().batch(batch_size).prefetch(1)
5        return ds
6    return _input_fn

Input pipeline throughput can be as important as model graph speed.

Debug Batch Behavior Explicitly

Unexpected shape errors often come from missing repeat, wrong batch stage, or accidental eval reuse of train function.

Add lightweight diagnostics during early runs:

  • Print first batch shapes.
  • Check steps until input exhaustion.
  • Confirm shuffle and repeat settings per mode.

Small checks save substantial debugging time.

Guard Against Deprecated API Drift

tf.contrib.learn is deprecated. Legacy systems still run it, but long-term maintenance is easier after migration to tf.estimator or Keras training loops.

If migration is not immediate, isolate legacy input functions in one module and test them heavily.

Practical Tuning Workflow

A practical batch tuning process:

  1. Start from stable baseline batch size.
  2. Increase gradually while monitoring memory.
  3. Track steps per second and eval metrics.
  4. Adjust learning rate when batch size changes materially.
  5. Keep one tuning log for reproducibility.

Measured tuning beats ad hoc trial changes.

Keep Reproducibility in Batch Experiments

When comparing batch sizes, fix random seeds and data split so results are comparable across runs.

python
1import numpy as np
2import tensorflow as tf
3np.random.seed(42)
4tf.compat.v1.set_random_seed(42)

Without reproducibility controls, batch-size conclusions can be distorted by run-to-run noise.

Common Pitfalls

  • Expecting estimator constructor to control batch size automatically.
  • Reusing one input function for train and eval without policy differences.
  • Forgetting repeat and hitting premature input exhaustion.
  • Increasing batch size until out-of-memory failures occur.
  • Copying deprecated contrib snippets into modern TensorFlow projects.

Summary

  • In contrib estimator pipelines, batch size is set in input_fn.
  • Use explicit dataset batching for readable and controllable behavior.
  • Keep train and eval input functions separate.
  • Tune batch size together with learning rate and memory constraints.
  • Plan migration away from deprecated contrib APIs.

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
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
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