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
mod arg_max_min;
mod avgpool;
mod batch_norm;
mod conv;
mod data_formats;
mod global_pools;
mod layer_max;
mod lrn;
mod maxpool;
mod padding;
mod patches;
mod reduce;

pub use self::arg_max_min::ArgMaxMin;
pub use self::avgpool::AvgPool;
pub use self::batch_norm::BatchNorm;
pub use self::conv::{Conv, ConvUnary, KernelFormat};
pub use self::data_formats::{DataFormat, DataShape};
pub use self::global_pools::{GlobalAvgPool, GlobalLpPool, GlobalMaxPool};
pub use self::layer_max::{LayerHardmax, LayerLogSoftmax, LayerSoftmax};
pub use self::lrn::Lrn;
pub use self::maxpool::MaxPool;
pub use self::padding::PaddingSpec;
pub use self::patches::Patch;
pub use self::reduce::{Reduce, Reducer};

use num_traits::AsPrimitive;

element_map!(Relu, [f32, i32], |x| if x < 0 as _ { 0 as _ } else { x });
element_map!(Sigmoid, [f32], |x| ((-x).exp() + 1.0).recip());
element_map!(Softplus, [f32], |x| (x.exp() + 1.0).ln());
element_map!(Softsign, [f32], |x| x / (x.abs() + 1.0));

element_map_with_params!(
    Elu,
    [f32, f64],
    { alpha: f32 },
    fn eval_one<T>(elu: &Elu, x: T) -> T
    where
        T: Datum + ::num_traits::Float,
        f32: ::num_traits::AsPrimitive<T>,
    {
        if x < 0.0.as_() {
            elu.alpha.as_() * (x.exp() - 1.0.as_())
        } else {
            x
        }
    }
);

element_map_with_params!(Hardsigmoid, [f32, f64], {alpha: f32, beta: f32},
    fn eval_one<T>(hs: &Hardsigmoid, x:T) -> T
    where T: Datum+::num_traits::Float, f32: ::num_traits::AsPrimitive<T>
    {
        (hs.alpha.as_() * x + hs.beta.as_()).min(1.0.as_()).max(0.0.as_())
    }
);

element_map_with_params!(
    LeakyRelu,
    [f32, f64],
    { alpha: f32 },
    fn eval_one<T>(lr: &LeakyRelu, x: T) -> T
    where
        T: Datum + ::num_traits::Float,
        f32: ::num_traits::AsPrimitive<T>,
    {
        if x < 0.0.as_() {
            lr.alpha.as_() * x
        } else {
            x
        }
    }
);

element_map_with_params!(ParametricSoftplus, [f32, f64], {alpha: f32, beta: f32},
    fn eval_one<T>(s: &ParametricSoftplus, x:T) -> T
    where T: Datum+::num_traits::Float, f32: ::num_traits::AsPrimitive<T>
    {
        s.alpha.as_() * ((s.beta.as_() * x).exp() + 1.0.as_()).ln()
    }
);

element_map_with_params!(ScaledTanh, [f32, f64], {alpha: f32, beta: f32},
    fn eval_one<T>(s: &ScaledTanh, x:T) -> T
    where T: Datum+::num_traits::Float, f32: ::num_traits::AsPrimitive<T>
    {
        s.alpha.as_() * (s.beta.as_() * x).tanh()
    }
);

element_map_with_params!(Selu, [f32, f64], {alpha: f32, gamma: f32},
    fn eval_one<T>(s: &Selu, x:T) -> T
    where T: Datum+::num_traits::Float, f32: ::num_traits::AsPrimitive<T>
    {
        if x < 0.0.as_() {
            s.gamma.as_() * (s.alpha.as_() * x.exp() - s.alpha.as_())
        } else {
            s.gamma.as_() * x
        }
    }
);

element_map_with_params!(
    ThresholdedRelu,
    [f32, f64],
    { alpha: f32 },
    fn eval_one<T>(s: &ThresholdedRelu, x: T) -> T
    where
        T: Datum + ::num_traits::Float,
        f32: ::num_traits::AsPrimitive<T>,
    {
        if x <= s.alpha.as_() {
            0.0.as_()
        } else {
            x
        }
    }
);