In [0]:
#@title Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
This document introduces tf.estimator
—a high-level TensorFlow
API. Estimators encapsulate the following actions:
You may either use the pre-made Estimators we provide or write your
own custom Estimators. All Estimators—whether pre-made or custom—are
classes based on the tf.estimator.Estimator
class.
For a quick example try Estimator tutorials. For an overview of the API design, see the white paper.
Similar to a tf.keras.Model
, an estimator
is a model-level abstraction. The tf.estimator
provides some capabilities currently still under development for tf.keras
. These are:
Estimators provide the following benefits:
When writing an application with Estimators, you must separate the data input pipeline from the model. This separation simplifies experiments with different data sets.
Pre-made Estimators enable you to work at a much higher conceptual level than the base TensorFlow APIs. You no longer have to worry about creating the computational graph or sessions since Estimators handle all the "plumbing" for you. Furthermore, pre-made Estimators let you experiment with different model architectures by making only minimal code changes. tf.estimator.DNNClassifier
, for example, is a pre-made Estimator class that trains classification models based on dense, feed-forward neural networks.
A TensorFlow program relying on a pre-made Estimator typically consists of the following four steps:
For example, you might create one function to import the training set and another function to import the test set. Each dataset importing function must return two objects:
For example, the following code illustrates the basic skeleton for an input function:
def input_fn(dataset):
... # manipulate dataset, extracting the feature dict and the label
return feature_dict, label
See data guide for details.
Each tf.feature_column
identifies a feature name, its type, and any input pre-processing. For example, the following snippet creates three feature columns that hold integer or floating-point data. The first two feature columns simply identify the feature's name and type. The third feature column also specifies a lambda the program will invoke to scale the raw data:
# Define three numeric feature columns.
population = tf.feature_column.numeric_column('population')
crime_rate = tf.feature_column.numeric_column('crime_rate')
median_education = tf.feature_column.numeric_column(
'median_education',
normalizer_fn=lambda x: x - global_education_mean)
For further information, see the feature columns tutorial.
For example, here's a sample instantiation of a pre-made Estimator named LinearClassifier
:
# Instantiate an estimator, passing the feature columns.
estimator = tf.estimator.LinearClassifier(
feature_columns=[population, crime_rate, median_education])
For further information, see the linear classifier tutorial.
For example, all Estimators provide a train
method, which trains a model.
# `input_fn` is the function created in Step 1
estimator.train(input_fn=my_training_set, steps=2000)
You can see an example of this below.
Pre-made Estimators encode best practices, providing the following benefits:
If you don't use pre-made Estimators, you must implement the preceding features yourself.
The heart of every Estimator—whether pre-made or custom—is its model function, which is a method that builds graphs for training, evaluation, and prediction. When you are using a pre-made Estimator, someone else has already implemented the model function. When relying on a custom Estimator, you must write the model function yourself.
In [0]:
import tensorflow as tf
In [0]:
import tensorflow_datasets as tfds
tfds.disable_progress_bar()
You can convert existing Keras models to Estimators with tf.keras.estimator.model_to_estimator
. Doing so enables your Keras
model to access Estimator's strengths, such as distributed training.
Instantiate a Keras MobileNet V2 model and compile the model with the optimizer, loss, and metrics to train with:
In [0]:
keras_mobilenet_v2 = tf.keras.applications.MobileNetV2(
input_shape=(160, 160, 3), include_top=False)
keras_mobilenet_v2.trainable = False
estimator_model = tf.keras.Sequential([
keras_mobilenet_v2,
tf.keras.layers.GlobalAveragePooling2D(),
tf.keras.layers.Dense(1)
])
# Compile the model
estimator_model.compile(
optimizer='adam',
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=['accuracy'])
Create an Estimator
from the compiled Keras model. The initial model state of the Keras model is preserved in the created Estimator
:
In [0]:
est_mobilenet_v2 = tf.keras.estimator.model_to_estimator(keras_model=estimator_model)
Treat the derived Estimator
as you would with any other Estimator
.
In [0]:
IMG_SIZE = 160 # All images will be resized to 160x160
def preprocess(image, label):
image = tf.cast(image, tf.float32)
image = (image/127.5) - 1
image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
return image, label
In [0]:
def train_input_fn(batch_size):
data = tfds.load('cats_vs_dogs', as_supervised=True)
train_data = data['train']
train_data = train_data.map(preprocess).shuffle(500).batch(batch_size)
return train_data
To train, call Estimator's train function:
In [0]:
est_mobilenet_v2.train(input_fn=lambda: train_input_fn(32), steps=500)
Similarly, to evaluate, call the Estimator's evaluate function:
In [0]:
est_mobilenet_v2.evaluate(input_fn=lambda: train_input_fn(32), steps=10)
For more details, please refer to the documentation for tf.keras.estimator.model_to_estimator
.