-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfunctions.go
104 lines (85 loc) · 1.76 KB
/
functions.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
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
package fonet
import "math"
// Activation functions available to this package. These are taken from
// https://en.wikipedia.org/wiki/Activation_function
const (
Sigmond ActivationFunction = iota
BentIdentity
ReLU
LeakyReLU
ArSinH
)
var (
functionPairs = map[ActivationFunction]funcPair{
Sigmond: {sigmoid, sigmoidD},
BentIdentity: {bentIdent, bentIdentD},
ReLU: {reLU, reLUD},
LeakyReLU: {leakyReLU, leakyReLUD},
ArSinH: {arSinH, arSinHD},
}
)
// ActivationFunction is the type of function to use for the neural network.
type ActivationFunction int
func (a ActivationFunction) String() string {
switch a {
case Sigmond:
return "Sigmond"
case BentIdentity:
return "Bent Identity"
case ReLU:
return "Rectified linear unit"
case LeakyReLU:
return "Leaky rectified linear unit"
case ArSinH:
return "ArSinH"
default:
return "Unknown"
}
}
type funcPair [2]func(float64) float64
func sigmoid(z float64) float64 {
return 1. / (1. + math.Exp(-z))
}
func sigmoidD(z float64) float64 {
return sigmoid(z) * (1 - sigmoid(z))
}
func bentIdent(z float64) float64 {
numerator := (math.Sqrt((z * z) + 1)) - 1
denominator := 2.0
return (numerator / denominator) + z
}
func bentIdentD(z float64) float64 {
numerator := z
denominator := 2 * math.Sqrt((z*z)+1)
return (numerator / denominator) + 1
}
func reLU(z float64) float64 {
if z > 0 {
return z
}
return 0
}
func reLUD(z float64) float64 {
if z > 0 {
return 1
}
return 0
}
func leakyReLU(z float64) float64 {
if z < 0 {
return 0.01 * z
}
return z
}
func leakyReLUD(z float64) float64 {
if z < 0 {
return 0.01
}
return 1
}
func arSinH(z float64) float64 {
return math.Log(z + math.Sqrt((z*z)+1))
}
func arSinHD(z float64) float64 {
return 1 / math.Sqrt((z*z)+1)
}