Machine LearningModel Training and Evaluation Metrics
Why Are Recall, F1-Score, and Accuracy Not Usually Used Directly as Training Loss Functions?
Question 302 of 303 · Updated
On this page
- 1. Training loss and evaluation metric have different jobs
- 2. Why hard accuracy gives a poor gradient
- 3. Why recall has the same difficulty
- 4. Why F1-score is even more involved
- 5. A numerical example: identical accuracy, different confidence
- 6. What should be used during training instead?
- Binary and categorical cross-entropy
- Hinge loss
- Other task-specific surrogates
- 7. Can F1 or another metric ever be optimized directly?
- 8. A practical workflow
- 9. Python example
- 10. Common misconceptions
- Interview-ready answer
Accuracy, recall, and F1-score are important ways to evaluate a classifier. But they are usually poor choices as the objective that a model directly minimizes during gradient-based training.
The central reason is their usual definitions operate on discrete class decisions rather than smooth probability predictions. A small change in model parameters often does not change a predicted class at all; when a decision finally crosses the classification threshold, the metric jumps. That gives ordinary backpropagation little or no useful gradient.
Instead, models are commonly trained with a differentiable surrogate objective—such as binary or categorical cross-entropy, or hinge loss—and evaluated with the metric that matters to the application. The surrogate is a training signal, not a promise that the evaluation metric will improve monotonically.
1. Training loss and evaluation metric have different jobs#
A training loss gives the optimizer a numerical signal about how to change model parameters. With gradient descent, the model uses derivatives such as:
Here, represents model parameters, is the learning rate, and is the training objective.
An evaluation metric answers a different question: how useful are the model's predictions under a chosen decision rule? Accuracy, recall, and F1 are often exactly the right metrics for this purpose, even though their standard hard-label versions are not convenient objectives for backpropagation.
A useful distinction:
- Loss: supplies a learning signal to the optimizer.
- Metric: summarizes model performance in terms people care about.
- Decision threshold: converts scores or probabilities into class labels and can change precision, recall, F1, and accuracy without changing the underlying model weights.
The same quantity can sometimes be used for both evaluation and optimization, but it does not follow that every useful metric is a useful gradient-based loss.
2. Why hard accuracy gives a poor gradient#
For binary classification, accuracy is:
The indicator function is 1 when the predicted class is correct and 0 otherwise. A common prediction rule is:
where is the model's predicted probability for the positive class.
Consider a positive example (true label 1):
- Model A predicts 0.51.
- Model B predicts 0.99.
Both receive exactly the same accuracy contribution: 1. If the probability changes from 0.51 to 0.60, accuracy does not change. If it changes from 0.49 to 0.51, accuracy jumps from 0 to 1.
As a function of the probability, hard accuracy is piecewise constant with jumps at the decision threshold. Its derivative is zero almost everywhere and undefined at the jump. Consequently, ordinary gradient descent cannot use it to learn how to improve a prediction.
This also means accuracy ignores confidence once the predicted class is fixed. A barely correct prediction and a confidently correct prediction count equally.
3. Why recall has the same difficulty#
For a binary classifier:
Recall measures the fraction of actual positive examples that the classifier identifies. Its standard calculation uses true-positive and false-negative counts, which depend on thresholded class predictions.
If a positive example's predicted probability moves from 0.20 to 0.40 while the threshold remains 0.50, its predicted class stays negative. The recall value does not change, so this movement gives no ordinary gradient signal through hard recall.
When a probability crosses the threshold, the count changes discretely. Recall is therefore not a smooth per-example function of the model's probabilities.
Recall also depends on the collection of examples being evaluated. It is a ratio of aggregate counts, not simply an independent smooth penalty that can be assigned to each example and averaged.
4. Why F1-score is even more involved#
The F1-score is the harmonic mean of precision and recall:
It balances false positives and false negatives through the precision and recall terms. But TP, FP, and FN are counts derived from hard class decisions.
This creates two related challenges:
- Discontinuous decisions: with a fixed threshold, small probability changes often leave all the counts unchanged; a threshold crossing changes a count abruptly.
- Batch-level coupling: the F1 value for one example depends on the other examples in the evaluated batch or dataset because all contribute to TP, FP, and FN.
That makes ordinary per-example gradient optimization awkward. The metric is not naturally decomposable into a simple sum of independent, smooth example losses.
F1 is also sensitive to the positive-class definition and averaging convention (binary, micro, macro, weighted, or samples). Those choices matter when designing a task-specific optimization strategy.
5. A numerical example: identical accuracy, different confidence#
Suppose the true labels are:
Two models predict positive-class probabilities:
Using a threshold of 0.50, both models predict:
Both therefore have 3 correct predictions out of 4, giving accuracy of 0.75. Their hard-label recall is also the same: 1 of 2 positive examples is found, so recall is 0.50.
Yet the probability quality is very different. Model A assigns 0.49 to a positive example it misses; Model B assigns only 0.01. Both miss the same example under the threshold, but Model B is much more confidently wrong.
Binary cross-entropy can distinguish these cases:
It penalizes assigning a very low probability to a true positive (or a very high probability to a true negative). Unlike hard accuracy, it changes as probabilities change—even when the predicted class has not changed.
The lesson is not that cross-entropy is always the best possible objective. It is that a probability-based loss provides a richer optimization signal than a hard threshold metric.
6. What should be used during training instead?#
Choose a differentiable or otherwise gradient-compatible surrogate that reflects the prediction task:
Binary and categorical cross-entropy#
Cross-entropy compares predicted class probabilities with the observed labels. It is a standard choice for probabilistic classification and is commonly used with sigmoid or softmax outputs.
For binary classification:
It rewards assigning probability to the observed class and penalizes confident mistakes. In standard neural-network implementations, numerically stable logits-based versions are preferred.
Hinge loss#
For margin-based classifiers such as support vector machines, hinge loss encourages the correct class to have a sufficient margin:
where and is the model score.
Other task-specific surrogates#
Depending on the problem, training may use focal loss for class imbalance or hard-example emphasis, ranking losses for ordering, or differentiable approximations to overlap metrics. The choice should reflect the model, data, and deployment objective.
These losses do not calculate hard accuracy, recall, or F1 directly. They provide smoother signals that can help the optimizer learn decision boundaries likely to improve the desired metrics.
7. Can F1 or another metric ever be optimized directly?#
Yes—with care. It is too strong to say that accuracy, recall, or F1 can never be optimized.
Researchers and practitioners use approaches such as:
- Soft or differentiable approximations: replace hard class membership with probabilities or smooth functions, then construct a differentiable approximation to a metric.
- Structured or non-decomposable optimization: optimize objectives that account for dataset-level interactions, sometimes using specialized algorithms.
- Threshold tuning: train a model with a suitable surrogate, then select a decision threshold on validation data to maximize F1, recall at a required precision, or another operational target.
- Reinforcement or black-box methods: in some settings, optimize a non-differentiable reward using methods other than ordinary backpropagation.
These approaches involve trade-offs. A soft-F1 objective, for example, is an approximation whose behavior can depend on batch composition, smoothing constants, and class imbalance. It is not automatically superior to cross-entropy.
8. A practical workflow#
For a typical binary classification problem:
- Train with binary cross-entropy (or another suitable surrogate).
- Monitor training and validation loss to understand optimization and overfitting.
- Evaluate accuracy, precision, recall, F1, and other task-relevant metrics on held-out data.
- Select the decision threshold using validation data—not the final test set.
- Compare models using the metric and operational constraints that matter, such as minimum recall or maximum false-positive rate.
- After selecting the model and threshold, report final performance on an untouched test set.
For imbalanced problems, accuracy alone can be misleading. A classifier that predicts the majority class for every example may have high accuracy while failing to identify the minority class. Recall, precision, F1, PR-AUC, and class-specific error analysis can provide a more informative evaluation.
9. Python example#
The following example separates the training objective from evaluation metrics:
```python from sklearn.datasets import make_classification from sklearn.linear_model import LogisticRegression from sklearn.metrics import ( accuracy_score, recall_score, f1_score, log_loss, )
X, y = make_classification( n_samples=1000, n_features=10, weights=[0.9, 0.1], random_state=42, )
model = LogisticRegression() model.fit(X, y) # Optimizes a probabilistic classification objective
probabilities = model.predict_proba(X)[:, 1] predictions = (probabilities >= 0.5).astype(int)
print("Log loss:", log_loss(y, probabilities)) print("Accuracy:", accuracy_score(y, predictions)) print("Recall:", recall_score(y, predictions)) print("F1:", f1_score(y, predictions)) ```
In scikit-learn, these metric functions are available for evaluating predictions. The estimator's fitting procedure uses its configured optimization objective; calling accuracy or F1 after fitting does not make those metrics the training loss.
For a production workflow, calculate these metrics on validation or test data rather than using the training set alone. If the business requirement is high recall, choose a threshold that meets the recall target while measuring the resulting precision and false-positive volume.
10. Common misconceptions#
“Accuracy is useless because it is not differentiable.” No. Accuracy is useful for evaluation and reporting. Its standard hard-label definition is simply not a convenient objective for ordinary gradient descent.
“F1-score should always be the training loss when F1 is the business metric.” Not necessarily. A differentiable surrogate may train more reliably. You can still select the model and tune its threshold using validation F1.
“Cross-entropy directly optimizes F1.” No. Cross-entropy optimizes a probabilistic objective. Better cross-entropy often helps classification, but it does not guarantee higher F1 or recall at every training step.
“A non-differentiable metric can never be optimized.” No. Specialized approximations and optimization methods exist. The limitation is with using the usual hard metric directly in standard backpropagation.
“One metric is enough.” Rarely. Accuracy, recall, precision, F1, calibration, and cost-sensitive measures answer different questions. Select evaluation metrics based on the consequences of false positives and false negatives.
Interview-ready answer#
Recall, F1-score, and accuracy are usually not used directly as training losses because their standard definitions depend on thresholded, discrete predictions and confusion-matrix counts. They are discontinuous or piecewise constant with respect to model scores, so ordinary backpropagation gets zero or unusable gradients over most regions. F1 and recall also depend on aggregate counts across examples. We therefore train with a differentiable surrogate such as cross-entropy or hinge loss, then evaluate and tune the decision threshold using the metric that matters. Direct metric optimization is possible with specialized differentiable approximations or non-standard methods, but it is not the usual default.
References
- 1.
- 2.scikit-learn — F1 scorescikit-learn.org
- 3.scikit-learn — Recall scorescikit-learn.org
- 4.
- 5.
- 6.Google for Developers — Machine Learning Glossary: Loss Functiondevelopers.google.com
Related questions
Explore other topics:Deep LearningMLOps