-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswish.go
58 lines (48 loc) · 1.41 KB
/
swish.go
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
package swish
import "math"
// Thanks https://codingforspeed.com/using-faster-exponential-approximation/
func exp256(x float64) float64 {
x = 1.0 + x/256.0
x *= x
x *= x
x *= x
x *= x
x *= x
x *= x
x *= x
x *= x
return x
}
// Swish is the x / (1 + exp(-x)) activation function, using exp256
func Swish(x float64) float64 {
return x / (1.0 + exp256(-x))
}
// Sigmoid is the 1 / (1 + exp(-x)) activation function, using exp256
func Sigmoid(x float64) float64 {
// Uses exp256 instead of math.Exp
return 1.0 / (1.0 + exp256(-x))
}
// SoftPlus is the log(1 + exp(x)) function, using exp256
func SoftPlus(x float64) float64 {
return math.Log(1.0 + exp256(x))
}
// Gaussian01 is the Gaussian function with mean 0 and sigma 1, using exp256
func Gaussian01(x float64) float64 {
return exp256(-(x * x) / 2.0)
}
// SwishPrecise is the x / (1 + exp(-x)) activation function, using math.Exp
func SwishPrecise(x float64) float64 {
return x / (1.0 + math.Exp(-x))
}
// SigmoidPrecise is the 1 / (1 + exp(-x)) activation function, using math.Exp
func SigmoidPrecise(x float64) float64 {
return 1.0 / (1.0 + math.Exp(-x))
}
// SoftPlusPrecise is the log(1 + exp(x)) function, using math.Exp
func SoftPlusPrecise(x float64) float64 {
return math.Log(1.0 + math.Exp(x))
}
// Gaussian01 is the Gaussian function with mean 0 and sigma 1, using math.Exp
func Gaussian01Precise(x float64) float64 {
return math.Exp(-(x * x) / 2.0)
}