Keras
machine learning
loss functions
metrics
scoring

Loss, metrics, and scoring in Keras

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Keras, loss and metrics are related but not interchangeable. The loss is what the optimizer tries to minimize during training. Metrics are extra measurements you track for visibility. When people say "scoring" in Keras, they usually mean the values returned by model.evaluate on validation or test data rather than a separate scikit-learn style scoring API.

Loss Drives Learning

The loss function is part of optimization. During backpropagation, Keras computes gradients from the loss, not from your display metrics.

That means the loss must match the task:

  • regression often uses mean squared error or mean absolute error
  • binary classification often uses binary crossentropy
  • multiclass classification often uses sparse or categorical crossentropy
python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(4,)),
5    tf.keras.layers.Dense(8, activation="relu"),
6    tf.keras.layers.Dense(1, activation="sigmoid")
7])
8
9model.compile(
10    optimizer="adam",
11    loss="binary_crossentropy",
12    metrics=["accuracy"]
13)

Here the optimizer learns from binary_crossentropy. accuracy is tracked, but it does not control the gradient updates.

Metrics Are For Monitoring

Metrics answer questions like these:

  • how many classifications were correct
  • what was the precision or recall
  • how close are predictions under some business-specific rule

Because metrics are not necessarily differentiable or useful for optimization, they are usually not suitable as the training objective.

python
1import tensorflow as tf
2
3model.compile(
4    optimizer="adam",
5    loss=tf.keras.losses.BinaryCrossentropy(),
6    metrics=[
7        tf.keras.metrics.BinaryAccuracy(),
8        tf.keras.metrics.Precision(),
9        tf.keras.metrics.Recall(),
10    ]
11)

This gives you richer monitoring without changing the underlying objective.

Why Loss and Metric Values Can Disagree

A common beginner surprise is that the loss improves while accuracy barely moves, or accuracy improves while the loss still looks worse than expected.

That is normal because they measure different things.

For example, in binary classification, changing a prediction from 0.51 to 0.99 may reduce crossentropy significantly even though the predicted class label stays the same and accuracy does not change at all.

That is why you should judge training with both the loss and the metrics that matter for the business problem.

What "Scoring" Usually Means in Keras

Keras does not center its API around a generic score method the way some scikit-learn estimators do. Instead, you typically evaluate the model on held-out data.

python
1import numpy as np
2import tensorflow as tf
3
4x = np.random.randn(100, 4).astype("float32")
5y = (x.sum(axis=1) > 0).astype("float32")
6
7model.fit(x, y, epochs=2, verbose=0)
8results = model.evaluate(x, y, verbose=0)
9print(results)

model.evaluate returns the loss first, followed by the configured metrics in order. That is effectively the model's evaluation score report.

Custom Metrics vs Custom Losses

You can write custom logic for either one, but the bar for custom losses is higher because the loss participates in optimization.

A custom metric can be as simple as a reporting helper:

python
1import tensorflow as tf
2
3
4def mean_prediction(y_true, y_pred):
5    return tf.reduce_mean(y_pred)
6
7model.compile(
8    optimizer="adam",
9    loss="binary_crossentropy",
10    metrics=[mean_prediction]
11)

A custom loss, on the other hand, should be differentiable and numerically stable enough to train with.

Choosing the Right Combination

A practical rule is:

  • choose the loss based on the mathematical training objective
  • choose metrics based on how you judge success
  • use evaluation results on validation or test data as your real score

For example, in imbalanced classification, plain accuracy may look good while recall is unacceptable. In that case, keeping crossentropy as the loss and tracking precision and recall as metrics is often the right approach.

Common Pitfalls

The biggest mistake is treating a metric as if it were the training objective. The model does not optimize your metrics unless the loss is designed to reflect them.

Another mistake is choosing an inappropriate loss for the output layer and label encoding, such as using categorical crossentropy with integer labels that really require sparse categorical crossentropy.

A third issue is reading model.evaluate output without remembering that the first number is always the loss.

Summary

  • The loss is what Keras optimizes during training
  • Metrics are additional measurements for monitoring model behavior
  • In Keras, "scoring" usually means evaluation results from model.evaluate
  • Loss and metric values can move differently because they measure different things
  • Choose the loss for optimization and choose metrics for decision-making and reporting

Course illustration
Course illustration

All Rights Reserved.