-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathlog_loss.py
More file actions
93 lines (74 loc) · 3.21 KB
/
Copy pathlog_loss.py
File metadata and controls
93 lines (74 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""DashAI log loss implementation."""
from typing import TYPE_CHECKING, Optional
from DashAI.back.core.utils import MultilingualString
from DashAI.back.metrics.classification_metric import (
ClassificationMetric,
prepare_to_metric,
)
if TYPE_CHECKING:
import numpy as np
from DashAI.back.dataloaders.classes.dashai_dataset import DashAIDataset
class LogLoss(ClassificationMetric):
"""
Negative log-likelihood of true labels under the predicted probability distribution.
Log Loss (cross-entropy loss) penalises confident wrong predictions much
more heavily than uncertain ones. Unlike accuracy or F1, it evaluates the
full probability output of the classifier rather than just the argmax
label, rewarding well-calibrated models.
Lower values indicate better performance (``MAXIMIZE = False``). A perfect
classifier achieves log loss of 0; a random classifier on a binary problem
achieves approximately ln(2) ≈ 0.693.
::
Log Loss = -(1/N) · Σᵢ Σ_c yᵢ_c · log(pᵢ_c)
where yᵢ_c is 1 if sample i belongs to class c and pᵢ_c is the
predicted probability.
Range: [0, +∞), lower is better (``MAXIMIZE = False``).
References
----------
- [1] https://scikit-learn.org/stable/modules/generated/sklearn.metrics.log_loss.html
"""
DESCRIPTION = MultilingualString(
en=(
"Log Loss, also known as Logistic Loss or Cross-Entropy Loss, "
"measures the performance of a classification model "
"where the prediction input is a probability value "
"between 0 and 1."
),
es=(
"Log Loss, también conocido como Pérdida Logística o Entropía Cruzada, "
"mide el rendimiento de un modelo de clasificación "
"donde la entrada de predicción es un valor de probabilidad "
"entre 0 y 1."
),
)
MAXIMIZE: bool = False
@staticmethod
def score(
true_labels: "DashAIDataset",
probs_pred_labels: "np.ndarray",
multiclass: Optional[bool] = None,
) -> float:
"""Calculate Log Loss score between true labels and predicted labels.
Parameters
----------
true_labels : DashAIDataset
A DashAI dataset with labels.
probs_pred_labels : np.ndarray
A two-dimensional matrix in which each column represents a class
and the row values represent the probability that an example belongs
to the class associated with the column.
multiclass : bool, optional
Whether the task is a multiclass classification. If None, it will be
determined automatically from the number of unique labels.
Returns
-------
float
Log Loss, lower is better.
"""
from sklearn.metrics import log_loss
true_labels, _ = prepare_to_metric(true_labels, probs_pred_labels)
# Pass all expected class indices so log_loss works even when a split
# happens to contain only one class (e.g. small validation sets).
n_classes = probs_pred_labels.shape[1]
labels = list(range(n_classes))
return log_loss(true_labels, probs_pred_labels, labels=labels)