Skip to content

Commit 9e41aad

Browse files
authored
Add KL divergence loss function (TheAlgorithms#656)
1 parent cb3b21d commit 9e41aad

4 files changed

Lines changed: 41 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
* Loss Function
146146
* [Mean Absolute Error Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/mean_absolute_error_loss.rs)
147147
* [Mean Squared Error Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/mean_squared_error_loss.rs)
148+
* [KL Divergence Loss](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/loss_function/kl_divergence_loss.rs)
148149
* Optimization
149150
* [Adam](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/optimization/adam.rs)
150151
* [Gradient Descent](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/optimization/gradient_descent.rs)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//! # KL divergence Loss Function
2+
//!
3+
//! For a pair of actual and predicted probability distributions represented as vectors `actual` and `predicted`, the KL divergence loss is calculated as:
4+
//!
5+
//! `L = -Σ(actual[i] * ln(predicted[i]/actual[i]))` for all `i` in the range of the vectors
6+
//!
7+
//! Where `ln` is the natural logarithm function, and `Σ` denotes the summation over all elements of the vectors.
8+
//!
9+
//! ## KL divergence Loss Function Implementation
10+
//!
11+
//! This implementation takes two references to vectors of f64 values, `actual` and `predicted`, and returns the KL divergence loss between them.
12+
//!
13+
pub fn kld_loss(actual: &[f64], predicted: &[f64]) -> f64 {
14+
// epsilon to handle if any of the elements are zero
15+
let eps = 0.00001f64;
16+
let loss: f64 = actual
17+
.iter()
18+
.zip(predicted.iter())
19+
.map(|(&a, &p)| ((a + eps) * ((a + eps) / (p + eps)).ln()))
20+
.sum();
21+
loss
22+
}
23+
24+
#[cfg(test)]
25+
mod tests {
26+
use super::*;
27+
28+
#[test]
29+
fn test_kld_loss() {
30+
let test_vector_actual = vec![1.346112, 1.337432, 1.246655];
31+
let test_vector = vec![1.033836, 1.082015, 1.117323];
32+
assert_eq!(
33+
kld_loss(&test_vector_actual, &test_vector),
34+
0.7752789394328498
35+
);
36+
}
37+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
mod kl_divergence_loss;
12
mod mean_absolute_error_loss;
23
mod mean_squared_error_loss;
34

5+
pub use self::kl_divergence_loss::kld_loss;
46
pub use self::mean_absolute_error_loss::mae_loss;
57
pub use self::mean_squared_error_loss::mse_loss;

src/machine_learning/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod simpson;
88
pub use self::cholesky::cholesky;
99
pub use self::k_means::k_means;
1010
pub use self::linear_regression::linear_regression;
11+
pub use self::loss_function::kld_loss;
1112
pub use self::loss_function::mae_loss;
1213
pub use self::loss_function::mse_loss;
1314
pub use self::optimization::gradient_descent;

0 commit comments

Comments
 (0)