A classifier that can establish a simple baseline.

Inherits From: Estimator, Estimator

This classifier ignores feature values and will learn to predict the average value of each label. For single-label problems, this will predict the probability distribution of the classes as seen in the labels. For multi-label problems, this will predict the fraction of examples that are positive for each class.


# Build BaselineClassifier
classifier = tf.estimator.BaselineClassifier(n_classes=3)

# Input builders
def input_fn_train:
  # Returns of (x, y) tuple where y represents label's class
  # index.

def input_fn_eval:
  # Returns of (x, y) tuple where y represents label's class
  # index.

# Fit model.

# Evaluate cross entropy between the test and train labels.
loss = classifier.evaluate(input_fn=input_fn_eval)["loss"]

# predict outputs the probability distribution of the classes as seen in
# training.
predictions = classifier.predict(new_samples)

Input of train and evaluate should have following features, otherwise there will be a KeyError:

  • if weight_column is not None, a feature with key=weight_column whose value is a Tensor.

model_dir Directory to save model parameters, graph and etc. This can also be used to load checkpoints from the directory into a estimator to continue training a previously saved model.
n_classes number of label classes. Default is binary classification. It must be greater than 1. Note: Class labels are integers representing the class index (i.e. values from 0 to n_classes-1). For arbitrary label values (e.g. string labels), convert to class indices first.
weight_column A string or a NumericColumn created by tf.feature_column.numeric_column defining feature column representing weights. It will be multiplied by the loss of the example.
label_vocabulary Optional list of strings with size [n_classes] defining the label vocabulary. Only supported for n_classes > 2.
optimizer String, tf.keras.optimizers.* object, or callable that creates the optimizer to use for training. If not specified, will use Ftrl as the default optimizer.
config RunConfig object to configure the runtime settings.
loss_reduction One of tf.losses.Reduction except NONE. Describes how to reduce training loss over batch. Defaults to SUM_OVER_BATCH_SIZE.

ValueError If n_classes < 2.




model_fn Returns the model_fn which is bound to self.params.



View source

Shows the directory name where evaluation metrics are dumped.

name Name of the evaluation if user needs to run multiple evaluations on different data sets, such as on training data vs test data. Metrics for different evaluations are saved in separate folders, and appear separately in tensorboard.

A string which is the path of directory contains evaluation metrics.


View source

Evaluates the model given evaluation data input_fn.

For each step, calls input_fn, which returns one batch of data. Evaluates until:

  • steps batches are processed, or
  • input_fn raises an end-of-input exception (