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
use std::f64;
use activation::Activation;
#[derive(Copy, Clone)]
pub struct HyperbolicTangent;
impl HyperbolicTangent {
pub fn new() -> HyperbolicTangent {
return HyperbolicTangent;
}
}
impl Activation for HyperbolicTangent {
fn calc(&self, x: Vec<f64>) -> Vec<f64> {
x.iter().map(|n| n.tanh()).collect::<Vec<_>>()
}
fn derivative(&self, x: Vec<f64>) -> Vec<f64> {
x.iter()
.map(|n| {
let tanh_factor = n.tanh();
1f64 - (tanh_factor * tanh_factor)
})
.collect::<Vec<_>>()
}
}
#[cfg(test)]
mod tests {
use super::Activation;
use super::HyperbolicTangent;
#[test]
fn tanh_test() {
let activation = HyperbolicTangent::new();
assert_approx_eq!(activation.calc(vec![3f64])[0], 0.995054754f64);
}
#[test]
fn tanh_derivative_test() {
let activation = HyperbolicTangent::new();
assert_approx_eq!(activation.derivative(vec![3f64])[0], 0.0098660372f64);
}
}