This repository was archived by the owner on Jun 7, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathactivations.go
More file actions
117 lines (97 loc) · 2.19 KB
/
Copy pathactivations.go
File metadata and controls
117 lines (97 loc) · 2.19 KB
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
package gorann
import "math"
//
// types
//
type Activation struct {
name string
f func(float64) float64
dfy func(float64) float64 // used when the derivative can be expressed as a function of the value of the function
dfx func(float64) float64
}
//
// static
//
var activations map[string]*Activation
func init() {
RegisterActivation(&Activation{"sigmoid", sigmoid, dsigmoid, nil})
RegisterActivation(&Activation{"tanh", tanh, dtanh, nil})
RegisterActivation(&Activation{"identity", identity, didentity, nil})
RegisterActivation(&Activation{"relu", relu, nil, drelu})
RegisterActivation(&Activation{"leakyrelu", leakyrelu, nil, dleakyrelu})
RegisterActivation(&Activation{"softplus", softplus, nil, dsoftplus})
}
func RegisterActivation(a *Activation) {
if activations == nil {
activations = make(map[string]*Activation, 8)
}
activations[a.name] = a
}
func sigmoid(x float64) float64 {
return 1 / (1 + math.Exp(-x))
}
func dsigmoid(y float64) float64 {
return y * (1 - y)
}
func tanh(x float64) float64 {
y1 := math.Exp(x)
y2 := math.Exp(-x)
return (y1 - y2) / (y1 + y2)
}
func dtanh(y float64) float64 {
return 1 - y*y
}
func identity(x float64) float64 {
return x
}
func didentity(y float64) float64 {
return 1
}
func relu(x float64) float64 {
if x < 0 {
return 0
}
return x
}
func drelu(x float64) float64 {
if x > 0 {
return 1
}
return 0
}
// aka parametric Rectifier
const LeakyApha = 1E-2
func leakyrelu(x float64) float64 {
if x < 0 {
return LeakyApha * x
}
return x
}
func dleakyrelu(x float64) float64 {
if x > 0 {
return 1
}
return LeakyApha
}
func softplus(x float64) float64 {
return math.Log(1 + math.Exp(x))
}
func dsoftplus(x float64) float64 {
return 1 / (1 + math.Exp(-x))
}
//-----------------------------------------------------------------------
//
// softmax - special case, not registered
// NOTE:
// softmax on the output layer MUST only be used with cross-entropy cost
//
//-----------------------------------------------------------------------
func softmax(sumexp float64, x float64) float64 {
return math.Exp(x) / sumexp
}
func dsoftmax(y []float64, i int, j int) float64 {
if i == j {
return y[i] * (1 - y[i])
}
return -y[i] * y[j]
}