MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / categorical_cross_entropy

Function categorical_cross_entropy

machine_learning/loss_functions.py:93–148  ·  view source on GitHub ↗

Calculate categorical cross-entropy (CCE) loss between true class labels and predicted class probabilities. CCE = -Σ(y_true * ln(y_pred)) Reference: https://en.wikipedia.org/wiki/Cross_entropy Parameters: - y_true: True class labels (one-hot encoded) - y_pred: Predict

(
    y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-15
)

Source from the content-addressed store, hash-verified

91
92
93def categorical_cross_entropy(
94 y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-15
95) -> float:
96 """
97 Calculate categorical cross-entropy (CCE) loss between true class labels and
98 predicted class probabilities.
99
100 CCE = -Σ(y_true * ln(y_pred))
101
102 Reference: https://en.wikipedia.org/wiki/Cross_entropy
103
104 Parameters:
105 - y_true: True class labels (one-hot encoded)
106 - y_pred: Predicted class probabilities
107 - epsilon: Small constant to avoid numerical instability
108
109 >>> true_labels = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
110 >>> pred_probs = np.array([[0.9, 0.1, 0.0], [0.2, 0.7, 0.1], [0.0, 0.1, 0.9]])
111 >>> float(categorical_cross_entropy(true_labels, pred_probs))
112 0.567395975254385
113 >>> true_labels = np.array([[1, 0], [0, 1]])
114 >>> pred_probs = np.array([[0.9, 0.1, 0.0], [0.2, 0.7, 0.1]])
115 >>> categorical_cross_entropy(true_labels, pred_probs)
116 Traceback (most recent call last):
117 ...
118 ValueError: Input arrays must have the same shape.
119 >>> true_labels = np.array([[2, 0, 1], [1, 0, 0]])
120 >>> pred_probs = np.array([[0.9, 0.1, 0.0], [0.2, 0.7, 0.1]])
121 >>> categorical_cross_entropy(true_labels, pred_probs)
122 Traceback (most recent call last):
123 ...
124 ValueError: y_true must be one-hot encoded.
125 >>> true_labels = np.array([[1, 0, 1], [1, 0, 0]])
126 >>> pred_probs = np.array([[0.9, 0.1, 0.0], [0.2, 0.7, 0.1]])
127 >>> categorical_cross_entropy(true_labels, pred_probs)
128 Traceback (most recent call last):
129 ...
130 ValueError: y_true must be one-hot encoded.
131 >>> true_labels = np.array([[1, 0, 0], [0, 1, 0]])
132 >>> pred_probs = np.array([[0.9, 0.1, 0.1], [0.2, 0.7, 0.1]])
133 >>> categorical_cross_entropy(true_labels, pred_probs)
134 Traceback (most recent call last):
135 ...
136 ValueError: Predicted probabilities must sum to approximately 1.
137 """
138 if y_true.shape != y_pred.shape:
139 raise ValueError("Input arrays must have the same shape.")
140
141 if np.any((y_true != 0) & (y_true != 1)) or np.any(y_true.sum(axis=1) != 1):
142 raise ValueError("y_true must be one-hot encoded.")
143
144 if not np.all(np.isclose(np.sum(y_pred, axis=1), 1, rtol=epsilon, atol=epsilon)):
145 raise ValueError("Predicted probabilities must sum to approximately 1.")
146
147 y_pred = np.clip(y_pred, epsilon, 1) # Clip predictions to avoid log(0)
148 return -np.sum(y_true * np.log(y_pred))
149
150

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected