-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperceptron.go
166 lines (143 loc) · 4.1 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
package main
import (
"bytes"
"fmt"
"math/rand"
"os"
"strconv"
"github.com/gosuri/uiprogress"
chart "github.com/wcharczuk/go-chart"
"gonum.org/v1/gonum/mat"
)
func runPerceptron() {
if len(os.Args) < 3 {
fmt.Println("Please enter a histogram file as an argument after 'run'.")
os.Exit(1)
}
histograms := shuffleHistograms(loadHistograms(os.Args[2]))
trainingSplit := int64(float64(len(histograms)) * 0.8)
trainingData := histograms[:trainingSplit]
validationData := histograms[trainingSplit:]
fmt.Printf("Out of %d histograms: %d are for training, %d are for validation.\n", len(histograms), len(trainingData), len(validationData))
histograms = nil
featureLength := len(trainingData[0].Data)
weights := make([]float64, featureLength)
for i := 0; i < len(weights); i++ {
weights[i] = rand.Float64()
}
learningRate, _ := strconv.ParseFloat(os.Args[4], 64)
epochs, _ := strconv.Atoi(os.Args[3])
errors := make([]float64, epochs)
accuracy := make([]float64, epochs)
xAxis := make([]float64, epochs)
for i := 1; i < epochs; i++ {
xAxis[i] = float64(i + 1)
}
uiprogress.Start()
bar := uiprogress.AddBar(epochs).AppendCompleted().PrependElapsed()
bar.PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("Epoch (%d/%d)", b.Current(), epochs)
})
for epoch := 0; epoch < epochs; epoch++ {
errorSet := 0.0
for _, histogram := range trainingData {
u := mat.NewVecDense(featureLength, histogram.Data)
v := mat.NewVecDense(featureLength, weights)
output := activation(mat.Dot(u, v))
error := histogram.Classification - output
errorSet += float64(error)
for index := range weights {
weights[index] += learningRate * float64(error) * float64(histogram.Data[index])
}
}
errorSet /= float64(len(trainingData))
errors[epoch] = errorSet
validationTotal := len(validationData)
validationSuccesses := 0
for _, histogram := range validationData {
u := mat.NewVecDense(featureLength, histogram.Data)
v := mat.NewVecDense(featureLength, weights)
output := activation(mat.Dot(u, v))
if histogram.Classification == output {
validationSuccesses++
}
}
accuracy[epoch] = 100.0 * (float64(validationSuccesses) / float64(validationTotal))
bar.Incr()
}
uiprogress.Stop()
validationTotal := len(validationData)
validationSuccesses := 0
for _, histogram := range validationData {
u := mat.NewVecDense(featureLength, histogram.Data)
v := mat.NewVecDense(featureLength, weights)
output := activation(mat.Dot(u, v))
if histogram.Classification == output {
validationSuccesses++
}
}
fmt.Printf("Validation successes: %d/%d (%0.2f%% accuracy)\n", validationSuccesses, validationTotal, 100.0*(float64(validationSuccesses)/float64(validationTotal)))
graph := chart.Chart{
XAxis: chart.XAxis{
Name: "Epochs",
NameStyle: chart.StyleShow(),
Style: chart.Style{
Show: true,
},
TickPosition: chart.TickPositionBetweenTicks,
},
YAxis: chart.YAxis{
Name: "Accuracy (Percent)",
NameStyle: chart.StyleShow(),
Style: chart.Style{
Show: true,
},
},
YAxisSecondary: chart.YAxis{
Name: "Error",
NameStyle: chart.StyleShow(),
Style: chart.Style{
Show: true,
},
},
Series: []chart.Series{
chart.ContinuousSeries{
Name: "Accuracy",
XValues: xAxis,
YValues: accuracy,
},
chart.ContinuousSeries{
Name: "Error",
XValues: xAxis,
YValues: errors,
YAxis: chart.YAxisSecondary,
},
},
}
graph.Elements = []chart.Renderable{
chart.LegendThin(&graph),
}
f, err := os.Create("rate_" + os.Args[4] + "_over_" + os.Args[3] + "_epochs.png")
defer f.Close()
if err == nil {
buffer := bytes.NewBuffer([]byte{})
graph.Render(chart.PNG, buffer)
f.Write(buffer.Bytes())
}
results, err := os.Create("rate_" + os.Args[4] + "_over_" + os.Args[3] + "_epochs.dat")
defer results.Close()
if err != nil {
panic("could not write output file.")
}
resultString := ""
for _, weight := range weights {
resultString += fmt.Sprintf("%0.10f", weight) + " "
}
results.WriteString(resultString)
}
func activation(num float64) int {
if num < 0.5 {
return 0
}
return 1
}