Usage of metrics

A metric is a function that is used to judge the performance of your model. Metric functions are to be supplied in the metrics parameter when a model is compiled.

  1. model.compile(loss='mean_squared_error',
  2. optimizer='sgd',
  3. metrics=['mae', 'acc'])
  1. from keras import metrics
  2. model.compile(loss='mean_squared_error',
  3. optimizer='sgd',
  4. metrics=[metrics.mae, metrics.categorical_accuracy])

A metric function is similar to a loss function, except that the results from evaluating a metric are not used when training the model. You may use any of the loss functions as a metric function.

You can either pass the name of an existing metric, or pass a Theano/TensorFlow symbolic function (see Custom metrics).

Arguments

  • y_true: True labels. Theano/TensorFlow tensor.
  • y_pred: Predictions. Theano/TensorFlow tensor of the same shape as y_true.

Returns

Single tensor value representing the mean of the output array across all datapoints.

Available metrics

accuracy

  1. keras.metrics.accuracy(y_true, y_pred)

binary_accuracy

  1. keras.metrics.binary_accuracy(y_true, y_pred, threshold=0.5)

categorical_accuracy

  1. keras.metrics.categorical_accuracy(y_true, y_pred)

sparse_categorical_accuracy

  1. keras.metrics.sparse_categorical_accuracy(y_true, y_pred)

top_k_categorical_accuracy

  1. keras.metrics.top_k_categorical_accuracy(y_true, y_pred, k=5)

sparse_top_k_categorical_accuracy

  1. keras.metrics.sparse_top_k_categorical_accuracy(y_true, y_pred, k=5)

cosine_proximity

  1. keras.metrics.cosine_proximity(y_true, y_pred, axis=-1)

clone_metric

  1. keras.metrics.clone_metric(metric)

Returns a clone of the metric if stateful, otherwise returns it as is.

clone_metrics

  1. keras.metrics.clone_metrics(metrics)

Clones the given metric list/dict.

In addition to the metrics above, you may use any of the loss functions described in the loss function page as metrics.

Custom metrics

Custom metrics can be passed at the compilation step. Thefunction would need to take (y_true, y_pred) as arguments and returna single tensor value.

  1. import keras.backend as K
  2. def mean_pred(y_true, y_pred):
  3. return K.mean(y_pred)
  4. model.compile(optimizer='rmsprop',
  5. loss='binary_crossentropy',
  6. metrics=['accuracy', mean_pred])