An estimator for TensorFlow Linear and DNN joined models for regression.
Inherits From: Estimator, Estimator
Input of train and evaluate should have following features, otherwise there will be a KeyError:
Loss is calculated by using mean squared error.
Estimators can be used while eager execution is enabled. Note that input_fn and all hooks are executed inside a graph context, so they have to be written to be compatible with graph mode. Note that input_fn code using tf.data generally works in both graph and eager modes.
View source
Shows the directory name where evaluation metrics are dumped.
View source
Evaluates the model given evaluation data input_fn.
For each step, calls input_fn, which returns one batch of data. Evaluates until:
View source
Exports a SavedModel with tf.MetaGraphDefs for each requested mode.
For each mode passed in via the input_receiver_fn_map, this method builds a new graph by calling the input_receiver_fn to obtain feature and label Tensors. Next, this method calls the Estimator's model_fn in the passed mode to generate the model graph based on those features and labels, and restores the given checkpoint (or, lacking that, the most recent checkpoint) into the graph. Only one of the modes is used for saving variables to the SavedModel (order of preference: tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL, then tf.estimator.ModeKeys.PREDICT), such that up to three tf.MetaGraphDefs are saved with a single set of variables in a single SavedModel directory.
For the variables and tf.MetaGraphDefs, a timestamped export directory below export_dir_base, and writes a SavedModel into it containing the tf.MetaGraphDef for the given mode and its associated signatures.
For prediction, the exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
For training and evaluation, the train_op is stored in an extra collection, and loss, metrics, and predictions are included in a SignatureDef for the mode in question.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
View source
Exports inference graph as a SavedModel into the given dir.
For a detailed guide, see SavedModel from Estimators.
This method builds a new graph by first calling the serving_input_receiver_fn to obtain feature Tensors, and then calling this Estimator's model_fn to generate the model graph based on those features. It restores the given checkpoint (or, lacking that, the most recent checkpoint) into this graph in a fresh session. Finally it creates a timestamped export directory below the given export_dir_base, and writes a SavedModel into it containing a single tf.MetaGraphDef saved from this session.
The exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
The experimental_mode parameter can be used to export a single train/eval/predict graph as a SavedModel. See experimental_export_all_saved_models for full docs.
View source
Returns list of all variable names in this model.
View source
Returns value of the variable given by name.
View source
Finds the filename of the latest saved checkpoint file in model_dir.
View source
Yields predictions for given features.
Please note that interleaving two predict outputs does not work. See: issue/20506
View source
Trains a model given training data input_fn.


A regressor for TensorFlow DNN models.
Inherits From: Estimator, Estimator
Input of train and evaluate should have following features, otherwise there will be a KeyError:
Loss is calculated by using mean squared error.
Estimators can be used while eager execution is enabled. Note that input_fn and all hooks are executed inside a graph context, so they have to be written to be compatible with graph mode. Note that input_fn code using tf.data generally works in both graph and eager modes.
View source
Shows the directory name where evaluation metrics are dumped.
View source
Evaluates the model given evaluation data input_fn.
For each step, calls input_fn, which returns one batch of data. Evaluates until:
View source
Exports a SavedModel with tf.MetaGraphDefs for each requested mode.
For each mode passed in via the input_receiver_fn_map, this method builds a new graph by calling the input_receiver_fn to obtain feature and label Tensors. Next, this method calls the Estimator's model_fn in the passed mode to generate the model graph based on those features and labels, and restores the given checkpoint (or, lacking that, the most recent checkpoint) into the graph. Only one of the modes is used for saving variables to the SavedModel (order of preference: tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL, then tf.estimator.ModeKeys.PREDICT), such that up to three tf.MetaGraphDefs are saved with a single set of variables in a single SavedModel directory.
For the variables and tf.MetaGraphDefs, a timestamped export directory below export_dir_base, and writes a SavedModel into it containing the tf.MetaGraphDef for the given mode and its associated signatures.
For prediction, the exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
For training and evaluation, the train_op is stored in an extra collection, and loss, metrics, and predictions are included in a SignatureDef for the mode in question.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
View source
Exports inference graph as a SavedModel into the given dir.
For a detailed guide, see SavedModel from Estimators.
This method builds a new graph by first calling the serving_input_receiver_fn to obtain feature Tensors, and then calling this Estimator's model_fn to generate the model graph based on those features. It restores the given checkpoint (or, lacking that, the most recent checkpoint) into this graph in a fresh session. Finally it creates a timestamped export directory below the given export_dir_base, and writes a SavedModel into it containing a single tf.MetaGraphDef saved from this session.
The exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
The experimental_mode parameter can be used to export a single train/eval/predict graph as a SavedModel. See experimental_export_all_saved_models for full docs.
View source
Returns list of all variable names in this model.
View source
Returns value of the variable given by name.
View source
Finds the filename of the latest saved checkpoint file in model_dir.
View source
Yields predictions for given features.
Please note that interleaving two predict outputs does not work. See: issue/20506
View source
Trains a model given training data input_fn.


Estimator class to train and evaluate TensorFlow models.
Inherits From: Estimator
The Estimator object wraps a model which is specified by a model_fn, which, given inputs and a number of other parameters, returns the ops necessary to perform training, evaluation, or predictions.
All outputs (checkpoints, event files, etc.) are written to model_dir, or a subdirectory thereof. If model_dir is not set, a temporary directory is used.
The config argument can be passed tf.estimator.RunConfig object containing information about the execution environment. It is passed on to the model_fn, if the model_fn has a parameter named "config" (and input functions in the same manner). If the config parameter is not passed, it is instantiated by the Estimator. Not passing config means that defaults useful for local execution are used. Estimator makes config available to the model (for instance, to allow specialization based on the number of workers available), and also uses some of its fields to control internals, especially regarding checkpointing.
The params argument contains hyperparameters. It is passed to the model_fn, if the model_fn has a parameter named "params", and to the input functions in the same manner. Estimator only passes params along, it does not inspect it. The structure of params is therefore entirely up to the developer.
None of Estimator's methods can be overridden in subclasses (its constructor enforces this). Subclasses should use model_fn to configure the base class, and may add methods implementing specialized functionality.
See estimators for more information.
To warm-start an Estimator:
For more details on warm-start configuration, see tf.estimator.WarmStartSettings.
Calling methods of Estimator will work while eager execution is enabled. However, the model_fn and input_fn is not executed eagerly, Estimator will switch to graph mode before calling all user-provided functions (incl. hooks), so their code has to be compatible with graph mode execution. Note that input_fn code using tf.data generally works in both graph and eager modes.
View source
Shows the directory name where evaluation metrics are dumped.
View source
Evaluates the model given evaluation data input_fn.
For each step, calls input_fn, which returns one batch of data. Evaluates until:
View source
Exports a SavedModel with tf.MetaGraphDefs for each requested mode.
For each mode passed in via the input_receiver_fn_map, this method builds a new graph by calling the input_receiver_fn to obtain feature and label Tensors. Next, this method calls the Estimator's model_fn in the passed mode to generate the model graph based on those features and labels, and restores the given checkpoint (or, lacking that, the most recent checkpoint) into the graph. Only one of the modes is used for saving variables to the SavedModel (order of preference: tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL, then tf.estimator.ModeKeys.PREDICT), such that up to three tf.MetaGraphDefs are saved with a single set of variables in a single SavedModel directory.
For the variables and tf.MetaGraphDefs, a timestamped export directory below export_dir_base, and writes a SavedModel into it containing the tf.MetaGraphDef for the given mode and its associated signatures.
For prediction, the exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
For training and evaluation, the train_op is stored in an extra collection, and loss, metrics, and predictions are included in a SignatureDef for the mode in question.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
View source
Exports inference graph as a SavedModel into the given dir.
For a detailed guide, see SavedModel from Estimators.
This method builds a new graph by first calling the serving_input_receiver_fn to obtain feature Tensors, and then calling this Estimator's model_fn to generate the model graph based on those features. It restores the given checkpoint (or, lacking that, the most recent checkpoint) into this graph in a fresh session. Finally it creates a timestamped export directory below the given export_dir_base, and writes a SavedModel into it containing a single tf.MetaGraphDef saved from this session.
The exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
The experimental_mode parameter can be used to export a single train/eval/predict graph as a SavedModel. See experimental_export_all_saved_models for full docs.
View source
Returns list of all variable names in this model.
View source
Returns value of the variable given by name.
View source
Finds the filename of the latest saved checkpoint file in model_dir.
View source
Yields predictions for given features.
Please note that interleaving two predict outputs does not work. See: issue/20506
View source
Trains a model given training data input_fn.


Ops and objects returned from a model_fn and passed to an Estimator.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.EstimatorSpec
EstimatorSpec fully defines the model to be run by an Estimator.


Configuration for the "eval" part for the train_and_evaluate call.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.EvalSpec
EvalSpec combines details of evaluation of the trained model as well as its export. Evaluation consists of computing metrics to judge the performance of the trained model. Export writes out the trained model on to external storage.


Public API for tf.estimator.experimental namespace.
class InMemoryEvaluatorHook: Hook to run evaluation in training without a checkpoint.
class LinearSDCA: Stochastic Dual Coordinate Ascent helper for linear estimators.
class RNNClassifier: A classifier for TensorFlow RNN models.
class RNNEstimator: An Estimator for TensorFlow RNN models with user-specified head.
build_raw_supervised_input_receiver_fn(...): Build a supervised_input_receiver_fn for raw features and labels.
call_logit_fn(...): Calls logit_fn (experimental).
make_early_stopping_hook(...): Creates early-stopping hook.
make_stop_at_checkpoint_step_hook(...): Creates a proper StopAtCheckpointStepHook based on chief status.
stop_if_higher_hook(...): Creates hook to stop if the given metric is higher than the threshold.
stop_if_lower_hook(...): Creates hook to stop if the given metric is lower than the threshold.
stop_if_no_decrease_hook(...): Creates hook to stop if metric does not decrease within given max steps.
stop_if_no_increase_hook(...): Creates hook to stop if metric does not increase within given max steps.


Build a supervised_input_receiver_fn for raw features and labels.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.experimental.build_raw_supervised_input_receiver_fn
This function wraps tensor placeholders in a supervised_receiver_fn with the expectation that the features and labels appear precisely as the model_fn expects them. Features and labels can therefore be dicts of tensors, or raw tensors.


Calls logit_fn (experimental).
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.experimental.call_logit_fn
THIS FUNCTION IS EXPERIMENTAL. Keras layers/models are the recommended APIs for logit and model composition.
A utility function that calls the provided logit_fn with the relevant subset of provided arguments. Similar to tf.estimator._call_model_fn().


Hook to run evaluation in training without a checkpoint.
Inherits From: SessionRunHook
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.experimental.InMemoryEvaluatorHook
Current limitations of this approach are:
View source
Does first run which shows the eval metrics before training.
View source
Runs evaluator.
View source
Called before each call to run().
You can return from this call a SessionRunArgs object indicating ops or tensors to add to the upcoming run() call. These ops/tensors will be run together with the ops/tensors originally passed to the original run() call. The run args you return can also contain feeds to be added to the run() call.
The run_context argument is a SessionRunContext that provides information about the upcoming run() call: the originally requested op/tensors, the TensorFlow Session.
At this point graph is finalized and you can not add ops.
View source
Build eval graph and restoring op.
View source
Runs evaluator for final model.


Stochastic Dual Coordinate Ascent helper for linear estimators.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.experimental.LinearSDCA
Objects of this class are intended to be provided as the optimizer argument (though LinearSDCA objects do not implement the tf.train.Optimizer interface) when creating tf.estimator.LinearClassifier or tf.estimator.LinearRegressor.
SDCA can only be used with LinearClassifier and LinearRegressor under the following conditions:
Here the expectation is that the input_fn_* functions passed to train and evaluate return a pair (dict, label_tensor) where dict has example_id_column as key whose value is a Tensor of shape [batch_size] and dtype string. num_loss_partitions defines sigma' in eq (11) of [3]. Convergence of (global) loss is guaranteed if num_loss_partitions is larger or equal to the product (#concurrent train ops/per worker) x (#workers). Larger values for num_loss_partitions lead to slower convergence. The recommended value for num_loss_partitions in tf.estimator (where currently there is one process per worker) is the number of workers running the train steps. It defaults to 1 (single machine). num_table_shards defines the number of shards for the internal state table, typically set to match the number of parameter servers for large data sets.
The SDCA algorithm was originally introduced in [1] and it was followed by the L1 proximal step [2], a distributed version [3] and adaptive sampling [4]. [1] www.jmlr.org/papers/volume14/shalev-shwartz13a/shalev-shwartz13a.pdf [2] https://arxiv.org/pdf/1309.2375.pdf [3] https://arxiv.org/pdf/1502.03508.pdf [4] https://arxiv.org/pdf/1502.08053.pdf Details specific to this implementation are provided in: https://github.com/tensorflow/estimator/tree/master/tensorflow_estimator/python/estimator/canned/linear_optimizer/doc/sdca.ipynb
View source
Returns the training operation of an SdcaModel optimizer.


Creates early-stopping hook.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.experimental.make_early_stopping_hook
Returns a SessionRunHook that stops training when should_stop_fn returns True.
Caveat: Current implementation supports early-stopping both training and evaluation in local mode. In distributed mode, training can be stopped but evaluation (where it's a separate job) will indefinitely wait for new model checkpoints to evaluate, so you will need other means to detect and stop it. Early-stopping evaluation in distributed mode requires changes in train_and_evaluate API and will be addressed in a future revision.


Creates a proper StopAtCheckpointStepHook based on chief status.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.experimental.make_stop_at_checkpoint_step_hook


A classifier for TensorFlow RNN models.
Inherits From: RNNEstimator, Estimator
Trains a recurrent neural network model to classify instances into one of multiple classes.
Input of train and evaluate should have following features, otherwise there will be a KeyError:
Loss is calculated by using softmax cross entropy.
Estimators are not compatible with eager execution.
View source
Shows the directory name where evaluation metrics are dumped.
View source
Evaluates the model given evaluation data input_fn.
For each step, calls input_fn, which returns one batch of data. Evaluates until:
View source
Exports a SavedModel with tf.MetaGraphDefs for each requested mode.
For each mode passed in via the input_receiver_fn_map, this method builds a new graph by calling the input_receiver_fn to obtain feature and label Tensors. Next, this method calls the Estimator's model_fn in the passed mode to generate the model graph based on those features and labels, and restores the given checkpoint (or, lacking that, the most recent checkpoint) into the graph. Only one of the modes is used for saving variables to the SavedModel (order of preference: tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL, then tf.estimator.ModeKeys.PREDICT), such that up to three tf.MetaGraphDefs are saved with a single set of variables in a single SavedModel directory.
For the variables and tf.MetaGraphDefs, a timestamped export directory below export_dir_base, and writes a SavedModel into it containing the tf.MetaGraphDef for the given mode and its associated signatures.
For prediction, the exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
For training and evaluation, the train_op is stored in an extra collection, and loss, metrics, and predictions are included in a SignatureDef for the mode in question.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
View source
Exports inference graph as a SavedModel into the given dir.
For a detailed guide, see SavedModel from Estimators.
This method builds a new graph by first calling the serving_input_receiver_fn to obtain feature Tensors, and then calling this Estimator's model_fn to generate the model graph based on those features. It restores the given checkpoint (or, lacking that, the most recent checkpoint) into this graph in a fresh session. Finally it creates a timestamped export directory below the given export_dir_base, and writes a SavedModel into it containing a single tf.MetaGraphDef saved from this session.
The exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
The experimental_mode parameter can be used to export a single train/eval/predict graph as a SavedModel. See experimental_export_all_saved_models for full docs.
View source
Exports inference graph as a SavedModel into the given dir. (deprecated)
For a detailed guide, see SavedModel from Estimators.
This method builds a new graph by first calling the serving_input_receiver_fn to obtain feature Tensors, and then calling this Estimator's model_fn to generate the model graph based on those features. It restores the given checkpoint (or, lacking that, the most recent checkpoint) into this graph in a fresh session. Finally it creates a timestamped export directory below the given export_dir_base, and writes a SavedModel into it containing a single tf.MetaGraphDef saved from this session.
The exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
View source
Returns list of all variable names in this model.
View source
Returns value of the variable given by name.
View source
Finds the filename of the latest saved checkpoint file in model_dir.
View source
Yields predictions for given features.
Please note that interleaving two predict outputs does not work. See: issue/20506
View source
Trains a model given training data input_fn.


An Estimator for TensorFlow RNN models with user-specified head.
Inherits From: Estimator
Input of train and evaluate should have following features, otherwise there will be a KeyError:
Loss and predicted output are determined by the specified head.
Estimators are not compatible with eager execution.
View source
Shows the directory name where evaluation metrics are dumped.
View source
Evaluates the model given evaluation data input_fn.
For each step, calls input_fn, which returns one batch of data. Evaluates until:
View source
Exports a SavedModel with tf.MetaGraphDefs for each requested mode.
For each mode passed in via the input_receiver_fn_map, this method builds a new graph by calling the input_receiver_fn to obtain feature and label Tensors. Next, this method calls the Estimator's model_fn in the passed mode to generate the model graph based on those features and labels, and restores the given checkpoint (or, lacking that, the most recent checkpoint) into the graph. Only one of the modes is used for saving variables to the SavedModel (order of preference: tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL, then tf.estimator.ModeKeys.PREDICT), such that up to three tf.MetaGraphDefs are saved with a single set of variables in a single SavedModel directory.
For the variables and tf.MetaGraphDefs, a timestamped export directory below export_dir_base, and writes a SavedModel into it containing the tf.MetaGraphDef for the given mode and its associated signatures.
For prediction, the exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
For training and evaluation, the train_op is stored in an extra collection, and loss, metrics, and predictions are included in a SignatureDef for the mode in question.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
View source
Exports inference graph as a SavedModel into the given dir.
For a detailed guide, see SavedModel from Estimators.
This method builds a new graph by first calling the serving_input_receiver_fn to obtain feature Tensors, and then calling this Estimator's model_fn to generate the model graph based on those features. It restores the given checkpoint (or, lacking that, the most recent checkpoint) into this graph in a fresh session. Finally it creates a timestamped export directory below the given export_dir_base, and writes a SavedModel into it containing a single tf.MetaGraphDef saved from this session.
The exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
The experimental_mode parameter can be used to export a single train/eval/predict graph as a SavedModel. See experimental_export_all_saved_models for full docs.
View source
Exports inference graph as a SavedModel into the given dir. (deprecated)
For a detailed guide, see SavedModel from Estimators.
This method builds a new graph by first calling the serving_input_receiver_fn to obtain feature Tensors, and then calling this Estimator's model_fn to generate the model graph based on those features. It restores the given checkpoint (or, lacking that, the most recent checkpoint) into this graph in a fresh session. Finally it creates a timestamped export directory below the given export_dir_base, and writes a SavedModel into it containing a single tf.MetaGraphDef saved from this session.
The exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
View source
Returns list of all variable names in this model.
View source
Returns value of the variable given by name.
View source
Finds the filename of the latest saved checkpoint file in model_dir.
View source
Yields predictions for given features.
Please note that interleaving two predict outputs does not work. See: issue/20506
View source
Trains a model given training data input_fn.


Creates hook to stop if the given metric is higher than the threshold.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.experimental.stop_if_higher_hook
Caveat: Current implementation supports early-stopping both training and evaluation in local mode. In distributed mode, training can be stopped but evaluation (where it's a separate job) will indefinitely wait for new model checkpoints to evaluate, so you will need other means to detect and stop it. Early-stopping evaluation in distributed mode requires changes in train_and_evaluate API and will be addressed in a future revision.


Creates hook to stop if the given metric is lower than the threshold.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.experimental.stop_if_lower_hook
Caveat: Current implementation supports early-stopping both training and evaluation in local mode. In distributed mode, training can be stopped but evaluation (where it's a separate job) will indefinitely wait for new model checkpoints to evaluate, so you will need other means to detect and stop it. Early-stopping evaluation in distributed mode requires changes in train_and_evaluate API and will be addressed in a future revision.


Creates hook to stop if metric does not decrease within given max steps.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.experimental.stop_if_no_decrease_hook
Caveat: Current implementation supports early-stopping both training and evaluation in local mode. In distributed mode, training can be stopped but evaluation (where it's a separate job) will indefinitely wait for new model checkpoints to evaluate, so you will need other means to detect and stop it. Early-stopping evaluation in distributed mode requires changes in train_and_evaluate API and will be addressed in a future revision.


Creates hook to stop if metric does not increase within given max steps.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.experimental.stop_if_no_increase_hook
Caveat: Current implementation supports early-stopping both training and evaluation in local mode. In distributed mode, training can be stopped but evaluation (where it's a separate job) will indefinitely wait for new model checkpoints to evaluate, so you will need other means to detect and stop it. Early-stopping evaluation in distributed mode requires changes in train_and_evaluate API and will be addressed in a future revision.


All public utility methods for exporting Estimator to SavedModel.
This file includes functions and constants from core (model_utils) and export.py
class ClassificationOutput: Represents the output of a classification head.
class ExportOutput: Represents an output of a model that can be served.
class PredictOutput: Represents the output of a generic prediction head.
class RegressionOutput: Represents the output of a regression head.
class ServingInputReceiver: A return type for a serving_input_receiver_fn.
class TensorServingInputReceiver: A return type for a serving_input_receiver_fn.
build_parsing_serving_input_receiver_fn(...): Build a serving_input_receiver_fn expecting fed tf.Examples.
build_raw_serving_input_receiver_fn(...): Build a serving_input_receiver_fn expecting feature Tensors.


Build a serving_input_receiver_fn expecting fed tf.Examples.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.export.build_parsing_serving_input_receiver_fn
Creates a serving_input_receiver_fn that expects a serialized tf.Example fed into a string placeholder. The function parses the tf.Example according to the provided feature_spec, and returns all parsed Tensors as features.


Build a serving_input_receiver_fn expecting feature Tensors.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.export.build_raw_serving_input_receiver_fn
Creates an serving_input_receiver_fn that expects all features to be fed directly.


Represents the output of a classification head.
Inherits From: ExportOutput
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.export.ClassificationOutput
Either classes or scores or both must be set.
The classes Tensor must provide string labels, not integer class IDs.
If only classes is set, it is interpreted as providing top-k results in descending order.
If only scores is set, it is interpreted as providing a score for every class in order of class ID.
If both classes and scores are set, they are interpreted as zipped, so each score corresponds to the class at the same index. Clients should not depend on the order of the entries.
View source
Generate a SignatureDef proto for inclusion in a MetaGraphDef.
The SignatureDef will specify outputs as described in this ExportOutput, and will use the provided receiver_tensors as inputs.


Represents an output of a model that can be served.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.export.ExportOutput
These typically correspond to model heads.
View source
Generate a SignatureDef proto for inclusion in a MetaGraphDef.
The SignatureDef will specify outputs as described in this ExportOutput, and will use the provided receiver_tensors as inputs.


Represents the output of a generic prediction head.
Inherits From: ExportOutput
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.export.PredictOutput
A generic prediction need not be either a classification or a regression.
Named outputs must be provided as a dict from string to Tensor,
View source
Generate a SignatureDef proto for inclusion in a MetaGraphDef.
The SignatureDef will specify outputs as described in this ExportOutput, and will use the provided receiver_tensors as inputs.


Represents the output of a regression head.
Inherits From: ExportOutput
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.export.RegressionOutput
View source
Generate a SignatureDef proto for inclusion in a MetaGraphDef.
The SignatureDef will specify outputs as described in this ExportOutput, and will use the provided receiver_tensors as inputs.


A return type for a serving_input_receiver_fn.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.export.ServingInputReceiver


A return type for a serving_input_receiver_fn.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.export.TensorServingInputReceiver
This is for use with models that expect a single Tensor or SparseTensor as an input feature, as opposed to a dict of features.
The normal ServingInputReceiver always returns a feature dict, even if it contains only one entry, and so can be used only with models that accept such a dict. For models that accept only a single raw feature, the serving_input_receiver_fn provided to Estimator.export_saved_model() should return this TensorServingInputReceiver instead. See: https://github.com/tensorflow/tensorflow/issues/11674
Note that the receiver_tensors and receiver_tensor_alternatives arguments will be automatically converted to the dict representation in either case, because the SavedModel format requires each input Tensor to have a name (provided by the dict key).


A class representing a type of model export.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.Exporter
A directory name under the export base directory where exports of this type are written. Should not be None nor empty.
View source
Exports the given Estimator to a specific format.


Runs feed_fn and sets the feed_dict accordingly.
Inherits From: SessionRunHook
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.FeedFnHook, tf.compat.v1.train.FeedFnHook
View source
Called when new TensorFlow session is created.
This is called to signal the hooks that a new session has been created. This has two essential differences with the situation in which begin is called:
View source
Called after each call to run().
The run_values argument contains results of requested ops/tensors by before_run().
The run_context argument is the same one send to before_run call. run_context.request_stop() can be called to stop the iteration.
If session.run() raises any exceptions then after_run() is not called.
View source
Called before each call to run().
You can return from this call a SessionRunArgs object indicating ops or tensors to add to the upcoming run() call. These ops/tensors will be run together with the ops/tensors originally passed to the original run() call. The run args you return can also contain feeds to be added to the run() call.
The run_context argument is a SessionRunContext that provides information about the upcoming run() call: the originally requested op/tensors, the TensorFlow Session.
At this point graph is finalized and you can not add ops.
View source
Called once before using the session.
When called, the default graph is the one that will be launched in the session. The hook can modify the graph by adding new operations to it. After the begin() call the graph will be finalized and the other callbacks can not modify the graph anymore. Second call of begin() on the same graph, should not change the graph.
View source
Called at the end of session.
The session argument can be used in case the hook wants to run final ops, such as saving a last checkpoint.
If session.run() raises exception other than OutOfRangeError or StopIteration then end() is not called. Note the difference between end() and after_run() behavior when session.run() raises OutOfRangeError or StopIteration. In that case end() is called but after_run() is not called.


This class exports the serving graph and checkpoints at the end.
Inherits From: Exporter
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.FinalExporter
This class performs a single export at the end of training.
A directory name under the export base directory where exports of this type are written. Should not be None nor empty.
View source
Exports the given Estimator to a specific format.


A hook which evaluates Tensors at the end of a session.
Inherits From: SessionRunHook
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.FinalOpsHook, tf.compat.v1.train.FinalOpsHook
View source
Called when new TensorFlow session is created.
This is called to signal the hooks that a new session has been created. This has two essential differences with the situation in which begin is called:
View source
Called after each call to run().
The run_values argument contains results of requested ops/tensors by before_run().
The run_context argument is the same one send to before_run call. run_context.request_stop() can be called to stop the iteration.
If session.run() raises any exceptions then after_run() is not called.
View source
Called before each call to run().
You can return from this call a SessionRunArgs object indicating ops or tensors to add to the upcoming run() call. These ops/tensors will be run together with the ops/tensors originally passed to the original run() call. The run args you return can also contain feeds to be added to the run() call.
The run_context argument is a SessionRunContext that provides information about the upcoming run() call: the originally requested op/tensors, the TensorFlow Session.
At this point graph is finalized and you can not add ops.
View source
Called once before using the session.
When called, the default graph is the one that will be launched in the session. The hook can modify the graph by adding new operations to it. After the begin() call the graph will be finalized and the other callbacks can not modify the graph anymore. Second call of begin() on the same graph, should not change the graph.
View source
Called at the end of session.
The session argument can be used in case the hook wants to run final ops, such as saving a last checkpoint.
If session.run() raises exception other than OutOfRangeError or StopIteration then end() is not called. Note the difference between end() and after_run() behavior when session.run() raises OutOfRangeError or StopIteration. In that case end() is called but after_run() is not called.


Delays execution until global step reaches wait_until_step.
Inherits From: SessionRunHook
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.GlobalStepWaiterHook, tf.compat.v1.train.GlobalStepWaiterHook
This hook delays execution until global step reaches to wait_until_step. It is used to gradually start workers in distributed settings. One example usage would be setting wait_until_step=int(K*log(task_id+1)) assuming that task_id=0 is the chief.
View source
Called when new TensorFlow session is created.
This is called to signal the hooks that a new session has been created. This has two essential differences with the situation in which begin is called:
View source
Called after each call to run().
The run_values argument contains results of requested ops/tensors by before_run().
The run_context argument is the same one send to before_run call. run_context.request_stop() can be called to stop the iteration.
If session.run() raises any exceptions then after_run() is not called.
View source
Called before each call to run().
You can return from this call a SessionRunArgs object indicating ops or tensors to add to the upcoming run() call. These ops/tensors will be run together with the ops/tensors originally passed to the original run() call. The run args you return can also contain feeds to be added to the run() call.
The run_context argument is a SessionRunContext that provides information about the upcoming run() call: the originally requested op/tensors, the TensorFlow Session.
At this point graph is finalized and you can not add ops.
View source
Called once before using the session.
When called, the default graph is the one that will be launched in the session. The hook can modify the graph by adding new operations to it. After the begin() call the graph will be finalized and the other callbacks can not modify the graph anymore. Second call of begin() on the same graph, should not change the graph.
View source
Called at the end of session.
The session argument can be used in case the hook wants to run final ops, such as saving a last checkpoint.
If session.run() raises exception other than OutOfRangeError or StopIteration then end() is not called. Note the difference between end() and after_run() behavior when session.run() raises OutOfRangeError or StopIteration. In that case end() is called but after_run() is not called.


Interface for the head/top of a model.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.Head
Head sits on top of the model network and handles computing the outputs of the network. Given logits (or output of a hidden layer), a Head knows how to compute predictions, loss, train_op, metrics and export outputs. It is meant to:
Here is simplified model_fn to build a DNN regression model.
Often is the number of classes, labels, or real values to be predicted. Typically, logits is of shape [batch_size, logits_dimension].
Describes how to reduce training loss over batch, such as mean or sum.
View source
Returns EstimatorSpec that a model_fn can return.
It is recommended to pass all args via name.
View source
Returns a loss Tensor from provided arguments.
Note that, the args of features and mode are most likely not used, but some Head implementations may require them.
View source
Returns a dict of metric objects.
View source
Returns a dict of predictions from provided logits.
View source
Updates metric objects and returns a dict of the updated metrics.


This class regularly exports the serving graph and checkpoints.
Inherits From: Exporter
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.LatestExporter
In addition to exporting, this class also garbage collects stale exports.
A directory name under the export base directory where exports of this type are written. Should not be None nor empty.
View source
Exports the given Estimator to a specific format.


Linear classifier model.
Inherits From: Estimator, Estimator
Train a linear model to classify instances into one of multiple possible classes. When number of possible classes is 2, this is binary classification.
Input of train and evaluate should have following features, otherwise there will be a KeyError:
Loss is calculated by using softmax cross entropy.
Estimators can be used while eager execution is enabled. Note that input_fn and all hooks are executed inside a graph context, so they have to be written to be compatible with graph mode. Note that input_fn code using tf.data generally works in both graph and eager modes.
View source
Shows the directory name where evaluation metrics are dumped.
View source
Evaluates the model given evaluation data input_fn.
For each step, calls input_fn, which returns one batch of data. Evaluates until:
View source
Exports a SavedModel with tf.MetaGraphDefs for each requested mode.
For each mode passed in via the input_receiver_fn_map, this method builds a new graph by calling the input_receiver_fn to obtain feature and label Tensors. Next, this method calls the Estimator's model_fn in the passed mode to generate the model graph based on those features and labels, and restores the given checkpoint (or, lacking that, the most recent checkpoint) into the graph. Only one of the modes is used for saving variables to the SavedModel (order of preference: tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL, then tf.estimator.ModeKeys.PREDICT), such that up to three tf.MetaGraphDefs are saved with a single set of variables in a single SavedModel directory.
For the variables and tf.MetaGraphDefs, a timestamped export directory below export_dir_base, and writes a SavedModel into it containing the tf.MetaGraphDef for the given mode and its associated signatures.
For prediction, the exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
For training and evaluation, the train_op is stored in an extra collection, and loss, metrics, and predictions are included in a SignatureDef for the mode in question.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
View source
Exports inference graph as a SavedModel into the given dir.
For a detailed guide, see SavedModel from Estimators.
This method builds a new graph by first calling the serving_input_receiver_fn to obtain feature Tensors, and then calling this Estimator's model_fn to generate the model graph based on those features. It restores the given checkpoint (or, lacking that, the most recent checkpoint) into this graph in a fresh session. Finally it creates a timestamped export directory below the given export_dir_base, and writes a SavedModel into it containing a single tf.MetaGraphDef saved from this session.
The exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
The experimental_mode parameter can be used to export a single train/eval/predict graph as a SavedModel. See experimental_export_all_saved_models for full docs.
View source
Returns list of all variable names in this model.
View source
Returns value of the variable given by name.
View source
Finds the filename of the latest saved checkpoint file in model_dir.
View source
Yields predictions for given features.
Please note that interleaving two predict outputs does not work. See: issue/20506
View source
Trains a model given training data input_fn.


An estimator for TensorFlow linear models with user-specified head.
Inherits From: Estimator, Estimator
Input of train and evaluate should have following features, otherwise there will be a KeyError:
Loss and predicted output are determined by the specified head.
Estimators can be used while eager execution is enabled. Note that input_fn and all hooks are executed inside a graph context, so they have to be written to be compatible with graph mode. Note that input_fn code using tf.data generally works in both graph and eager modes.
View source
Shows the directory name where evaluation metrics are dumped.
View source
Evaluates the model given evaluation data input_fn.
For each step, calls input_fn, which returns one batch of data. Evaluates until:
View source
Exports a SavedModel with tf.MetaGraphDefs for each requested mode.
For each mode passed in via the input_receiver_fn_map, this method builds a new graph by calling the input_receiver_fn to obtain feature and label Tensors. Next, this method calls the Estimator's model_fn in the passed mode to generate the model graph based on those features and labels, and restores the given checkpoint (or, lacking that, the most recent checkpoint) into the graph. Only one of the modes is used for saving variables to the SavedModel (order of preference: tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL, then tf.estimator.ModeKeys.PREDICT), such that up to three tf.MetaGraphDefs are saved with a single set of variables in a single SavedModel directory.
For the variables and tf.MetaGraphDefs, a timestamped export directory below export_dir_base, and writes a SavedModel into it containing the tf.MetaGraphDef for the given mode and its associated signatures.
For prediction, the exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
For training and evaluation, the train_op is stored in an extra collection, and loss, metrics, and predictions are included in a SignatureDef for the mode in question.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
View source
Exports inference graph as a SavedModel into the given dir.
For a detailed guide, see SavedModel from Estimators.
This method builds a new graph by first calling the serving_input_receiver_fn to obtain feature Tensors, and then calling this Estimator's model_fn to generate the model graph based on those features. It restores the given checkpoint (or, lacking that, the most recent checkpoint) into this graph in a fresh session. Finally it creates a timestamped export directory below the given export_dir_base, and writes a SavedModel into it containing a single tf.MetaGraphDef saved from this session.
The exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
The experimental_mode parameter can be used to export a single train/eval/predict graph as a SavedModel. See experimental_export_all_saved_models for full docs.
View source
Returns list of all variable names in this model.
View source
Returns value of the variable given by name.
View source
Finds the filename of the latest saved checkpoint file in model_dir.
View source
Yields predictions for given features.
Please note that interleaving two predict outputs does not work. See: issue/20506
View source
Trains a model given training data input_fn.


An estimator for TensorFlow Linear regression problems.
Inherits From: Estimator, Estimator
Train a linear regression model to predict label value given observation of feature values.
Input of train and evaluate should have following features, otherwise there will be a KeyError:
Loss is calculated by using mean squared error.
Estimators can be used while eager execution is enabled. Note that input_fn and all hooks are executed inside a graph context, so they have to be written to be compatible with graph mode. Note that input_fn code using tf.data generally works in both graph and eager modes.
View source
Shows the directory name where evaluation metrics are dumped.
View source
Evaluates the model given evaluation data input_fn.
For each step, calls input_fn, which returns one batch of data. Evaluates until:
View source
Exports a SavedModel with tf.MetaGraphDefs for each requested mode.
For each mode passed in via the input_receiver_fn_map, this method builds a new graph by calling the input_receiver_fn to obtain feature and label Tensors. Next, this method calls the Estimator's model_fn in the passed mode to generate the model graph based on those features and labels, and restores the given checkpoint (or, lacking that, the most recent checkpoint) into the graph. Only one of the modes is used for saving variables to the SavedModel (order of preference: tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL, then tf.estimator.ModeKeys.PREDICT), such that up to three tf.MetaGraphDefs are saved with a single set of variables in a single SavedModel directory.
For the variables and tf.MetaGraphDefs, a timestamped export directory below export_dir_base, and writes a SavedModel into it containing the tf.MetaGraphDef for the given mode and its associated signatures.
For prediction, the exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
For training and evaluation, the train_op is stored in an extra collection, and loss, metrics, and predictions are included in a SignatureDef for the mode in question.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
View source
Exports inference graph as a SavedModel into the given dir.
For a detailed guide, see SavedModel from Estimators.
This method builds a new graph by first calling the serving_input_receiver_fn to obtain feature Tensors, and then calling this Estimator's model_fn to generate the model graph based on those features. It restores the given checkpoint (or, lacking that, the most recent checkpoint) into this graph in a fresh session. Finally it creates a timestamped export directory below the given export_dir_base, and writes a SavedModel into it containing a single tf.MetaGraphDef saved from this session.
The exported MetaGraphDef will provide one SignatureDef for each element of the export_outputs dict returned from the model_fn, named using the same keys. One of these keys is always tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY, indicating which signature will be served when a serving request does not specify one. For each signature, the outputs are provided by the corresponding tf.estimator.export.ExportOutputs, and the inputs are always the input receivers provided by the serving_input_receiver_fn.
Extra assets may be written into the SavedModel via the assets_extra argument. This should be a dict, where each key gives a destination path (including the filename) relative to the assets.extra directory. The corresponding value gives the full path of the source file to be copied. For example, the simple case of copying a single file without renaming it is specified as {'my_asset_file.txt': '/path/to/my_asset_file.txt'}.
The experimental_mode parameter can be used to export a single train/eval/predict graph as a SavedModel. See experimental_export_all_saved_models for full docs.
View source
Returns list of all variable names in this model.
View source
Returns value of the variable given by name.
View source
Finds the filename of the latest saved checkpoint file in model_dir.
View source
Yields predictions for given features.
Please note that interleaving two predict outputs does not work. See: issue/20506
View source
Trains a model given training data input_fn.


Prints the given tensors every N local steps, every N seconds, or at end.
Inherits From: SessionRunHook
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.LoggingTensorHook, tf.compat.v1.train.LoggingTensorHook
The tensors will be printed to the log, with INFO severity. If you are not seeing the logs, you might want to add the following line after your imports:
Note that if at_end is True, tensors should not include any tensor whose evaluation produces a side effect such as consuming additional inputs.
View source
Called when new TensorFlow session is created.
This is called to signal the hooks that a new session has been created. This has two essential differences with the situation in which begin is called:
View source
Called after each call to run().
The run_values argument contains results of requested ops/tensors by before_run().
The run_context argument is the same one send to before_run call. run_context.request_stop() can be called to stop the iteration.
If session.run() raises any exceptions then after_run() is not called.
View source
Called before each call to run().
You can return from this call a SessionRunArgs object indicating ops or tensors to add to the upcoming run() call. These ops/tensors will be run together with the ops/tensors originally passed to the original run() call. The run args you return can also contain feeds to be added to the run() call.
The run_context argument is a SessionRunContext that provides information about the upcoming run() call: the originally requested op/tensors, the TensorFlow Session.
At this point graph is finalized and you can not add ops.
View source
Called once before using the session.
When called, the default graph is the one that will be launched in the session. The hook can modify the graph by adding new operations to it. After the begin() call the graph will be finalized and the other callbacks can not modify the graph anymore. Second call of begin() on the same graph, should not change the graph.
View source
Called at the end of session.
The session argument can be used in case the hook wants to run final ops, such as saving a last checkpoint.
If session.run() raises exception other than OutOfRangeError or StopIteration then end() is not called. Note the difference between end() and after_run() behavior when session.run() raises OutOfRangeError or StopIteration. In that case end() is called but after_run() is not called.


Creates a Head for logistic regression.
Inherits From: RegressionHead, Head
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.LogisticRegressionHead
Uses sigmoid_cross_entropy_with_logits loss, which is the same as BinaryClassHead. The differences compared to BinaryClassHead are:
The head expects logits with shape [D0, D1, ... DN, 1]. In many applications, the shape is [batch_size, 1].
The labels shape must match logits, namely [D0, D1, ... DN] or [D0, D1, ... DN, 1].
If weight_column is specified, weights must be of shape [D0, D1, ... DN] or [D0, D1, ... DN, 1].
This is implemented as a generalized linear model, see https://en.wikipedia.org/wiki/Generalized_linear_model
The head can be used with a canned estimator. Example:
It can also be used with a custom model_fn. Example:
View source
Returns EstimatorSpec that a model_fn can return.
It is recommended to pass all args via name.
View source
Return predictions based on keys. See base_head.Head for details.
View source
Creates metrics. See base_head.Head for details.
View source
Return predictions based on keys.
See base_head.Head for details.
View source
Updates eval metrics. See base_head.Head for details.


Standard names for Estimator model modes.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.ModeKeys
The following standard keys are defined:


Creates a Head for multi class classification.
Inherits From: Head
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.MultiClassHead
Uses sparse_softmax_cross_entropy loss.
The head expects logits with shape [D0, D1, ... DN, n_classes]. In many applications, the shape is [batch_size, n_classes].
labels must be a dense Tensor with shape matching logits, namely [D0, D1, ... DN, 1]. If label_vocabulary given, labels must be a string Tensor with values from the vocabulary. If label_vocabulary is not given, labels must be an integer Tensor with values specifying the class index.
If weight_column is specified, weights must be of shape [D0, D1, ... DN], or [D0, D1, ... DN, 1].
The loss is the weighted sum over the input dimensions. Namely, if the input labels have shape [batch_size, 1], the loss is the weighted sum over batch_size.
Also supports custom loss_fn. loss_fn takes (labels, logits) or (labels, logits, features, loss_reduction) as arguments and returns unreduced loss with shape [D0, D1, ... DN, 1]. loss_fn must support integer labels with shape [D0, D1, ... DN, 1]. Namely, the head applies label_vocabulary to the input labels before passing them to loss_fn.
Usage with a canned estimator:
It can also be used with a custom model_fn. Example:
View source
Returns EstimatorSpec that a model_fn can return.
It is recommended to pass all args via name.
View source
Returns regularized training loss. See base_head.Head for details.
View source
Creates metrics. See base_head.Head for details.
View source
Return predictions based on keys.
See base_head.Head for details.
View source
Updates eval metrics. See base_head.Head for details.


Creates a Head for multi-objective learning.
Inherits From: Head
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.MultiHead
This class merges the output of multiple Head objects. Specifically:
Usage with a canned estimator:
Also supports logits as a Tensor of shape [D0, D1, ... DN, logits_dimension]. It will split the Tensor along the last dimension and distribute it appropriately among the heads. E.g.:
View source
Returns a model_fn.EstimatorSpec.
View source
Returns regularized training loss. See base_head.Head for details.
View source
Creates metrics. See base_head.Head for details.
View source
Create predictions. See base_head.Head for details.
View source
Updates eval metrics. See base_head.Head for details.


Creates a Head for multi-label classification.
Inherits From: Head
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.MultiLabelHead
Multi-label classification handles the case where each example may have zero or more associated labels, from a discrete set. This is distinct from MultiClassHead which has exactly one label per example.
Uses sigmoid_cross_entropy loss average over classes and weighted sum over the batch. Namely, if the input logits have shape [batch_size, n_classes], the loss is the average over n_classes and the weighted sum over batch_size.
The head expects logits with shape [D0, D1, ... DN, n_classes]. In many applications, the shape is [batch_size, n_classes].
If weight_column is specified, weights must be of shape [D0, D1, ... DN], or [D0, D1, ... DN, 1].
Also supports custom loss_fn. loss_fn takes (labels, logits) or (labels, logits, features) as arguments and returns unreduced loss with shape [D0, D1, ... DN, 1]. loss_fn must support indicator labels with shape [D0, D1, ... DN, n_classes]. Namely, the head applies label_vocabulary to the input labels before passing them to loss_fn.
Usage with a canned estimator:
It can also be used with a custom model_fn. Example:
View source
Returns EstimatorSpec that a model_fn can return.
It is recommended to pass all args via name.
View source
Returns regularized training loss. See base_head.Head for details.
View source
Creates metrics. See base_head.Head for details.
View source
Return predictions based on keys.
See base_head.Head for details.
View source
Updates eval metrics. See base_head.Head for details.


Unspecified run-time error.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.NanLossDuringTrainingError, tf.compat.v1.train.NanLossDuringTrainingError


Monitors the loss tensor and stops training if loss is NaN.
Inherits From: SessionRunHook
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.NanTensorHook, tf.compat.v1.train.NanTensorHook
Can either fail with exception or just stop training.
View source
Called when new TensorFlow session is created.
This is called to signal the hooks that a new session has been created. This has two essential differences with the situation in which begin is called:
View source
Called after each call to run().
The run_values argument contains results of requested ops/tensors by before_run().
The run_context argument is the same one send to before_run call. run_context.request_stop() can be called to stop the iteration.
If session.run() raises any exceptions then after_run() is not called.
View source
Called before each call to run().
You can return from this call a SessionRunArgs object indicating ops or tensors to add to the upcoming run() call. These ops/tensors will be run together with the ops/tensors originally passed to the original run() call. The run args you return can also contain feeds to be added to the run() call.
The run_context argument is a SessionRunContext that provides information about the upcoming run() call: the originally requested op/tensors, the TensorFlow Session.
At this point graph is finalized and you can not add ops.
View source
Called once before using the session.
When called, the default graph is the one that will be launched in the session. The hook can modify the graph by adding new operations to it. After the begin() call the graph will be finalized and the other callbacks can not modify the graph anymore. Second call of begin() on the same graph, should not change the graph.
View source
Called at the end of session.
The session argument can be used in case the hook wants to run final ops, such as saving a last checkpoint.
If session.run() raises exception other than OutOfRangeError or StopIteration then end() is not called. Note the difference between end() and after_run() behavior when session.run() raises OutOfRangeError or StopIteration. In that case end() is called but after_run() is not called.


Creates a Head for poisson regression using tf.nn.log_poisson_loss.
Inherits From: RegressionHead, Head
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.PoissonRegressionHead
The loss is the weighted sum over all input dimensions. Namely, if the input labels have shape [batch_size, label_dimension], the loss is the weighted sum over both batch_size and label_dimension.
The head expects logits with shape [D0, D1, ... DN, label_dimension]. In many applications, the shape is [batch_size, label_dimension].
The labels shape must match logits, namely [D0, D1, ... DN, label_dimension]. If label_dimension=1, shape [D0, D1, ... DN] is also supported.
If weight_column is specified, weights must be of shape [D0, D1, ... DN], [D0, D1, ... DN, 1] or [D0, D1, ... DN, label_dimension].
This is implemented as a generalized linear model, see https://en.wikipedia.org/wiki/Generalized_linear_model
The head can be used with a canned estimator. Example:
It can also be used with a custom model_fn. Example:
View source
Returns EstimatorSpec that a model_fn can return.
It is recommended to pass all args via name.
View source
Return predictions based on keys. See base_head.Head for details.
View source
Creates metrics. See base_head.Head for details.
View source
Return predictions based on keys.
See base_head.Head for details.
View source
Updates eval metrics. See base_head.Head for details.


Captures CPU/GPU profiling information every N steps or seconds.
Inherits From: SessionRunHook
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.ProfilerHook, tf.compat.v1.train.ProfilerHook
This produces files called "timeline-.json", which are in Chrome Trace format.
For more information see: https://github.com/catapult-project/catapult/blob/master/tracing/README.md
View source
Called when new TensorFlow session is created.
This is called to signal the hooks that a new session has been created. This has two essential differences with the situation in which begin is called:
View source
Called after each call to run().
The run_values argument contains results of requested ops/tensors by before_run().
The run_context argument is the same one send to before_run call. run_context.request_stop() can be called to stop the iteration.
If session.run() raises any exceptions then after_run() is not called.
View source
Called before each call to run().
You can return from this call a SessionRunArgs object indicating ops or tensors to add to the upcoming run() call. These ops/tensors will be run together with the ops/tensors originally passed to the original run() call. The run args you return can also contain feeds to be added to the run() call.
The run_context argument is a SessionRunContext that provides information about the upcoming run() call: the originally requested op/tensors, the TensorFlow Session.
At this point graph is finalized and you can not add ops.
View source
Called once before using the session.
When called, the default graph is the one that will be launched in the session. The hook can modify the graph by adding new operations to it. After the begin() call the graph will be finalized and the other callbacks can not modify the graph anymore. Second call of begin() on the same graph, should not change the graph.
View source
Called at the end of session.
The session argument can be used in case the hook wants to run final ops, such as saving a last checkpoint.
If session.run() raises exception other than OutOfRangeError or StopIteration then end() is not called. Note the difference between end() and after_run() behavior when session.run() raises OutOfRangeError or StopIteration. In that case end() is called but after_run() is not called.


Creates a Head for regression using the mean_squared_error loss.
Inherits From: Head
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.RegressionHead
The loss is the weighted sum over all input dimensions. Namely, if the input labels have shape [batch_size, label_dimension], the loss is the weighted sum over both batch_size and label_dimension.
The head expects logits with shape [D0, D1, ... DN, label_dimension]. In many applications, the shape is [batch_size, label_dimension].
The labels shape must match logits, namely [D0, D1, ... DN, label_dimension]. If label_dimension=1, shape [D0, D1, ... DN] is also supported.
If weight_column is specified, weights must be of shape [D0, D1, ... DN], [D0, D1, ... DN, 1] or [D0, D1, ... DN, label_dimension].
Supports custom loss_fn. loss_fn takes (labels, logits) or (labels, logits, features, loss_reduction) as arguments and returns unreduced loss with shape [D0, D1, ... DN, label_dimension].
Also supports custom inverse_link_fn, also known as 'mean function'. inverse_link_fn is only used in PREDICT mode. It takes logits as argument and returns predicted values. This function is the inverse of the link function defined in https://en.wikipedia.org/wiki/Generalized_linear_model#Link_function Namely, for poisson regression, set inverse_link_fn=tf.exp.
Usage with a canned estimator:
It can also be used with a custom model_fn. Example:
View source
Returns EstimatorSpec that a model_fn can return.
It is recommended to pass all args via name.
View source
Return predictions based on keys. See base_head.Head for details.
View source
Creates metrics. See base_head.Head for details.
View source
Return predictions based on keys.
See base_head.Head for details.
View source
Updates eval metrics. See base_head.Head for details.


Generates parsing spec for tf.parse_example to be used with regressors.
If users keep data in tf.Example format, they need to call tf.parse_example with a proper feature spec. There are two main things that this utility helps:
Example output of parsing spec:
Example usage with a regressor:


This class specifies the configurations for an Estimator run.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.RunConfig
If device_fn is not None, it overrides the default device function used in Estimator. Otherwise the default one is used.
All global ids in the training cluster are assigned from an increasing sequence of consecutive integers. The first id is 0.
Nodes with task type worker can have id 0, 1, 2. Nodes with task type ps can have id, 0, 1. So, task_id is not unique, but the pair (task_type, task_id) can uniquely determine a node in the cluster.
Global id, i.e., this field, is tracking the index of the node among ALL nodes in the cluster. It is uniquely assigned. For example, for the cluster spec given above, the global ids are assigned as:
View source
Returns a new instance of RunConfig replacing specified properties.
Only the properties in the following list are allowed to be replaced:
In addition, either save_checkpoints_steps or save_checkpoints_secs can be set (should not be both).


Timer that triggers at most once every N seconds or once every N steps.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.SecondOrStepTimer, tf.compat.v1.train.SecondOrStepTimer
This symbol is also exported to v2 in tf.estimator namespace. See https://github.com/tensorflow/estimator/blob/master/tensorflow_estimator/python/estimator/hooks/basic_session_run_hooks.py
View source
Returns the last triggered time step or None if never triggered.
View source
Resets the timer.
View source
Return true if the timer should trigger for the specified step.
View source
Update the last triggered time and step number.


Represents arguments to be added to a Session.run() call.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.SessionRunArgs, tf.compat.v1.train.SessionRunArgs


Provides information about the session.run() call being made.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.SessionRunContext, tf.compat.v1.train.SessionRunContext
Provides information about original request to Session.Run() function. SessionRunHook objects can stop the loop by calling request_stop() of run_context. In the future we may use this object to add more information about run without changing the Hook API.
If user called MonitoredSession.run(fetches=a, feed_dict=b), then this field is equal to SessionRunArgs(a, b).
If true, MonitoredSession stops iterations. Returns: A bool
View source
Sets stop requested field.
Hooks can use this function to request stop of iterations. MonitoredSession checks whether this is called or not.


Hook to extend calls to MonitoredSession.run().
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.SessionRunHook, tf.compat.v1.train.SessionRunHook
View source
Called when new TensorFlow session is created.
This is called to signal the hooks that a new session has been created. This has two essential differences with the situation in which begin is called:
View source
Called after each call to run().
The run_values argument contains results of requested ops/tensors by before_run().
The run_context argument is the same one send to before_run call. run_context.request_stop() can be called to stop the iteration.
If session.run() raises any exceptions then after_run() is not called.
View source
Called before each call to run().
You can return from this call a SessionRunArgs object indicating ops or tensors to add to the upcoming run() call. These ops/tensors will be run together with the ops/tensors originally passed to the original run() call. The run args you return can also contain feeds to be added to the run() call.
The run_context argument is a SessionRunContext that provides information about the upcoming run() call: the originally requested op/tensors, the TensorFlow Session.
At this point graph is finalized and you can not add ops.
View source
Called once before using the session.
When called, the default graph is the one that will be launched in the session. The hook can modify the graph by adding new operations to it. After the begin() call the graph will be finalized and the other callbacks can not modify the graph anymore. Second call of begin() on the same graph, should not change the graph.
View source
Called at the end of session.
The session argument can be used in case the hook wants to run final ops, such as saving a last checkpoint.
If session.run() raises exception other than OutOfRangeError or StopIteration then end() is not called. Note the difference between end() and after_run() behavior when session.run() raises OutOfRangeError or StopIteration. In that case end() is called but after_run() is not called.


Contains the results of Session.run().
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.SessionRunValues, tf.compat.v1.train.SessionRunValues
In the future we may use this object to add more information about result of run without changing the Hook API.


Hook that counts steps per second.
Inherits From: SessionRunHook
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.StepCounterHook, tf.compat.v1.train.StepCounterHook
View source
Called when new TensorFlow session is created.
This is called to signal the hooks that a new session has been created. This has two essential differences with the situation in which begin is called:
View source
Called after each call to run().
The run_values argument contains results of requested ops/tensors by before_run().
The run_context argument is the same one send to before_run call. run_context.request_stop() can be called to stop the iteration.
If session.run() raises any exceptions then after_run() is not called.
View source
Called before each call to run().
You can return from this call a SessionRunArgs object indicating ops or tensors to add to the upcoming run() call. These ops/tensors will be run together with the ops/tensors originally passed to the original run() call. The run args you return can also contain feeds to be added to the run() call.
The run_context argument is a SessionRunContext that provides information about the upcoming run() call: the originally requested op/tensors, the TensorFlow Session.
At this point graph is finalized and you can not add ops.
View source
Called once before using the session.
When called, the default graph is the one that will be launched in the session. The hook can modify the graph by adding new operations to it. After the begin() call the graph will be finalized and the other callbacks can not modify the graph anymore. Second call of begin() on the same graph, should not change the graph.
View source
Called at the end of session.
The session argument can be used in case the hook wants to run final ops, such as saving a last checkpoint.
If session.run() raises exception other than OutOfRangeError or StopIteration then end() is not called. Note the difference between end() and after_run() behavior when session.run() raises OutOfRangeError or StopIteration. In that case end() is called but after_run() is not called.


Hook that requests stop at a specified step.
Inherits From: SessionRunHook
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.StopAtStepHook, tf.compat.v1.train.StopAtStepHook
View source
Called when new TensorFlow session is created.
This is called to signal the hooks that a new session has been created. This has two essential differences with the situation in which begin is called:
View source
Called after each call to run().
The run_values argument contains results of requested ops/tensors by before_run().
The run_context argument is the same one send to before_run call. run_context.request_stop() can be called to stop the iteration.
If session.run() raises any exceptions then after_run() is not called.
View source
Called before each call to run().
You can return from this call a SessionRunArgs object indicating ops or tensors to add to the upcoming run() call. These ops/tensors will be run together with the ops/tensors originally passed to the original run() call. The run args you return can also contain feeds to be added to the run() call.
The run_context argument is a SessionRunContext that provides information about the upcoming run() call: the originally requested op/tensors, the TensorFlow Session.
At this point graph is finalized and you can not add ops.
View source
Called once before using the session.
When called, the default graph is the one that will be launched in the session. The hook can modify the graph by adding new operations to it. After the begin() call the graph will be finalized and the other callbacks can not modify the graph anymore. Second call of begin() on the same graph, should not change the graph.
View source
Called at the end of session.
The session argument can be used in case the hook wants to run final ops, such as saving a last checkpoint.
If session.run() raises exception other than OutOfRangeError or StopIteration then end() is not called. Note the difference between end() and after_run() behavior when session.run() raises OutOfRangeError or StopIteration. In that case end() is called but after_run() is not called.


Saves summaries every N steps.
Inherits From: SessionRunHook
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.SummarySaverHook, tf.compat.v1.train.SummarySaverHook
View source
Called when new TensorFlow session is created.
This is called to signal the hooks that a new session has been created. This has two essential differences with the situation in which begin is called:
View source
Called after each call to run().
The run_values argument contains results of requested ops/tensors by before_run().
The run_context argument is the same one send to before_run call. run_context.request_stop() can be called to stop the iteration.
If session.run() raises any exceptions then after_run() is not called.
View source
Called before each call to run().
You can return from this call a SessionRunArgs object indicating ops or tensors to add to the upcoming run() call. These ops/tensors will be run together with the ops/tensors originally passed to the original run() call. The run args you return can also contain feeds to be added to the run() call.
The run_context argument is a SessionRunContext that provides information about the upcoming run() call: the originally requested op/tensors, the TensorFlow Session.
At this point graph is finalized and you can not add ops.
View source
Called once before using the session.
When called, the default graph is the one that will be launched in the session. The hook can modify the graph by adding new operations to it. After the begin() call the graph will be finalized and the other callbacks can not modify the graph anymore. Second call of begin() on the same graph, should not change the graph.
View source
Called at the end of session.
The session argument can be used in case the hook wants to run final ops, such as saving a last checkpoint.
If session.run() raises exception other than OutOfRangeError or StopIteration then end() is not called. Note the difference between end() and after_run() behavior when session.run() raises OutOfRangeError or StopIteration. In that case end() is called but after_run() is not called.


Configuration for the "train" part for the train_and_evaluate call.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.TrainSpec
TrainSpec determines the input data for the training, as well as the duration. Optional hooks run at various stages of training.


Train and evaluate the estimator.
Compat aliases for migration
See Migration guide for more details.
tf.compat.v1.estimator.train_and_evaluate
This utility function trains, evaluates, and (optionally) exports the model by using the given estimator. All training related specification is held in train_spec, including training input_fn and training max steps, etc. All evaluation and export related specification is held in eval_spec, including evaluation input_fn, steps, etc.
This utility function provides consistent behavior for both local (non-distributed) and distributed configurations. The default distribution configuration is parameter server-based between-graph replication. For other types of distribution configurations such as all-reduce training, please use DistributionStrategies.
Overfitting: In order to avoid overfitting, it is recommended to set up the training input_fn to shuffle the training data properly.
Stop condition: In order to support both distributed and non-distributed configuration reliably, the only supported stop condition for model training is train_spec.max_steps. If train_spec.max_steps is None, the model is trained forever. Use with care if model stop condition is different. For example, assume that the model is expected to be trained with one epoch of training data, and the training input_fn is configured to throw OutOfRangeError after going through one epoch, which stops the Estimator.train. For a three-training-worker distributed configuration, each training worker is likely to go through the whole epoch independently. So, the model will be trained with three epochs of training data instead of one epoch.
Example of local (non-distributed) training:
Note that in current implementation estimator.evaluate will be called multiple times. This means that evaluation graph (including eval_input_fn) will be re-created for each evaluate call. estimator.train will be called only once.
Example of distributed training:
Regarding the example of distributed training, the code above can be used without a change (Please do make sure that the RunConfig.model_dir for all workers is set to the same directory, i.e., a shared file system all workers can read and write). The only extra work to do is setting the environment variable TF_CONFIG properly for each worker correspondingly.
Also see Distributed TensorFlow.
Setting environment variable depends on the platform. For example, on Linux, it can be done as follows ($ is the shell prompt):
For the content in TF_CONFIG, assume that the training cluster spec looks like:
Example of TF_CONFIG for chief training worker (must have one and only one):
Note that the chief worker also does the model training job, similar to other non-chief training workers (see next paragraph). In addition to the model training, it manages some extra work, e.g., checkpoint saving and restoring, writing summaries, etc.
Example of TF_CONFIG for non-chief training worker (optional, could be multiple):
where the task.index should be set as 0, 1, 2, in this example, respectively for non-chief training workers.
Example of TF_CONFIG for parameter server, aka ps (could be multiple):
where the task.index should be set as 0 and 1, in this example, respectively for parameter servers.
Example of TF_CONFIG for evaluator task. Evaluator is a special task that is not part of the training cluster. There could be only one. It is used for model evaluation.
When distribute or experimental_distribute.train_distribute and experimental_distribute.remote_cluster is set, this method will start a client running on the current host which connects to the remote_cluster for training and evaluation.