-
Notifications
You must be signed in to change notification settings - Fork 1
/
mlp.go
229 lines (217 loc) · 6.69 KB
/
mlp.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"fmt"
"math"
"os"
"time"
)
var modelMLP = &Model{
Train: trainMLPSyncronousMiniBatch,
Description: `MLP -MultiLayer Perceptrontrain feed-forward neural network with back propagation`,
}
var (
epsillon = 1.0 / float64(1<<23)
momentum float64
learningRate float64
costThresh float64
maxEpochs int
numnets int
batchsize int
)
func init() {
modelMLP.Flag.Float64Var(&momentum, "momentum", 0.0, "Momentum of network (default 0)")
modelMLP.Flag.Float64Var(&learningRate, "learningRate", 1.0, "Learning rate of the of network")
modelMLP.Flag.Float64Var(&costThresh, "costThresh", -math.MaxFloat64, "Minimum epoch error")
modelMLP.Flag.IntVar(&maxEpochs, "maxEpochs", math.MaxInt64, "Maximum epoch training cycles")
modelMLP.Flag.IntVar(&batchsize, "batchsize", 1, "number of batches within an epoch")
if momentum < 0.0 || momentum > 1.0 {
fmt.Fprintf(os.Stderr, "momentum [%v] is not between 0.0 and 1.0\n", momentum)
os.Exit(2)
}
if learningRate < 0.0 || learningRate > 1.0 {
fmt.Fprintf(os.Stderr, "learning rate [%v] is not between 0.0 and 1.0\n", learningRate)
os.Exit(2)
}
}
// NewMLP constructs the convolutional neuro network based on the topology described in
// Add* and Set* options
func NewMLP(insize, outsize int) *Network {
// Define weight initialization function
n := NewNetwork(SetLearningRate(learningRate),
SetMomentum(momentum),
SetCostor(CrossEntropy),
SetWeightInitFunc(Normal(0.0, 0.01)),
AddInputLayer(insize, true),
AddLayer(50, true, Rectlin),
AddOutputLayer(outsize, Sigmoid))
return n
}
// fMLP runs the feed forward pass based on inputs and produces outputs of the network
func fMLP(n *Network, inputs []float64) (outputs []float64) {
// Set input layer values
for idx, val := range inputs {
n.Layers[0].Neurons[idx].Value = val
}
for idx, layer := range n.Layers {
for _, neuron := range layer.Neurons {
for _, cxn := range neuron.Connections {
cxn.toNeuron.Value += neuron.Value * cxn.Weight
}
}
if layer.Bias != nil {
for _, cxn := range layer.Bias.Connections {
cxn.toNeuron.Value += layer.Bias.OUT * cxn.Weight
}
}
// Apply the activation function to the forward layer
if idx < n.numlayers-1 {
for _, neuron := range n.Layers[idx+1].Neurons {
neuron.Value = n.Layers[idx+1].A.F(neuron.Value)
}
}
}
// Get values from output neurons to return
outputs = make([]float64, n.numout)
for idx, outputneuron := range n.Layers[n.numlayers-1].Neurons {
outputs[idx] = outputneuron.Value
}
return outputs
}
// backPropagate adjusts the connection weights of the network based on the error, Delta,
// of each neuron computed during gradient decent
func bMLP(n *Network, targets []float64) {
var (
activatePrime float64
costPrime float64
delta float64
)
outlayer := n.Layers[n.numlayers-1]
// Calculate delta of output layer
if outlayer.shortcut {
for idx, neuron := range outlayer.Neurons {
costPrime = n.C.FPrime(neuron.Value, targets[idx])
neuron.Delta = costPrime
}
} else {
for idx, neuron := range outlayer.Neurons {
costPrime = n.C.FPrime(neuron.Value, targets[idx])
activatePrime = outlayer.A.FPrime(neuron.Value)
neuron.Delta = costPrime * activatePrime
}
}
// Calculate delta of hidden layers
for idx := n.numlayers - 2; idx >= 1; idx-- {
layer := n.Layers[idx]
for _, neuron := range layer.Neurons {
for _, cxn := range neuron.Connections {
delta = cxn.Weight * cxn.toNeuron.Delta
neuron.Delta += delta
}
delta = layer.A.FPrime(neuron.Value)
neuron.Delta *= delta
}
if layer.Bias != nil {
for _, cxn := range layer.Bias.Connections {
delta = cxn.Weight * cxn.toNeuron.Delta
layer.Bias.Delta += delta
}
delta = layer.A.FPrime(layer.Bias.OUT)
layer.Bias.Delta *= delta
}
}
}
func accumulateWeightChange(n *Network) {
var weightChange float64
for _, layer := range n.Layers[0 : n.numlayers-1] {
for _, neuron := range layer.Neurons {
for _, cxn := range neuron.Connections {
weightChange = n.eta * neuron.Value * cxn.toNeuron.Delta
cxn.weightChange += weightChange
}
}
if layer.Bias != nil {
for _, cxn := range layer.Bias.Connections {
weightChange = n.eta * layer.Bias.OUT * cxn.toNeuron.Delta
cxn.weightChange += weightChange
}
}
}
}
func updateWeights(n *Network) {
var (
weightChange float64
momentumTerm float64
bze = float64(batchsize)
)
// Apply deltas to connection weights
for _, layer := range n.Layers {
for _, neuron := range layer.Neurons {
for _, cxn := range neuron.Connections {
weightChange = cxn.weightChange / bze
momentumTerm = n.momentum * cxn.prevChange
weightChange += momentumTerm
cxn.Weight -= weightChange
cxn.prevChange = weightChange
cxn.weightChange = 0.0
}
}
if layer.Bias != nil {
for _, cxn := range layer.Bias.Connections {
weightChange = cxn.weightChange / bze
momentumTerm = n.momentum * cxn.prevChange
weightChange += momentumTerm
cxn.Weight -= weightChange
cxn.prevChange = weightChange
cxn.weightChange = 0.0
}
}
}
}
type MLPExec struct {
network *Network
}
// execMLP prediction on input given a trained network
func (mlp *MLPExec) Execute(input []float64) []float64 {
output := fMLP(mlp.network, input)
mlp.network.zeroValues()
return output
}
func trainMLPSyncronousMiniBatch(d *TrainData) (e Executor) {
mlp := NewMLP(d.FeatureLen, d.OutputLen)
ts := NewTrainStatus()
sgd := NewSGD(d.SampleLen, pRNG)
numbatches := d.SampleLen / batchsize
fmt.Fprintf(os.Stderr, "Beginning training with:\n%v", mlp)
fmt.Fprintf(os.Stderr, "MaxEpochs = %v and CostThreshold = %v\n", maxEpochs, costThresh)
fmt.Fprintf(os.Stderr, "BatchCount = %v\n\n", numbatches)
for ts.epoch < maxEpochs && ts.epochCost > costThresh && !isStopEarly() {
ts.epochTime = time.Now()
batches := sgd.ChooseMany(numbatches)
for _, batch := range batches {
for _, sampleIDX := range batch {
output := fMLP(mlp, d.Input[sampleIDX])
expected := d.Output[sampleIDX]
for idx, val := range output {
ts.sampCost += mlp.C.F(val, expected[idx]) / mlp.costDivisor
}
ts.epochCost += (ts.sampCost / d.SampleLength)
ts.sampCost = 0.0
bMLP(mlp, expected)
accumulateWeightChange(mlp)
mlp.zeroValuesAndDeltas()
}
updateWeights(mlp)
}
fmt.Fprintf(os.Stderr, "Epoch took %v\n", time.Now().Sub(ts.epochTime))
fmt.Fprintf(os.Stderr, "Epoch: %v, Cost: %.4f\n", ts.epoch, ts.epochCost)
fmt.Fprintf(os.Stderr, "%v\n", mlp)
ts.epoch++
ts.finalCost = ts.epochCost
ts.epochCost, ts.sampCost = 0.0, 0.0
}
fmt.Fprintf(os.Stderr, "Finished training of %v\n", mlp)
fmt.Fprintf(os.Stderr, "Epoch: %v, Cost: %.4f\n", ts.epoch, ts.finalCost)
return &MLPExec{
network: mlp,
}
}