Path: blob/master/site/en-snapshot/guide/migrate/migrating_estimator.ipynb
25118 views
Copyright 2021 The TensorFlow Authors.
Migrate from Estimator to Keras APIs
This guide demonstrates how to migrate from TensorFlow 1's tf.estimator.Estimator
APIs to TensorFlow 2's tf.keras
APIs. First, you will set up and run a basic model for training and evaluation with tf.estimator.Estimator
. Then, you will perform the equivalent steps in TensorFlow 2 with the tf.keras
APIs. You will also learn how to customize the training step by subclassing tf.keras.Model
and using tf.GradientTape
.
In TensorFlow 1, the high-level
tf.estimator.Estimator
APIs let you train and evaluate a model, as well as perform inference and save your model (for serving).In TensorFlow 2, use the Keras APIs to perform the aforementioned tasks, such as model building, gradient application, training, evaluation, and prediction.
(For migrating model/checkpoint saving workflows to TensorFlow 2, check out the SavedModel and Checkpoint migration guides.)
Setup
Start with imports and a simple dataset:
TensorFlow 1: Train and evaluate with tf.estimator.Estimator
This example shows how to perform training and evaluation with tf.estimator.Estimator
in TensorFlow 1.
Start by defining a few functions: an input function for the training data, an evaluation input function for the evaluation data, and a model function that tells the Estimator
how the training op is defined with the features and labels:
Instantiate your Estimator
, and train the model:
Evaluate the program with the evaluation set:
TensorFlow 2: Train and evaluate with the built-in Keras methods
This example demonstrates how to perform training and evaluation with Keras Model.fit
and Model.evaluate
in TensorFlow 2. (You can learn more in the Training and evaluation with the built-in methods guide.)
Start by preparing the dataset pipeline with the
tf.data.Dataset
APIs.Define a simple Keras Sequential model with one linear (
tf.keras.layers.Dense
) layer.Instantiate an Adagrad optimizer (
tf.keras.optimizers.Adagrad
).Configure the model for training by passing the
optimizer
variable and the mean-squared error ("mse"
) loss toModel.compile
.
With that, you are ready to train the model by calling Model.fit
:
Finally, evaluate the model with Model.evaluate
:
TensorFlow 2: Train and evaluate with a custom training step and built-in Keras methods
In TensorFlow 2, you can also write your own custom training step function with tf.GradientTape
to perform forward and backward passes, while still taking advantage of the built-in training support, such as tf.keras.callbacks.Callback
and tf.distribute.Strategy
. (Learn more in Customizing what happens in Model.fit and Writing custom training loops from scratch.)
In this example, start by creating a custom tf.keras.Model
by subclassing tf.keras.Sequential
that overrides Model.train_step
. (Learn more about subclassing tf.keras.Model). Inside that class, define a custom train_step
function that for each batch of data performs a forward pass and backward pass during one training step.
Next, as before:
Prepare the dataset pipeline with
tf.data.Dataset
.Define a simple model with one
tf.keras.layers.Dense
layer.Instantiate Adagrad (
tf.keras.optimizers.Adagrad
)Configure the model for training with
Model.compile
, while using mean-squared error ("mse"
) as the loss function.
Call Model.fit
to train the model:
And, finally, evaluate the program with Model.evaluate
:
Next steps
Additional Keras resources you may find useful:
The following guides can assist with migrating distribution strategy workflows from tf.estimator
APIs: