-
Notifications
You must be signed in to change notification settings - Fork 9
/
perceptron.go
180 lines (138 loc) · 5.26 KB
/
perceptron.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
// Neural provides struct to represents most common neural networks model and algorithms to train / test them.
package neural
import (
// sys import
"os"
"math/rand"
// third part import
log "github.com/sirupsen/logrus"
// this repo internal import
mu "github.com/made2591/go-perceptron-go/util"
)
// Perceptron struct represents a simple Perceptron network with a slice of n weights.
type Perceptron struct {
// Weights represents Perceptron vector representation
Weights []float64
// Bias represents Perceptron natural propensity to spread signal
Bias float64
// Lrate represents learning rate of perceptron
Lrate float64
}
// #######################################################################################
func init() {
// Output to stdout instead of the default stderr
log.SetOutput(os.Stdout)
// Only log the warning severity or above.
log.SetLevel(log.InfoLevel)
}
// RandomPerceptronInit initialize perceptron weight, bias and learning rate using NormFloat64 random value.
func RandomPerceptronInit(perceptron *Perceptron) {
// init random weights
for index, _ := range perceptron.Weights {
// init random threshold weight
perceptron.Weights[index] = rand.NormFloat64()
}
// init random bias and lrate
perceptron.Bias = rand.NormFloat64()
perceptron.Lrate = rand.NormFloat64() * 0.01
log.WithFields(log.Fields{
"level" : "debug",
"place" : "perceptron",
"func" : "RandomPerceptronInit",
"msg" : "random perceptron weights init",
"weights" : perceptron.Weights,
}).Debug()
}
// UpdateWeights performs update in perceptron weights with respect to passed stimulus.
// It returns error of prediction before and after updating weights.
func UpdateWeights(perceptron *Perceptron, stimulus *Stimulus) (float64, float64) {
// compute prediction value and error for stimulus given perceptron BEFORE update (actual state)
var predictedValue, prevError, postError float64 = Predict(perceptron, stimulus), 0.0, 0.0
prevError = stimulus.Expected - predictedValue
// performs weights update for perceptron
perceptron.Bias = perceptron.Bias + perceptron.Lrate * prevError
// performs weights update for perceptron
for index, _ := range perceptron.Weights {
perceptron.Weights[index] = perceptron.Weights[index] + perceptron.Lrate * prevError * stimulus.Dimensions[index]
}
// compute prediction value and error for stimulus given perceptron AFTER update (actual state)
predictedValue = Predict(perceptron, stimulus)
postError = stimulus.Expected - predictedValue
log.WithFields(log.Fields{
"level" : "debug",
"place" : "perceptron",
"func" : "UpdateWeights",
"msg" : "updating weights of perceptron",
"weights" : perceptron.Weights,
}).Debug()
// return errors
return prevError, postError
}
// TrainPerceptron trains a passed perceptron with stimuli passed, for specified number of epoch.
// If init is 0, leaves weights unchanged before training.
// If init is 1, reset weights and bias of perceptron before training.
func TrainPerceptron(perceptron *Perceptron, stimuli *Stimuli, epochs int, init int) {
// init weights if specified
if init == 1 {
perceptron.Weights = make([]float64, len(stimuli.Training[0].Dimensions))
perceptron.Bias = 0.0
}
// init counter
var epoch int = 0
// accumulator errors prev and post weights updates
var squaredPrevError, squaredPostError float64 = 0.0, 0.0
// in each epoch
for epoch < epochs {
// update weight using each stimulus in training set
for _, stimulus := range stimuli.Training {
prevError, postError := UpdateWeights(perceptron, &stimulus)
// NOTE: in each step, use weights already updated by previous
squaredPrevError = squaredPrevError + (prevError * prevError)
squaredPostError = squaredPostError + (postError * postError)
}
log.WithFields(log.Fields{
"level" : "debug",
"place" : "error evolution in epoch",
"method" : "TrainPerceptron",
"msg" : "epoch and squared errors reached before and after updating weights",
"epochReached" : epoch+1,
"squaredErrorPrev" : squaredPrevError,
"squaredErrorPost" : squaredPostError,
}).Debug()
// increment epoch counter
epoch++
}
}
// Predict performs a perceptron prediction to passed stimulus.
// It returns a float64 binary predicted value.
func Predict(perceptron *Perceptron, stimulus *Stimulus) float64 {
if mu.ScalarProduct(perceptron.Weights, stimulus.Dimensions) + perceptron.Bias < 0.0 {
return 0.0
}
return 1.0
}
// Accuracy calculate percentage of equal values between two float64 based slices.
// It returns int number and a float64 percentage value of corrected values.
func Accuracy(actual []float64, predicted []float64) (int, float64) {
// if slices have different number of elements
if len(actual) != len(predicted) {
log.WithFields(log.Fields{
"level" : "error",
"place" : "perceptron",
"method" : "Accuracy",
"msg" : "accuracy between actual and predicted slices of values",
"actualLen" : len(actual),
"predictedLen" : len(predicted),
}).Error("Failed to compute accuracy between actual values and predictions: different length.")
return -1, -1.0
}
// init result
var correct int = 0
for index, value := range actual {
if value == predicted[index] {
correct++
}
}
// return correct
return correct, float64(correct) / float64(len(actual)) * 100.0
}