Skip to main content
Fanout
Estimator API
Curriculum overview

TensorFlow Fundamentals · lesson 21/27

Estimator API

tf.estimator was TensorFlow's high-level API before Keras became default. It wraps the train/evaluate/predict loop around a model_fn and an input_fn, and it handles checkpoints, summaries, and distribution for you. It is legacy now, but reading it teaches the separation of data, model, and training loop that Keras still follows.

The idea

An Estimator has three user-supplied pieces:

  • input_fn — returns features and labels as tensors, built on tf.data.
  • model_fn — builds the graph and returns a tf.estimator.EstimatorSpec with a mode, loss, train op, and predictions.
  • train / evaluate / predict — drive the loop and write checkpoints into model_dir.

For a plain feedforward classifier you rarely write model_fn yourself; tf.estimator.DNNClassifier ships one. Use tf.estimator.Estimator(model_fn=...) only for custom models.

The Keras equivalents map cleanly:

EstimatorKeras
input_fntf.data.Dataset passed to fit
model_fn + EstimatorSpeckeras.Model or keras.Sequential
estimator.train()model.fit()
estimator.evaluate()model.evaluate()
estimator.predict()model.predict()
model_dirModelCheckpoint callback

Worked example

Iris: 4 numeric features, 3 classes.

  • feature_columns = [tf.feature_column.numeric_column("x", shape=[4])].
  • DNNClassifier(hidden_units=[10, 10], n_classes=3).
  • input_fn builds tf.data.Dataset.from_tensor_slices(({"x": x}, y)).batch(32).

train writes checkpoint and saved_model.pb files under model_dir; evaluate restores the latest checkpoint automatically, so no path juggling is needed between runs.

In code

import tensorflow as tf

# Legacy Estimator
fc = [tf.feature_column.numeric_column("x", shape=[4])]
est = tf.estimator.DNNClassifier(hidden_units=[10, 10], feature_columns=fc,
                                 n_classes=3, model_dir="/tmp/iris_est")

def input_fn(x, y, batch=32):
    return tf.data.Dataset.from_tensor_slices(({"x": x}, y)).batch(batch)

est.train(lambda: input_fn(train_x, train_y), steps=500)
print(est.evaluate(lambda: input_fn(test_x, test_y))["accuracy"])

# Keras equivalent
model = tf.keras.Sequential([
    tf.keras.layers.Dense(10, activation="relu", input_shape=(4,)),
    tf.keras.layers.Dense(10, activation="relu"),
    tf.keras.layers.Dense(3, activation="softmax"),
])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy",
              metrics=["accuracy"])
model.fit(train_x, train_y, batch_size=32, epochs=50)

Check yourself

  1. Which Estimator responsibilities have no direct Keras counterpart, and why?
  2. What does model_dir handle that a bare Keras fit does not?
  3. Why is tf.estimator considered legacy, and what replaced it?

Key takeaways

  • Estimators split a model into input_fn, model_fn, and a managed train/eval loop.
  • model_dir gives checkpointing and export for free, which Keras does through callbacks.
  • New code should use Keras; read Estimator code as the historical API contract.