|  TensorFlow 2 version |  View source on GitHub | 
Interface for the head/top of a model.
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:
- Simplify writing model_fn and to make model_fn more configurable for Estimator.
- Simpilfy creating loss and metrics for the train and test loop in Eager execution.
- Support wide range of machine learning models. Since most heads can work with logits, they can support DNN, RNN, Wide, Wide&Deep, Global objectives, Gradient boosted trees and many other types of machine learning models.
Common usage:
Here is simplified model_fn to build a DNN regression model.
def _my_dnn_model_fn(features, labels, mode, params, config=None): # Optionally your callers can pass head to model_fn as a param. head = tf.estimator.RegressionHead(...) inputs = tf.feature_column.input_layer(features, ...) # Compute logits with tf.keras.layers API hidden_layer0 = tf.keras.layers.Dense( units=1000, activation="relu")(inputs) hidden_layer1 = tf.keras.layers.Dense( units=500, activation="relu")(hidden_layer0) logits = tf.keras.layers.Dense( units=head.logits_dimension, activation=None)(hidden_layer1) # Or use Keras model for logits computation model = tf.keras.Sequential() model.add(tf.keras.layers.Dense(units=1000, activation="relu")) model.add(tf.keras.layers.Dense(units=500, activation="relu")) model.add(tf.keras.layers.Dense( units=head.logits_dimension, activation=None)) logits = model(inputs) return head.create_estimator_spec( features=features, labels=labels, mode=mode, logits=logits, optimizer=optimizer)
| Attributes | |
|---|---|
| logits_dimension | Size of the last dimension of the logits Tensor.Often is the number of classes, labels, or real values to be predicted.
Typically, logits is of shape  | 
| loss_reduction | One of tf.losses.Reduction.Describes how to reduce training loss over batch, such as mean or sum. | 
| name | The name of this head. | 
Methods
create_estimator_spec
create_estimator_spec(
    features, mode, logits, labels=None, optimizer=None, trainable_variables=None,
    train_op_fn=None, update_ops=None, regularization_losses=None
)
Returns EstimatorSpec that a model_fn can return.
It is recommended to pass all args via name.
| Args | |
|---|---|
| features | Input dictmapping string feature names toTensororSparseTensorobjects containing the values for that feature in a
minibatch. Often to be used to fetch example-weight tensor. | 
| mode | Estimator's ModeKeys. | 
| logits | Logits Tensorto be used by the head. | 
| labels | Labels Tensor, ordictmapping string label names toTensorobjects of the label values. | 
| optimizer | An tf.keras.optimizers.Optimizerinstance to optimize the
loss in TRAIN mode. Namely, setstrain_op = optimizer.get_updates(loss,
trainable_variables), which updates variables to minimizeloss. | 
| trainable_variables | A list or tuple of Variableobjects to update to
minimizeloss. In Tensorflow 1.x, by default these are the list of
variables collected in the graph under the keyGraphKeys.TRAINABLE_VARIABLES. As Tensorflow 2.x doesn't have
collections and GraphKeys, trainable_variables need to be passed
explicitly here. | 
| train_op_fn | Function that takes a scalar loss Tensorand returns an op
to optimize the model with the loss in TRAIN mode. Used ifoptimizerisNone. Exactly one oftrain_op_fnandoptimizermust be set in
TRAIN mode. By default, it isNonein other modes. If you want to
optimize loss yourself, you can passlambda _: tf.no_op()and then useEstimatorSpec.lossto compute and apply gradients. | 
| update_ops | A list or tuple of update ops to be run at training time. For example, layers such as BatchNormalization create mean and variance update ops that need to be run at training time. In Tensorflow 1.x, these are thrown into an UPDATE_OPS collection. As Tensorflow 2.x doesn't have collections, update_ops need to be passed explicitly here. | 
| regularization_losses | A list of additional scalar losses to be added to the training loss, such as regularization losses. | 
| Returns | |
|---|---|
| EstimatorSpec. | 
loss
@abc.abstractmethodloss( labels, logits, features=None, mode=None, regularization_losses=None )
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.
| Args | |
|---|---|
| labels | Labels Tensor, ordictmapping string label names toTensorobjects of the label values. | 
| logits | Logits Tensorto be used for loss construction. | 
| features | Input dictmapping string feature names toTensororSparseTensorobjects containing the values for that feature in a
minibatch. Often to be used to fetch example-weight tensor. | 
| mode | Estimator's ModeKeys. To be used in case loss calculation is
different in Train and Eval mode. | 
| regularization_losses | A list of additional scalar losses to be added to the training loss, such as regularization losses. | 
| Returns | |
|---|---|
| A scalar Tensorrepresenting regularized training loss used in train and
eval. | 
metrics
@abc.abstractmethodmetrics( regularization_losses=None )
Returns a dict of metric objects.
| Args | |
|---|---|
| regularization_losses | A list of additional scalar losses to be added to the training loss, such as regularization losses. | 
| Returns | |
|---|---|
| A dictof metrics keyed by string name. The value is an instance ofMetricclass. | 
predictions
@abc.abstractmethodpredictions( logits, keys=None )
Returns a dict of predictions from provided logits.
| Args | |
|---|---|
| logits | Logits Tensorto be used for prediction construction. | 
| keys | A list of stringfor prediction keys. Defaults toNone, meaning
if not specified, predictions will be created for all the pre-defined
valid keys in the head. | 
| Returns | |
|---|---|
| A dictof predictedTensorkeyed by prediction name. | 
update_metrics
@abc.abstractmethodupdate_metrics( eval_metrics, features, logits, labels, mode=None, regularization_losses=None )
Updates metric objects and returns a dict of the updated metrics.
| Args | |
|---|---|
| eval_metrics | A dictof metrics to be updated. | 
| features | Input dictmapping string feature names toTensororSparseTensorobjects containing the values for that feature in a
minibatch. Often to be used to fetch example-weight tensor. | 
| logits | logits Tensorto be used for metrics update. | 
| labels | Labels Tensor, ordictmapping string label names toTensorobjects of the label values. | 
| mode | Estimator's ModeKeys. | 
| regularization_losses | A list of additional scalar losses to be added to the training and evaluation loss, such as regularization losses. | 
| Returns | |
|---|---|
| A dictof updated metrics keyed by name. The value is an instance ofMetricclass. |