-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
201 lines (165 loc) · 4.82 KB
/
main.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
package main
import (
"encoding/csv"
"flag"
"fmt"
"log"
"math/rand"
"os"
"path/filepath"
"strconv"
)
func main() {
var (
folderPath = flag.String("path", ".", "Path to the datasets")
cycles = flag.Int("cycles", 1, "Nº of training cycles")
learningRate = flag.Float64("lr", 0.1, "Learning rate of the neuron")
outputPath = flag.String("out", ".", "Path to save the output file")
)
flag.Parse()
trainPath := filepath.Join(*folderPath, "train.csv")
validatePath := filepath.Join(*folderPath, "validate.csv")
testPath := filepath.Join(*folderPath, "test.csv")
// Read data from csv file
data, expectedY := readCSV(trainPath)
validateData, valExpectedY := readCSV(validatePath)
testData, testExpectedY := readCSV(testPath)
// Init weights randomly [-1,1]
weights := initWeights(len(data[0]))
var estimate float64
var estimates []float64
var errorsTrain []float64
var errorsValidate []float64
var errorsTest float64
// Learning
for i := 0; i < *cycles; i++ {
for j := range data {
//Calculate estimate
estimate = 0
for x := range data[j] {
estimate += data[j][x] * weights[x]
}
// Update weights (range passes values as a copy)
for x := 0; x < len(weights); x++ {
weights[x] += *learningRate * (expectedY[j] - estimate) * data[j][x]
}
}
// Compute cycle train error
errorsTrain = append(errorsTrain, computeError(data, expectedY, weights))
errorsValidate = append(errorsValidate, computeError(validateData, valExpectedY, weights))
}
errorsTest = computeError(testData, testExpectedY, weights)
for i := range testData {
estimate = 0
for j := range testData[i] {
estimate += testData[i][j] * weights[j]
}
estimates = append(estimates, estimate)
}
fmt.Println("Test error: ")
fmt.Println(errorsTest)
fmt.Println("Weights:")
fmt.Println(weights)
createCSV(*outputPath, errorsTrain, errorsValidate, weights, estimates)
}
// TODO: refactor to improve performance (S.O. QUESTION)
func readCSV(filepath string) ([][]float64, []float64) {
csvfile, err := os.Open(filepath)
if err != nil {
log.Fatalf("could not open %q: %v", filepath, err)
}
reader := csv.NewReader(csvfile)
reader.Comma = ';'
stringMatrix, err := reader.ReadAll()
if err != nil {
log.Fatalf("could not decode CSV file: %v", err)
}
csvfile.Close()
matrix := make([][]float64, len(stringMatrix))
expectedY := make([]float64, len(stringMatrix))
//Parse string matrix into float64
for i := range stringMatrix {
matrix[i] = make([]float64, len(stringMatrix[0]))
for j := range stringMatrix[i] {
if j < 8 {
matrix[i][j], err = strconv.ParseFloat(stringMatrix[i][j], 64)
if err != nil {
log.Fatalf("could not parse float %q: %v", stringMatrix[i][j], err)
}
} else {
//Extract expected output date from file (last column)
expectedY[i], err = strconv.ParseFloat(stringMatrix[i][j], 64)
if err != nil {
log.Fatalf("could not parse float %q: %v", stringMatrix[i][j], err)
}
matrix[i][j] = 1
}
}
}
return matrix, expectedY
}
//This also inits the threshold
func initWeights(length int) []float64 {
weights := make([]float64, length)
//Inits the slice with random numbers between [-1, 1]
for index := range weights {
w := 2*rand.Float64() - 1
weights[index] = w
}
return weights
}
func createCSV(path string, train []float64, validate []float64, weights []float64, estimates []float64) {
var filePath string
if path == "." {
filePath = "errors.csv"
} else {
filePath = path
}
file, err := os.Create(filePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
writer := csv.NewWriter(file)
trainS := []string{"Train"}
validateS := []string{"Validate"}
weightsS := []string{"Weights"}
estimatesS := []string{"Estimates"}
for i := range train {
trainS = append(trainS, strconv.FormatFloat(train[i], 'f', 6, 64))
}
for i := range validate {
validateS = append(validateS, strconv.FormatFloat(validate[i], 'f', 6, 64))
}
for i := range estimates {
estimatesS = append(estimatesS, strconv.FormatFloat(estimates[i], 'f', 6, 64))
}
for i := range weights {
weightsS = append(weightsS, strconv.FormatFloat(weights[i], 'f', 6, 64))
}
for _, v := range [][]string{trainS, validateS, estimatesS, weightsS} {
err = writer.Write(v)
if err != nil {
log.Fatalf("could not write back sample: %v", err)
}
}
writer.Flush()
err = file.Close()
if err != nil {
log.Fatalf("could not write back data to file: %v", err)
}
}
func computeError(data [][]float64, expected []float64, weights []float64) float64 {
var errors float64
var errorSum, estimate float64 = 0, 0
for i := range data {
estimate = 0
for j := range data[i] {
estimate += data[i][j] * weights[j]
}
// Squared error E = (Yd - Ye)^2
errorSum += (expected[i] - estimate) * (expected[i] - estimate)
}
errors = errorSum / float64(len(data))
return errors
}