From 4c626bf09cd9b6c1d52bb29ffbca29dd4ac008af Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sun, 6 Jul 2025 20:00:17 +0100 Subject: [PATCH] Add leaky_relu and dleaky_relu functions with corresponding unit tests --- src/compute/activations.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/compute/activations.rs b/src/compute/activations.rs index f25c00c..d2f3710 100644 --- a/src/compute/activations.rs +++ b/src/compute/activations.rs @@ -17,6 +17,13 @@ pub fn drelu(x: &Matrix) -> Matrix { x.map(|v| if v > 0.0 { 1.0 } else { 0.0 }) } +pub fn leaky_relu(x: &Matrix) -> Matrix { + x.map(|v| if v > 0.0 { v } else { 0.01 * v }) +} + +pub fn dleaky_relu(x: &Matrix) -> Matrix { + x.map(|v| if v > 0.0 { 1.0 } else { 0.01 }) +} mod tests { use super::*; @@ -57,4 +64,17 @@ mod tests { let expected = Matrix::from_vec(vec![0.0, 0.0, 1.0], 3, 1); assert_eq!(drelu(&x), expected); } -} \ No newline at end of file + #[test] + fn test_leaky_relu() { + let x = Matrix::from_vec(vec![-1.0, 0.0, 1.0], 3, 1); + let expected = Matrix::from_vec(vec![-0.01, 0.0, 1.0], 3, 1); + assert_eq!(leaky_relu(&x), expected); + } + #[test] + fn test_dleaky_relu() { + let x = Matrix::from_vec(vec![-1.0, 0.0, 1.0], 3, 1); + let expected = Matrix::from_vec(vec![0.01, 0.01, 1.0], 3, 1); + assert_eq!(dleaky_relu(&x), expected); + } + +}