rust_lstm/
utils.rs

1/// Utility functions for the LSTM library.
2
3/// Sigmoid activation function: σ(x) = 1 / (1 + e^(-x))
4pub fn sigmoid(x: f64) -> f64 {
5    1.0 / (1.0 + (-x).exp())
6}
7
8/// Hyperbolic tangent activation: tanh(x) = (e^x - e^(-x)) / (e^x + e^(-x))
9pub fn tanh(x: f64) -> f64 {
10    x.tanh()
11}
12
13#[cfg(test)]
14mod tests {
15    use super::*;
16
17    #[test]
18    fn test_sigmoid() {
19        assert!((sigmoid(0.0) - 0.5).abs() < 1e-10);
20        assert!(sigmoid(1000.0) > 0.99);
21        assert!(sigmoid(-1000.0) < 0.01);
22    }
23
24    #[test]
25    fn test_tanh() {
26        assert!((tanh(0.0) - 0.0).abs() < 1e-10);
27        assert!(tanh(1000.0) > 0.99);
28        assert!(tanh(-1000.0) < -0.99);
29    }
30}