tract_linalg/frame/
sigmoid.rs

1macro_rules! sigmoid_impl {
2    ($ti: ident, $func: ident, $nr: expr, $alignment_items: expr, $cond: expr) => {
3        ew_impl!($ti, $func, $nr, $alignment_items);
4        #[cfg(test)]
5        paste! {
6            mod [<test_ $func>] {
7                use super::*;
8                sigmoid_frame_tests!($cond, $ti, $func);
9            }
10        }
11    };
12}
13
14#[cfg(test)]
15#[macro_use]
16pub mod test {
17    use crate::{frame::element_wise::*, LADatum};
18    use num_traits::{AsPrimitive, Float};
19    use proptest::test_runner::TestCaseResult;
20
21    #[macro_export]
22    macro_rules! sigmoid_frame_tests {
23        ($cond:expr, $t: ty, $ker:ty) => {
24            proptest::proptest! {
25                #[test]
26                fn sigmoid(xs in proptest::collection::vec(-25f32..25.0, 0..100)) {
27                    if $cond {
28                        $crate::frame::sigmoid::test::test_sigmoid::<$ker, $t>(&*xs).unwrap()
29                    }
30                }
31            }
32
33            #[test]
34            fn sigmoid_4_magic() {
35                if $cond {
36                    $crate::frame::sigmoid::test::test_sigmoid::<$ker, $t>(&[
37                        0f32, -20.0, 20.0, 0.0,
38                    ])
39                    .unwrap()
40                }
41            }
42
43            #[test]
44            fn sigmoid_4zeros() {
45                if $cond {
46                    $crate::frame::sigmoid::test::test_sigmoid::<$ker, $t>(&[0.0; 4]).unwrap();
47                }
48            }
49
50            #[test]
51            fn sigmoid_20_ones() {
52                if $cond {
53                    $crate::frame::sigmoid::test::test_sigmoid::<$ker, $t>(&[1.0; 20]).unwrap();
54                }
55            }
56
57            #[test]
58            fn sigmoid_18_zeros() {
59                if $cond {
60                    $crate::frame::sigmoid::test::test_sigmoid::<$ker, $t>(&[0.0; 18]).unwrap();
61                }
62            }
63
64            #[test]
65            fn sigmoid_asymptots() {
66                use tract_data::internal::*;
67                use $crate::frame::element_wise::*;
68                if $cond {
69                    let mut input: Vec<$t> = [-100f32, 100f32]
70                        .iter()
71                        .map(|x| <f32 as num_traits::AsPrimitive<$t>>::as_(*x))
72                        .collect();
73                    let expected: Vec<$t> = [-0f32, 1f32]
74                        .iter()
75                        .map(|x| <f32 as num_traits::AsPrimitive<$t>>::as_(*x))
76                        .collect();
77                    <$ker>::ew().run(&mut input).unwrap();
78                    tensor1(&input)
79                        .close_enough(&tensor1(&expected), Approximation::Close)
80                        .unwrap();
81                }
82            }
83        };
84    }
85
86    pub fn test_sigmoid<K: ElementWiseKer<T>, T: LADatum + Float>(values: &[f32]) -> TestCaseResult
87    where
88        f32: AsPrimitive<T>,
89    {
90        crate::setup_test_logger();
91        let values: Vec<T> = values.iter().copied().map(|x| x.as_()).collect();
92        crate::frame::element_wise::test::test_element_wise::<K, _, _>(&values, |x| {
93            (1f32).as_() / (1f32.as_() + (-x).exp())
94        })
95    }
96}