Skip to content

Commit d3d0a8a

Browse files
authored
Add ReLU, Sigmoid, and Tanh functions (TheAlgorithms#534)
1 parent 69c76a3 commit d3d0a8a

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

src/math/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ mod prime_factors;
3535
mod prime_numbers;
3636
mod quadratic_residue;
3737
mod random;
38+
mod relu;
3839
pub mod sieve_of_eratosthenes;
40+
mod sigmoid;
3941
mod signum;
4042
mod simpson_integration;
4143
mod sine;
4244
mod square_pyramidal_numbers;
4345
mod square_root;
4446
mod sum_of_digits;
47+
mod tanh;
4548
mod trial_division;
4649
mod zellers_congruence_algorithm;
4750

@@ -88,12 +91,15 @@ pub use self::prime_factors::prime_factors;
8891
pub use self::prime_numbers::prime_numbers;
8992
pub use self::quadratic_residue::cipolla;
9093
pub use self::random::PCG32;
94+
pub use self::relu::relu;
9195
pub use self::sieve_of_eratosthenes::sieve_of_eratosthenes;
96+
pub use self::sigmoid::sigmoid;
9297
pub use self::signum::signum;
9398
pub use self::simpson_integration::simpson_integration;
9499
pub use self::sine::sine;
95100
pub use self::square_pyramidal_numbers::square_pyramidal_number;
96101
pub use self::square_root::{fast_inv_sqrt, square_root};
97102
pub use self::sum_of_digits::{sum_digits_iterative, sum_digits_recursive};
103+
pub use self::tanh::tanh;
98104
pub use self::trial_division::trial_division;
99105
pub use self::zellers_congruence_algorithm::zellers_congruence_algorithm;

src/math/relu.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//Rust implementation of the ReLU (rectified linear unit) activation function.
2+
//The formula for ReLU is quite simple really: (if x>0 -> x, else -> 0)
3+
//More information on the concepts of ReLU can be found here:
4+
//https://en.wikipedia.org/wiki/Rectifier_(neural_networks)
5+
6+
//The function below takes a reference to a mutable <f32> Vector as an argument
7+
//and returns the vector with 'ReLU' applied to all values.
8+
//Of course, these functions can be changed by the developer so that the input vector isn't manipulated.
9+
//This is simply an implemenation of the formula.
10+
11+
pub fn relu(array: &mut Vec<f32>) -> &mut Vec<f32> {
12+
//note that these calculations are assuming the Vector values consists of real numbers in radians
13+
for value in &mut *array {
14+
if value <= &mut 0. {
15+
*value = 0.;
16+
}
17+
}
18+
19+
array
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use super::*;
25+
26+
#[test]
27+
fn test_relu() {
28+
let mut test: Vec<f32> = Vec::from([1.0, 0.5, -1.0, 0.0, 0.3]);
29+
assert_eq!(
30+
relu(&mut test),
31+
&mut Vec::<f32>::from([1.0, 0.5, 0.0, 0.0, 0.3])
32+
);
33+
}
34+
}

src/math/sigmoid.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//Rust implementation of the Sigmoid activation function.
2+
//The formula for Sigmoid: 1 / (1 + e^(-x))
3+
//More information on the concepts of Sigmoid can be found here:
4+
//https://en.wikipedia.org/wiki/Sigmoid_function
5+
6+
//The function below takes a reference to a mutable <f32> Vector as an argument
7+
//and returns the vector with 'Sigmoid' applied to all values.
8+
//Of course, these functions can be changed by the developer so that the input vector isn't manipulated.
9+
//This is simply an implemenation of the formula.
10+
11+
use std::f32::consts::E;
12+
13+
pub fn sigmoid(array: &mut Vec<f32>) -> &mut Vec<f32> {
14+
//note that these calculations are assuming the Vector values consists of real numbers in radians
15+
for value in &mut *array {
16+
*value = 1. / (1. + E.powf(-1. * *value));
17+
}
18+
19+
array
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use super::*;
25+
26+
#[test]
27+
fn test_sigmoid() {
28+
let mut test = Vec::from([1.0, 0.5, -1.0, 0.0, 0.3]);
29+
assert_eq!(
30+
sigmoid(&mut test),
31+
&mut Vec::<f32>::from([0.7310586, 0.62245935, 0.26894143, 0.5, 0.5744425,])
32+
);
33+
}
34+
}

src/math/tanh.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//Rust implementation of the Tanh (hyperbolic tangent) activation function.
2+
//The formula for Tanh: (e^x - e^(-x))/(e^x + e^(-x)) OR (2/(1+e^(-2x))-1
3+
//More information on the concepts of Sigmoid can be found here:
4+
//https://en.wikipedia.org/wiki/Hyperbolic_functions
5+
6+
//The function below takes a reference to a mutable <f32> Vector as an argument
7+
//and returns the vector with 'Tanh' applied to all values.
8+
//Of course, these functions can be changed by the developer so that the input vector isn't manipulated.
9+
//This is simply an implemenation of the formula.
10+
11+
use std::f32::consts::E;
12+
13+
pub fn tanh(array: &mut Vec<f32>) -> &mut Vec<f32> {
14+
//note that these calculations are assuming the Vector values consists of real numbers in radians
15+
for value in &mut *array {
16+
*value = (2. / (1. + E.powf(-2. * *value))) - 1.;
17+
}
18+
19+
array
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use super::*;
25+
26+
#[test]
27+
fn test_tanh() {
28+
let mut test = Vec::from([1.0, 0.5, -1.0, 0.0, 0.3]);
29+
assert_eq!(
30+
tanh(&mut test),
31+
&mut Vec::<f32>::from([0.76159406, 0.4621172, -0.7615941, 0.0, 0.29131258,])
32+
);
33+
}
34+
}

0 commit comments

Comments
 (0)