Skip to content

Commit efc37fd

Browse files
THEGAMECHANGER416sedatguzelsemme
authored andcommitted
Added categorical_crossentropy loss function (TheAlgorithms#10152)
1 parent 155bd0b commit efc37fd

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""
2+
Categorical Cross-Entropy Loss
3+
4+
This function calculates the Categorical Cross-Entropy Loss between true class
5+
labels and predicted class probabilities.
6+
7+
Formula:
8+
Categorical Cross-Entropy Loss = -Σ(y_true * ln(y_pred))
9+
10+
Resources:
11+
- [Wikipedia - Cross entropy](https://en.wikipedia.org/wiki/Cross_entropy)
12+
"""
13+
14+
import numpy as np
15+
16+
17+
def categorical_cross_entropy(
18+
y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-15
19+
) -> float:
20+
"""
21+
Calculate Categorical Cross-Entropy Loss between true class labels and
22+
predicted class probabilities.
23+
24+
Parameters:
25+
- y_true: True class labels (one-hot encoded) as a NumPy array.
26+
- y_pred: Predicted class probabilities as a NumPy array.
27+
- epsilon: Small constant to avoid numerical instability.
28+
29+
Returns:
30+
- ce_loss: Categorical Cross-Entropy Loss as a floating-point number.
31+
32+
Example:
33+
>>> true_labels = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
34+
>>> pred_probs = np.array([[0.9, 0.1, 0.0], [0.2, 0.7, 0.1], [0.0, 0.1, 0.9]])
35+
>>> categorical_cross_entropy(true_labels, pred_probs)
36+
0.567395975254385
37+
38+
>>> y_true = np.array([[1, 0], [0, 1]])
39+
>>> y_pred = np.array([[0.9, 0.1, 0.0], [0.2, 0.7, 0.1]])
40+
>>> categorical_cross_entropy(y_true, y_pred)
41+
Traceback (most recent call last):
42+
...
43+
ValueError: Input arrays must have the same shape.
44+
45+
>>> y_true = np.array([[2, 0, 1], [1, 0, 0]])
46+
>>> y_pred = np.array([[0.9, 0.1, 0.0], [0.2, 0.7, 0.1]])
47+
>>> categorical_cross_entropy(y_true, y_pred)
48+
Traceback (most recent call last):
49+
...
50+
ValueError: y_true must be one-hot encoded.
51+
52+
>>> y_true = np.array([[1, 0, 1], [1, 0, 0]])
53+
>>> y_pred = np.array([[0.9, 0.1, 0.0], [0.2, 0.7, 0.1]])
54+
>>> categorical_cross_entropy(y_true, y_pred)
55+
Traceback (most recent call last):
56+
...
57+
ValueError: y_true must be one-hot encoded.
58+
59+
>>> y_true = np.array([[1, 0, 0], [0, 1, 0]])
60+
>>> y_pred = np.array([[0.9, 0.1, 0.1], [0.2, 0.7, 0.1]])
61+
>>> categorical_cross_entropy(y_true, y_pred)
62+
Traceback (most recent call last):
63+
...
64+
ValueError: Predicted probabilities must sum to approximately 1.
65+
"""
66+
if y_true.shape != y_pred.shape:
67+
raise ValueError("Input arrays must have the same shape.")
68+
69+
if np.any((y_true != 0) & (y_true != 1)) or np.any(y_true.sum(axis=1) != 1):
70+
raise ValueError("y_true must be one-hot encoded.")
71+
72+
if not np.all(np.isclose(np.sum(y_pred, axis=1), 1, rtol=epsilon, atol=epsilon)):
73+
raise ValueError("Predicted probabilities must sum to approximately 1.")
74+
75+
# Clip predicted probabilities to avoid log(0)
76+
y_pred = np.clip(y_pred, epsilon, 1)
77+
78+
# Calculate categorical cross-entropy loss
79+
return -np.sum(y_true * np.log(y_pred))
80+
81+
82+
if __name__ == "__main__":
83+
import doctest
84+
85+
doctest.testmod()

0 commit comments

Comments
 (0)