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
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! # activation functions
//! 
//! `activation_functions` is a collection of functions, which can be used as
//! activation function for machine learning

/// containing the activation functions which need an f32 parameter and return a f32
pub mod f32 {
    /// calculate the `sigmoid` to the given f32 number
    /// 
    /// # Examples
    /// 
    /// ```
    /// let x:f32 = 0.5;
    /// let answer:f32 = sigmoid(x);
    /// 
    /// println!("sigmoid({}) => {}",x,answer);
    /// ```
    pub fn sigmoid(x:f32) -> f32 {
        1 as f32 / (1 as f32 + std::f32::consts::E.powf(-x))
    }
    /// calculate the `binary step` to the given f32 number
    /// 
    /// # Examples
    /// 
    /// ```
    /// let x:f32 = 0.5;
    /// let answer:f32 = bstep(x);
    /// 
    /// println!("bstep({}) => {}",x,answer);
    /// ```
    pub fn bstep(x:f32) -> f32 {
        if x<0 as f32 {
            0 as f32
        } else {
            1 as f32
        }
    }
    /// calculate the `tanh` to the given f32 number
    /// 
    /// # Examples
    /// 
    /// ```
    /// let x:f32 = 0.5;
    /// let answer:f32 = tanh(x);
    /// 
    /// println!("tanh({}) => {}",x,answer);
    /// ```
    pub fn tanh(x:f32) -> f32 {
        (std::f32::consts::E.powf(x) - std::f32::consts::E.powf(-x)) / (std::f32::consts::E.powf(x) + std::f32::consts::E.powf(-x))
    }
    /// calculate the `rectified linear unit` to the given f32 number
    /// 
    /// # Examples
    /// 
    /// ```
    /// let x:f32 = 0.5;
    /// let answer:f32 = relu(x);
    /// 
    /// println!("relu({}) => {}",x,answer);
    /// ```
    pub fn relu(x:f32) -> f32 {
        if x<=0 as f32 {
            0 as f32
        } else {
            1 as f32
        }
    }
    /// calculate the `sigmoid linear unit` to the given f32 number
    /// 
    /// # Examples
    /// 
    /// ```
    /// let x:f32 = 0.5;
    /// let answer:f32 = silu(x);
    /// 
    /// println!("silu({}) => {}",x,answer);
    /// ```
    pub fn silu(x:f32) -> f32 {
        x / (1 as f32 + std::f32::consts::E.powf(-x))
    }
    /// calculate the `gaussian` to the given f32 number
    /// 
    /// # Examples
    /// 
    /// ```
    /// let x:f32 = 0.5;
    /// let answer:f32 = gaussian(x);
    /// 
    /// println!("gaussian({}) => {}",x,answer);
    /// ```
    pub fn gaussian(x:f32) -> f32 {
        std::f32::consts::E.powf(-(x*x))
    }
}

/// containing the activation functions which need an f64 parameter and return a f64
pub mod f64 {
    /// calculate the `sigmoid` to the given f64 number
    /// 
    /// # Examples
    /// 
    /// ```
    /// let x:f64 = 0.5;
    /// let answer:f64 = sigmoid(x);
    /// 
    /// println!("sigmoid({}) => {}",x,answer);
    /// ```
    pub fn sigmoid(x:f64) -> f64 {
        1 as f64 / (1 as f64 + std::f64::consts::E.powf(-x))
    }
    /// calculate the `binary step` to the given f64 number
    /// 
    /// # Examples
    /// 
    /// ```
    /// let x:f64 = 0.5;
    /// let answer:f64 = bstep(x);
    /// 
    /// println!("bstep({}) => {}",x,answer);
    /// ```
    pub fn bstep(x:f64) -> f64 {
        if x<0 as f64 {            0 as f64        } else {            1 as f64        }
    }
    /// calculate the `tanh` to the given f64 number
    /// 
    /// # Examples
    /// 
    /// ```
    /// let x:f64 = 0.5;
    /// let answer:f64 = tanh(x);
    /// 
    /// println!("tanh({}) => {}",x,answer);
    /// ```
    pub fn tanh(x:f64) -> f64 {
        (std::f64::consts::E.powf(x) - std::f64::consts::E.powf(-x)) / (std::f64::consts::E.powf(x) + std::f64::consts::E.powf(-x))
    }
    /// calculate the `rectified linear unit` to the given f64 number
    /// 
    /// # Examples
    /// 
    /// ```
    /// let x:f64 = 0.5;
    /// let answer:f64 = relu(x);
    /// 
    /// println!("relu({}) => {}",x,answer);
    /// ```
    pub fn relu(x:f64) -> f64 {
        if x<=0 as f64 {
            0 as f64
        } else {
            1 as f64
        }
    }
    /// calculate the `sigmoid linear unit` to the given f64 number
    /// 
    /// # Examples
    /// 
    /// ```
    /// let x:f64 = 0.5;
    /// let answer:f64 = silu(x);
    /// 
    /// println!("silu({}) => {}",x,answer);
    /// ```
    pub fn silu(x:f64) -> f64 {
        x / (1 as f64 + std::f64::consts::E.powf(-x))
    }
    /// calculate the `gaussian` to the given f64 number
    /// 
    /// # Examples
    /// 
    /// ```
    /// let x:f64 = 0.5;
    /// let answer:f64 = gaussian(x);
    /// 
    /// println!("gaussian({}) => {}",x,answer);
    /// ```
    pub fn gaussian(x:f64) -> f64 {
        std::f64::consts::E.powf(-(x*x))
    }
}