concision_core/activate/traits/
activate.rs1use super::unary::*;
6
7use crate::Apply;
8#[cfg(feature = "complex")]
9use num_complex::ComplexFloat;
10use num_traits::One;
11
12pub trait Rho<U>: Apply<U> {
19    fn linear(&self) -> Self::Cont<U> {
22        self.apply(|x| x)
23    }
24
25    fn linear_derivative(&self) -> Self::Cont<U::Output>
26    where
27        U: One,
28    {
29        self.apply(|_| <U>::one())
30    }
31
32    fn heavyside(&self) -> Self::Cont<U::Output>
33    where
34        U: Heavyside,
35    {
36        self.apply(|x| x.heavyside())
37    }
38
39    fn heavyside_derivative(&self) -> Self::Cont<U::Output>
40    where
41        U: Heavyside,
42    {
43        self.apply(|x| x.heavyside_derivative())
44    }
45
46    fn relu(&self) -> Self::Cont<U::Output>
47    where
48        U: ReLU,
49    {
50        self.apply(|x| x.relu())
51    }
52
53    fn relu_derivative(&self) -> Self::Cont<U::Output>
54    where
55        U: ReLU,
56    {
57        self.apply(|x| x.relu_derivative())
58    }
59
60    fn sigmoid(&self) -> Self::Cont<U::Output>
61    where
62        U: Sigmoid,
63    {
64        self.apply(|x| x.sigmoid())
65    }
66
67    fn sigmoid_derivative(&self) -> Self::Cont<U::Output>
68    where
69        U: Sigmoid,
70    {
71        self.apply(|x| x.sigmoid_derivative())
72    }
73
74    fn tanh(&self) -> Self::Cont<U::Output>
75    where
76        U: Tanh,
77    {
78        self.apply(|x| x.tanh())
79    }
80
81    fn tanh_derivative(&self) -> Self::Cont<U::Output>
82    where
83        U: Tanh,
84    {
85        self.apply(|x| x.tanh_derivative())
86    }
87}
88
89#[cfg(feature = "complex")]
90pub trait RhoComplex<U>: Apply<U>
103where
104    U: ComplexFloat,
105{
106    fn sigmoid(&self) -> Self::Cont<U> {
107        self.apply(|x| U::one() / (U::one() + (-x).exp()))
108    }
109
110    fn sigmoid_derivative(&self) -> Self::Cont<U> {
111        self.apply(|x| {
112            let s = U::one() / (U::one() + (-x).exp());
113            s * (U::one() - s)
114        })
115    }
116
117    fn tanh(&self) -> Self::Cont<U> {
118        self.apply(|x| x.tanh())
119    }
120
121    fn tanh_derivative(&self) -> Self::Cont<U> {
122        self.apply(|x| {
123            let s = x.tanh();
124            U::one() - s * s
125        })
126    }
127}
128
129impl<U, S> Rho<U> for S where S: Apply<U> {}
133
134#[cfg(feature = "complex")]
135impl<U, S> RhoComplex<U> for S
136where
137    S: Apply<U>,
138    U: ComplexFloat,
139{
140}