Skip to content

Commit 6ca5b56

Browse files
THEGAMECHANGER416pre-commit-ci[bot]cclausstianyizheng02
committed
Created folder for losses in Machine_Learning (TheAlgorithms#9969)
* Created folder for losses in Machine_Learning * Update binary_cross_entropy.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update mean_squared_error.py * Update binary_cross_entropy.py * Update mean_squared_error.py * Update binary_cross_entropy.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update mean_squared_error.py * Update binary_cross_entropy.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update mean_squared_error.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update binary_cross_entropy.py * Update mean_squared_error.py * Update binary_cross_entropy.py * Update mean_squared_error.py * Update machine_learning/losses/binary_cross_entropy.py Co-authored-by: Christian Clauss <[email protected]> * Update machine_learning/losses/mean_squared_error.py Co-authored-by: Christian Clauss <[email protected]> * Update machine_learning/losses/binary_cross_entropy.py Co-authored-by: Christian Clauss <[email protected]> * Update mean_squared_error.py * Update machine_learning/losses/mean_squared_error.py Co-authored-by: Tianyi Zheng <[email protected]> * Update binary_cross_entropy.py * Update mean_squared_error.py * Update binary_cross_entropy.py * Update mean_squared_error.py * Update mean_squared_error.py * Update binary_cross_entropy.py * renamed: losses -> loss_functions * updated 2 files * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update mean_squared_error.py * Update mean_squared_error.py * Update binary_cross_entropy.py * Update mean_squared_error.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: Tianyi Zheng <[email protected]>
1 parent df099ca commit 6ca5b56

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
Binary Cross-Entropy (BCE) Loss Function
3+
4+
Description:
5+
Quantifies dissimilarity between true labels (0 or 1) and predicted probabilities.
6+
It's widely used in binary classification tasks.
7+
8+
Formula:
9+
BCE = -Σ(y_true * log(y_pred) + (1 - y_true) * log(1 - y_pred))
10+
11+
Source:
12+
[Wikipedia - Cross entropy](https://en.wikipedia.org/wiki/Cross_entropy)
13+
"""
14+
15+
import numpy as np
16+
17+
18+
def binary_cross_entropy(
19+
y_true: np.ndarray, y_pred: np.ndarray, epsilon: float = 1e-15
20+
) -> float:
21+
"""
22+
Calculate the BCE Loss between true labels and predicted probabilities.
23+
24+
Parameters:
25+
- y_true: True binary labels (0 or 1).
26+
- y_pred: Predicted probabilities for class 1.
27+
- epsilon: Small constant to avoid numerical instability.
28+
29+
Returns:
30+
- bce_loss: Binary Cross-Entropy Loss.
31+
32+
Example Usage:
33+
>>> true_labels = np.array([0, 1, 1, 0, 1])
34+
>>> predicted_probs = np.array([0.2, 0.7, 0.9, 0.3, 0.8])
35+
>>> binary_cross_entropy(true_labels, predicted_probs)
36+
0.2529995012327421
37+
>>> true_labels = np.array([0, 1, 1, 0, 1])
38+
>>> predicted_probs = np.array([0.3, 0.8, 0.9, 0.2])
39+
>>> binary_cross_entropy(true_labels, predicted_probs)
40+
Traceback (most recent call last):
41+
...
42+
ValueError: Input arrays must have the same length.
43+
"""
44+
if len(y_true) != len(y_pred):
45+
raise ValueError("Input arrays must have the same length.")
46+
# Clip predicted probabilities to avoid log(0) and log(1)
47+
y_pred = np.clip(y_pred, epsilon, 1 - epsilon)
48+
49+
# Calculate binary cross-entropy loss
50+
bce_loss = -(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred))
51+
52+
# Take the mean over all samples
53+
return np.mean(bce_loss)
54+
55+
56+
if __name__ == "__main__":
57+
import doctest
58+
59+
doctest.testmod()
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Mean Squared Error (MSE) Loss Function
3+
4+
Description:
5+
MSE measures the mean squared difference between true values and predicted values.
6+
It serves as a measure of the model's accuracy in regression tasks.
7+
8+
Formula:
9+
MSE = (1/n) * Σ(y_true - y_pred)^2
10+
11+
Source:
12+
[Wikipedia - Mean squared error](https://en.wikipedia.org/wiki/Mean_squared_error)
13+
"""
14+
15+
import numpy as np
16+
17+
18+
def mean_squared_error(y_true: np.ndarray, y_pred: np.ndarray) -> float:
19+
"""
20+
Calculate the Mean Squared Error (MSE) between two arrays.
21+
22+
Parameters:
23+
- y_true: The true values (ground truth).
24+
- y_pred: The predicted values.
25+
26+
Returns:
27+
- mse: The Mean Squared Error between y_true and y_pred.
28+
29+
Example usage:
30+
>>> true_values = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
31+
>>> predicted_values = np.array([0.8, 2.1, 2.9, 4.2, 5.2])
32+
>>> mean_squared_error(true_values, predicted_values)
33+
0.028000000000000032
34+
>>> true_labels = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
35+
>>> predicted_probs = np.array([0.3, 0.8, 0.9, 0.2])
36+
>>> mean_squared_error(true_labels, predicted_probs)
37+
Traceback (most recent call last):
38+
...
39+
ValueError: Input arrays must have the same length.
40+
"""
41+
if len(y_true) != len(y_pred):
42+
raise ValueError("Input arrays must have the same length.")
43+
44+
squared_errors = (y_true - y_pred) ** 2
45+
return np.mean(squared_errors)
46+
47+
48+
if __name__ == "__main__":
49+
import doctest
50+
51+
doctest.testmod()

0 commit comments

Comments
 (0)