-
Notifications
You must be signed in to change notification settings - Fork 1
/
activators.go
169 lines (141 loc) · 3.88 KB
/
activators.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package neural
import "math"
// Activator calculates neuron activation and it's derivative from given potential
type Activator interface {
Activation(dst, potentials []float64)
Derivative(dst, potentials []float64)
}
// NewLinearActivator creates Activator that applies linear function to given potential
//
// Activation: a*potential + 0
//
// Derivative: a
func NewLinearActivator(a float64) Activator {
return &linearActivation{a}
}
type linearActivation struct {
a float64
}
func (l *linearActivation) Activation(dst, potentials []float64) {
for i, potential := range potentials {
dst[i] = l.a * potential
}
}
func (l *linearActivation) Derivative(dst, potentials []float64) {
for i := range potentials {
dst[i] = l.a
}
}
// NewSigmoidActivator creates Activator that applies linear function to given potential
//
// Activation: 1/(1+exp(-potential))
//
// Derivative: f(potential) * (1- f(potential))
func NewSigmoidActivator() Activator {
return &sigmoidActivator{}
}
type sigmoidActivator struct{}
func (s *sigmoidActivator) Activation(dst, potentials []float64) {
for i, potential := range potentials {
dst[i] = 1.0 / (1.0 + math.Exp(-potential))
}
}
func (s *sigmoidActivator) Derivative(dst, potentials []float64) {
s.Activation(dst, potentials)
for i, activation := range dst {
dst[i] = activation * (1 - activation)
}
}
// NewStepActivator creates Activator that returns 0 or 1 only.
//
// Activation: 1 if potential >= 0 else 0
//
// Derivative: 1 (is that correct?)
func NewStepActivator() Activator {
return &stepActicator{}
}
type stepActicator struct{}
func (s *stepActicator) Activation(dst, potentials []float64) {
for i, potential := range potentials {
if potential >= 0 {
dst[i] = 1
} else {
dst[i] = 0
}
}
}
func (s *stepActicator) Derivative(dst, potentials []float64) {
for i := range potentials {
dst[i] = 1
}
}
// NewSoftmaxActivator creates Activator that scales responses in layer from 0 to 1.
//
// Sum of responses in layer are equal 1, so it can be interpret as probability.
// This activator should be used in last layer with Log Likelihood.
// Derivative is not implemented as it should not be needed. If used it will panic.
func NewSoftmaxActivator() Activator {
return &softmaxActicator{}
}
type softmaxActicator struct{}
func (s *softmaxActicator) Activation(dst, potentials []float64) {
var sum float64
for i, potential := range potentials {
dst[i] = math.Exp(potential)
sum += dst[i]
}
for i, dstVal := range dst {
dst[i] = dstVal / sum
}
}
func (s *softmaxActicator) Derivative(dst, potentials []float64) {
panic("Derivative of Softmax should not be used in ANN")
}
// NewTanhActivator creates Activator that returns values between -1 and 1.
// Very similar to sigmoid function in nature.
//
// Activation: tanh(potential)
//
// Derivative: 1/f(potential/2)/2
func NewTanhActivator() Activator {
return &tanhActicator{}
}
type tanhActicator struct{}
func (s *tanhActicator) Activation(dst, potentials []float64) {
for i, potential := range potentials {
dst[i] = math.Tanh(potential)
}
}
func (s *tanhActicator) Derivative(dst, potentials []float64) {
for i, potential := range potentials {
dst[i] = (1 + math.Tanh(potential/2)) / 2
}
}
// NewRectActivator creates Activator that returns 0 for non positive potential, otherwise it returns potential
// It's a rectified linear function
//
// Activation: 0 for potential < 0, potential otherwise
//
// Derivative: 0 for potential < 0, 1 otherwise
func NewRectActivator() Activator {
return &rectActicator{}
}
type rectActicator struct{}
func (s *rectActicator) Activation(dst, potentials []float64) {
for i, potential := range potentials {
if potential > 0 {
dst[i] = potential
} else {
dst[i] = 0
}
}
}
func (s *rectActicator) Derivative(dst, potentials []float64) {
for i, potential := range potentials {
if potential > 0 {
dst[i] = 1
} else {
dst[i] = 0
}
}
}