-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
leaky_relu.rs
47 lines (43 loc) · 1.42 KB
/
leaky_relu.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! # Leaky ReLU Function
//!
//! The `leaky_relu` function computes the Leaky Rectified Linear Unit (ReLU) values of a given vector
//! of f64 numbers with a specified alpha parameter.
//!
//! The Leaky ReLU activation function is commonly used in neural networks to introduce a small negative
//! slope (controlled by the alpha parameter) for the negative input values, preventing neurons from dying
//! during training.
//!
//! ## Formula
//!
//! For a given input vector `x` and an alpha parameter `alpha`, the Leaky ReLU function computes the output
//! `y` as follows:
//!
//! `y_i = { x_i if x_i >= 0, alpha * x_i if x_i < 0 }`
//!
//! ## Leaky ReLU Function Implementation
//!
//! This implementation takes a reference to a vector of f64 values and an alpha parameter, and returns a new
//! vector with the Leaky ReLU transformation applied to each element. The input vector is not altered.
//!
pub fn leaky_relu(vector: &Vec<f64>, alpha: f64) -> Vec<f64> {
let mut _vector = vector.to_owned();
for value in &mut _vector {
if value < &mut 0. {
*value *= alpha;
}
}
_vector
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_leaky_relu() {
let test_vector = vec![-10., 2., -3., 4., -5., 10., 0.05];
let alpha = 0.01;
assert_eq!(
leaky_relu(&test_vector, alpha),
vec![-0.1, 2.0, -0.03, 4.0, -0.05, 10.0, 0.05]
);
}
}