1
+ macro onlyreal (ex)
2
+ @capture (ex, (f_ (x, a__) = body_) | (function f_ (x, a__) body_ end )) ||
3
+ error (" expected a function with initial argument `x`" )
4
+
5
+ errmsg = " Use explicit invocations such as `$(f) .(x)` to apply activation functions to tensors!"
6
+
7
+ quote
8
+ Base. @__doc__ $ (f)(x:: Real , $ (a... )) = $ body
9
+ $ (f)(x:: AbstractArray , $ (a... )) = error ($ errmsg)
10
+ end |> esc
11
+ end
12
+
1
13
"""
2
14
σ(x) = 1 / (1 + exp(-x))
3
15
4
16
Classic [sigmoid](https://en.wikipedia.org/wiki/Sigmoid_function) activation
5
17
function.
6
18
"""
7
- σ (x) = one (x) / (one (x) + exp (- x))
19
+ @onlyreal σ (x) = one (x) / (one (x) + exp (- x))
8
20
9
21
const sigmoid = σ
10
22
11
23
# ForwardDiff numerical stability hack
12
- σ_stable (x) = ifelse (x < - 80 , zero (x), one (x) / (one (x) + exp (- x)))
24
+ @onlyreal σ_stable (x) = ifelse (x < - 80 , zero (x), one (x) / (one (x) + exp (- x)))
13
25
14
26
σ (x:: Float32 ) = σ_stable (x)
15
27
@@ -30,7 +42,7 @@ Return `log(σ(x))` which is computed in a numerically stable way.
30
42
-10.0
31
43
-0.0
32
44
"""
33
- function logσ (x)
45
+ @onlyreal function logσ (x)
34
46
max_v = max (zero (x), - x)
35
47
z = exp (- max_v) + exp (- x- max_v)
36
48
- (max_v + log (z))
@@ -44,7 +56,7 @@ const logsigmoid = logσ
44
56
[Rectified Linear Unit](https://en.wikipedia.org/wiki/Rectifier_(neural_networks))
45
57
activation function.
46
58
"""
47
- relu (x) = max (zero (x), x)
59
+ @onlyreal relu (x) = max (zero (x), x)
48
60
49
61
50
62
"""
@@ -54,7 +66,7 @@ Leaky [Rectified Linear Unit](https://en.wikipedia.org/wiki/Rectifier_(neural_ne
54
66
activation function.
55
67
You can also specify the coefficient explicitly, e.g. `leakyrelu(x, 0.01)`.
56
68
"""
57
- leakyrelu (x, a = oftype (x/ 1 , 0.01 )) = max (a* x, x/ 1 )
69
+ @onlyreal leakyrelu (x, a = oftype (x/ 1 , 0.01 )) = max (a* x, x/ 1 )
58
70
59
71
"""
60
72
elu(x, α = 1) =
@@ -64,15 +76,15 @@ Exponential Linear Unit activation function.
64
76
See [Fast and Accurate Deep Network Learning by Exponential Linear Units](https://arxiv.org/abs/1511.07289).
65
77
You can also specify the coefficient explicitly, e.g. `elu(x, 1)`.
66
78
"""
67
- elu (x, α = one (x)) = ifelse (x ≥ 0 , x/ 1 , α * (exp (x) - one (x)))
79
+ @onlyreal elu (x, α = one (x)) = ifelse (x ≥ 0 , x/ 1 , α * (exp (x) - one (x)))
68
80
69
81
"""
70
82
gelu(x) = 0.5x*(1 + tanh(√(2/π)*(x + 0.044715x^3)))
71
83
72
84
[Gaussian Error Linear Unit](https://arxiv.org/pdf/1606.08415.pdf)
73
85
activation function.
74
86
"""
75
- function gelu (x)
87
+ @onlyreal function gelu (x)
76
88
λ = oftype (x/ 1 , √ (2 / π))
77
89
α = oftype (x/ 1 , 0.044715 )
78
90
h = oftype (x/ 1 , 0.5 )
86
98
Self-gated actvation function.
87
99
See [Swish: a Self-Gated Activation Function](https://arxiv.org/pdf/1710.05941.pdf).
88
100
"""
89
- swish (x) = x * σ (x)
101
+ @onlyreal swish (x) = x * σ (x)
90
102
91
103
"""
92
104
selu(x) = λ * (x ≥ 0 ? x : α * (exp(x) - 1))
@@ -97,7 +109,7 @@ swish(x) = x * σ(x)
97
109
Scaled exponential linear units.
98
110
See [Self-Normalizing Neural Networks](https://arxiv.org/pdf/1706.02515.pdf).
99
111
"""
100
- function selu (x)
112
+ @onlyreal function selu (x)
101
113
λ = oftype (x/ 1 , 1.0507009873554804934193349852946 )
102
114
α = oftype (x/ 1 , 1.6732632423543772848170429916717 )
103
115
λ * ifelse (x > 0 , x/ 1 , α * (exp (x) - 1 ))
@@ -108,12 +120,12 @@ end
108
120
109
121
See [Quadratic Polynomials Learn Better Image Features](http://www.iro.umontreal.ca/~lisa/publications2/index.php/attachments/single/205).
110
122
"""
111
- softsign (x) = x / (one (x) + abs (x))
123
+ @onlyreal softsign (x) = x / (one (x) + abs (x))
112
124
113
125
114
126
"""
115
127
softplus(x) = log(exp(x) + 1)
116
128
117
129
See [Deep Sparse Rectifier Neural Networks](http://proceedings.mlr.press/v15/glorot11a/glorot11a.pdf).
118
130
"""
119
- softplus (x) = log1p (exp (x))
131
+ @onlyreal softplus (x) = log1p (exp (x))
0 commit comments