-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstochastic_hill_climber.go
215 lines (163 loc) · 5.49 KB
/
stochastic_hill_climber.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package neurvolve
import (
"github.com/couchbaselabs/logg"
ng "github.com/tleyden/neurgo"
"math"
"math/rand"
)
type StochasticHillClimber struct {
FitnessThreshold float64
MaxIterationsBeforeRestart int
MaxAttempts int
WeightSaturationRange []float64
}
func (shc *StochasticHillClimber) Train(cortex *ng.Cortex, scape Scape) (fittestNeuralNet *ng.Cortex, succeeded bool) {
shc.validate()
numAttempts := 0
fittestNeuralNet = cortex
// Apply NN to problem and save fitness
fitness := scape.Fitness(fittestNeuralNet)
logg.LogTo("MAIN", "Initial fitness: %v", fitness)
if fitness > shc.FitnessThreshold {
succeeded = true
return
}
for i := 0; ; i++ {
// Save the genotype
candidateNeuralNet := fittestNeuralNet.Copy()
// Perturb synaptic weights and biases
PerturbParameters(candidateNeuralNet, shc.WeightSaturationRange)
// Re-Apply NN to problem
candidateFitness := scape.Fitness(candidateNeuralNet)
logg.LogTo("DEBUG", "candidate fitness: %v", fitness)
// If fitness of perturbed NN is higher, discard original NN and keep new
// If fitness of original is higher, discard perturbed and keep old.
if candidateFitness > fitness {
logg.LogTo("MAIN", "i: %v candidateFitness: %v > fitness: %v", i, candidateFitness, fitness)
i = 0
fittestNeuralNet = candidateNeuralNet
fitness = candidateFitness
}
if candidateFitness > shc.FitnessThreshold {
logg.LogTo("MAIN", "candidateFitness: %v > Threshold. Success at i=%v", candidateFitness, i)
succeeded = true
break
}
if ng.IntModuloProper(i, shc.MaxIterationsBeforeRestart) {
logg.LogTo("MAIN", "** restart hill climber. fitness: %f i/max: %d/%d", fitness, numAttempts, shc.MaxAttempts)
numAttempts += 1
i = 0
shc.resetParametersToRandom(fittestNeuralNet)
ng.SeedRandom()
}
if numAttempts >= shc.MaxAttempts {
succeeded = false
break
}
}
return
}
func (shc *StochasticHillClimber) TrainExamples(cortex *ng.Cortex, examples []*ng.TrainingSample) (fittestNeuralNet *ng.Cortex, succeeded bool) {
trainingSampleScape := &TrainingSampleScape{
examples: examples,
}
return shc.Train(cortex, trainingSampleScape)
}
// 1. Each neuron in the neural net (weight or bias) will be chosen for perturbation
// with a probability of 1/sqrt(nn_size)
// 2. Within the chosen neuron, the weights which will be perturbed will be chosen
// with probability of 1/sqrt(parameters_size)
// 3. The intensity of the parameter perturbation will chosen with uniform distribution
// of -pi and pi
func PerturbParameters(cortex *ng.Cortex, saturationBounds []float64) {
// pick the neurons to perturb (at least one)
neurons := chooseNeuronsToPerturb(cortex)
for _, neuron := range neurons {
logg.LogTo("DEBUG", "Going to perturb neuron: %v", neuron.NodeId.UUID)
perturbNeuron(neuron, saturationBounds)
}
}
func (shc *StochasticHillClimber) resetParametersToRandom(cortex *ng.Cortex) {
neurons := cortex.Neurons
for _, neuronNode := range neurons {
for _, cxn := range neuronNode.Inbound {
cxn.Weights = ng.RandomWeights(len(cxn.Weights))
}
neuronNode.Bias = ng.RandomBias()
}
}
func chooseNeuronsToPerturb(cortex *ng.Cortex) []*ng.Neuron {
neuronsToPerturb := make([]*ng.Neuron, 0)
// choose some random neurons to perturb. we need at least one, so
// keep looping until we've chosen at least one
didChooseNeuron := false
for {
probability := nodePerturbProbability(cortex)
neurons := cortex.Neurons
for _, neuronNode := range neurons {
if rand.Float64() < probability {
neuronsToPerturb = append(neuronsToPerturb, neuronNode)
didChooseNeuron = true
}
}
if didChooseNeuron {
break
}
}
return neuronsToPerturb
}
func nodePerturbProbability(cortex *ng.Cortex) float64 {
neurons := cortex.Neurons
numNeurons := len(neurons)
return 1 / math.Sqrt(float64(numNeurons))
}
func perturbNeuron(neuron *ng.Neuron, saturationBounds []float64) {
probability := parameterPerturbProbability(neuron)
// keep trying until we've perturbed at least one parameter
for {
didPerturbWeight := false
for _, cxn := range neuron.Inbound {
didPerturbWeight = possiblyPerturbConnection(cxn, probability, saturationBounds)
}
didPerturbBias := possiblyPerturbBias(neuron, probability, saturationBounds)
// did we perturb anything? if so, we're done
if didPerturbWeight || didPerturbBias {
break
}
}
}
func parameterPerturbProbability(neuron *ng.Neuron) float64 {
numWeights := 0
for _, connection := range neuron.Inbound {
numWeights += len(connection.Weights)
}
return 1 / math.Sqrt(float64(numWeights))
}
func possiblyPerturbConnection(cxn *ng.InboundConnection, probability float64, saturationBounds []float64) bool {
didPerturb := false
for j, weight := range cxn.Weights {
if rand.Float64() < probability {
perturbedWeight := perturbParameter(weight, saturationBounds)
logg.LogTo("DEBUG", "weight %v -> %v", weight, perturbedWeight)
cxn.Weights[j] = perturbedWeight
didPerturb = true
}
}
return didPerturb
}
func possiblyPerturbBias(neuron *ng.Neuron, probability float64, saturationBounds []float64) bool {
didPerturb := false
if rand.Float64() < probability {
bias := neuron.Bias
perturbedBias := perturbParameter(bias, saturationBounds)
neuron.Bias = perturbedBias
logg.LogTo("DEBUG", "bias %v -> %v", bias, perturbedBias)
didPerturb = true
}
return didPerturb
}
func (shc *StochasticHillClimber) validate() {
if len(shc.WeightSaturationRange) == 0 {
logg.LogPanic("Invalid (empty) WeightSaturationRange")
}
}