-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
544 lines (452 loc) · 15.1 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package main
import (
"encoding/csv"
"errors"
"fmt"
"log"
"math"
"math/rand"
"os"
"strconv"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
"gonum.org/v1/gonum/floats"
"gonum.org/v1/gonum/mat"
)
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("NeurOG")
// Criar rótulos
labelTrain := widget.NewLabel("Nome do arquivo de treinamento")
labelTest := widget.NewLabel("Nome do arquivo de teste")
labelLR := widget.NewLabel("Learning Rate")
labelEpochs := widget.NewLabel("Número de Épocas")
labelHiddenNeurons := widget.NewLabel("Neurônios na Camada Oculta")
// Criar entradas
entryTrain := widget.NewEntry()
entryTrain.SetText("teste.csv")
entryTest := widget.NewEntry()
entryTest.SetText("treinamento.csv")
entryLR := widget.NewEntry()
entryLR.SetText("0.3")
entryEpochs := widget.NewEntry()
entryEpochs.SetText("5000")
entryHiddenNeurons := widget.NewEntry()
entryHiddenNeurons.SetText("25")
// Criar layout para organizar os elementos
form := container.NewVBox(
container.New(layout.NewVBoxLayout(),
labelTrain,
entryTrain,
labelTest,
entryTest,
labelLR,
entryLR,
labelEpochs,
entryEpochs,
labelHiddenNeurons,
entryHiddenNeurons,
widget.NewButton("Treinar e Testar", func() {
// Obtenha os valores dos campos de entrada
trainFileName := entryTrain.Text
testFileName := entryTest.Text
lr, err := strconv.ParseFloat(entryLR.Text, 64)
if err != nil {
log.Fatal(err)
return
}
epochs, err := strconv.Atoi(entryEpochs.Text)
if err != nil {
log.Fatal(err)
return
}
hiddenNeurons, err := strconv.Atoi(entryHiddenNeurons.Text)
if err != nil {
log.Fatal(err)
return
}
// Chame a função trainAndTest
trainAndTest(trainFileName, testFileName, hiddenNeurons, epochs, lr)
}),
),
)
myWindow.SetContent(container.NewVBox(
widget.NewLabel("NeurOG"), // Título
form,
))
myWindow.Resize(fyne.NewSize(600, 300))
myWindow.SetMaster()
myWindow.ShowAndRun()
}
func trainAndTest(trainFileName, testFileName string, hiddenNeurons, numEpochs int, learningRate float64) {
// lê os arquivos de treinamento
inputs, labels := makeInputsAndLabels(trainFileName)
labels = formatLabel(labels)
config := neuralNetConfig{
inputNeurons: 6,
outputNeurons: 5,
hiddenNeurons: hiddenNeurons,
numEpochs: numEpochs,
learningRate: learningRate,
}
// Cria e treina a rede neural
network := createNeuralNetwork(config)
if err := network.train(inputs, labels); err != nil {
log.Fatal(err)
}
// Formaliza a matriz de teste
testInputs, testLabels := makeInputsAndLabels(testFileName)
testLabels = formatLabel(testLabels)
// Realiza as predições usando o modelo treinado (network)
predictions, err := network.predict(testInputs)
if err != nil {
log.Fatal(err)
}
binarizedPredictions := binarizePredictions(predictions)
// fmt.Println("binarized Predictions\n", mat.Formatted(binarizedPredictions, mat.Squeeze()))
comparePredictions(binarizedPredictions, testLabels)
}
// Função para transformar as predições em uma matriz binária
func binarizePredictions(predictions *mat.Dense) *mat.Dense {
rows, cols := predictions.Dims()
binarized := mat.NewDense(rows, cols, nil)
for i := 0; i < rows; i++ {
// Encontra o índice do maior elemento na linha
_, maxIdx := findMaxIndex(predictions.RowView(i))
for j := 0; j < cols; j++ {
// Define 1 no índice do maior elemento, 0 nos demais
if j == maxIdx {
binarized.Set(i, j, 1)
} else {
binarized.Set(i, j, 0)
}
}
}
return binarized
}
// Função para encontrar o índice do valor máximo em um vetor
func findMaxIndex(v mat.Vector) (float64, int) {
maxIdx := 0
maxVal := v.AtVec(0)
for i := 1; i < v.Len(); i++ {
val := v.AtVec(i)
if val > maxVal {
maxVal = val
maxIdx = i
}
}
return maxVal, maxIdx
}
// Define a arquitetura e os parâmetros de aprendizado da Rede Neural
type neuralNetConfig struct {
inputNeurons int // Quantidade de neurônios de entrada
outputNeurons int // Quantidade de neurônios de saída
hiddenNeurons int // Quantidade de neurônios escondidos
numEpochs int // Quantidade de "Epochs"
learningRate float64 // Taxa de aprendizado
}
// Contém as informações sobre o treinamento da rede neural
type neuralNet struct {
config neuralNetConfig // Configurações
weightHidden *mat.Dense // Matriz de pesos de entrada
biasHidden *mat.Dense // Matriz de bias de entrada
weightOut *mat.Dense // Matriz de pesos de saída
biasOut *mat.Dense // Matriz de bias de saída
}
// Inicia uma nova rede neural
func createNeuralNetwork(config neuralNetConfig) *neuralNet {
return &neuralNet{config: config}
}
// Implementação da sigmoid
func sigmoid(x float64) float64 {
return 1.0 / (1.0 + math.Exp(-x))
}
// implementação da sigmoid derivativa para o backpropagation
func sigmoidPrime(x float64) float64 {
return sigmoid(x) * (1.0 - sigmoid(x))
}
// Formata o label para 1,0,0,0,0 / 0,1,0,0,0 / ...
func formatLabel(labels *mat.Dense) *mat.Dense {
rows, cols := labels.Dims()
labelsOutput := mat.NewDense(rows, cols*5, nil)
for i := 0; i < rows; i++ {
val := int(labels.At(i, 0)) - 1 // Ajuste para começar de 0
for j := 0; j < cols*5; j++ {
if j%5 == val {
labelsOutput.Set(i, j, 1)
} else {
labelsOutput.Set(i, j, 0)
}
}
}
return labelsOutput
}
// Treina a rede neural
func (nn *neuralNet) train(inputs, labels *mat.Dense) error {
rand.Seed(time.Now().UnixNano())
// Inicializa os pesos e bias
weightInputHidden := mat.NewDense(nn.config.inputNeurons, nn.config.hiddenNeurons, nil)
biasInputHidden := mat.NewDense(1, nn.config.hiddenNeurons, nil)
weightHiddenOut := mat.NewDense(nn.config.hiddenNeurons, nn.config.outputNeurons, nil)
biasHiddenOut := mat.NewDense(1, nn.config.outputNeurons, nil)
weightInputHiddenRaw := weightInputHidden.RawMatrix().Data
biasInputHiddenRaw := biasInputHidden.RawMatrix().Data
weightHiddenOutRaw := weightHiddenOut.RawMatrix().Data
biasHiddenOutRaw := biasHiddenOut.RawMatrix().Data
// Atribui valores aleatórios aos pesos e bias
for _, param := range [][]float64{
weightInputHiddenRaw,
biasInputHiddenRaw,
weightHiddenOutRaw,
biasHiddenOutRaw,
} {
for i := range param {
param[i] = rand.Float64()*2 - 1
}
}
// fmt.Println("weightInputHidden\n", mat.Formatted(weightInputHidden, mat.Squeeze()))
// fmt.Println("biasInputHidden\n", mat.Formatted(biasInputHidden, mat.Squeeze()))
// fmt.Println("weightHiddenOut\n", mat.Formatted(weightHiddenOut, mat.Squeeze()))
// fmt.Println("biasHiddenOut\n", mat.Formatted(biasHiddenOut, mat.Squeeze()))
// Saída da rede neural
output := new(mat.Dense)
// Usa o backpropagation para ajustar os pesos e bias
if err := nn.backPropagate(
inputs,
labels,
weightInputHidden,
biasInputHidden,
weightHiddenOut,
biasHiddenOut,
output); err != nil {
return err
}
// FIM DO TREINAMENTO: Implementa os elementos dentro da rede neural
nn.weightHidden = weightInputHidden
nn.biasHidden = biasInputHidden
nn.weightOut = weightHiddenOut
nn.biasOut = biasHiddenOut
return nil
}
func (nn *neuralNet) backPropagate(inputs,
labels,
weightInputHidden,
biasInputHidden,
weightHiddenOut,
biasHiddenOut,
output *mat.Dense) error {
// Loop através do número de epochs utilizando backpropagation
for i := 0; i < nn.config.numEpochs; i++ {
// FEEDFORWARD
// Input -> hidden
hiddenLayerInput := new(mat.Dense)
// fmt.Println("inputs\n", mat.Formatted(inputs, mat.Squeeze()))
// fmt.Println("weightInputHidden\n", mat.Formatted(weightInputHidden, mat.Squeeze()))
hiddenLayerInput.Mul(inputs, weightInputHidden)
// fmt.Println("hiddenLayerInput\n", mat.Formatted(hiddenLayerInput, mat.Squeeze()))
addBiasInputHidden := func(_, col int, v float64) float64 {
return v + biasInputHidden.At(0, col)
}
hiddenLayerInput.Apply(addBiasInputHidden, hiddenLayerInput)
// Aplicação da sigmoid
InputHiddenLayerActivation := new(mat.Dense)
applySigmoid := func(_, _ int, v float64) float64 {
return sigmoid(v)
}
InputHiddenLayerActivation.Apply(applySigmoid, hiddenLayerInput)
// fmt.Println("InputHiddenLayerActivation\n", mat.Formatted(InputHiddenLayerActivation, mat.Squeeze()))
// hidden -> output
outputLayerInput := new(mat.Dense)
outputLayerInput.Mul(InputHiddenLayerActivation, weightHiddenOut)
addBiasHiddenOut := func(_, col int, v float64) float64 {
return v + biasHiddenOut.At(0, col)
}
outputLayerInput.Apply(addBiasHiddenOut, outputLayerInput)
output.Apply(applySigmoid, outputLayerInput)
// Backpropagation
// fmt.Println(labels.Dims())
// fmt.Println("labels:\n", mat.Formatted(labels, mat.Squeeze()))
// fmt.Println(output.Dims())
// fmt.Println("output:\n", mat.Formatted(output, mat.Squeeze()))
networkError := new(mat.Dense)
networkError.Sub(labels, output)
// fmt.Println("Error:\n", mat.Formatted(networkError, mat.Squeeze()))
slopeOutputLayer := new(mat.Dense)
applySigmoidPrime := func(_, _ int, v float64) float64 {
return sigmoidPrime(v)
}
slopeOutputLayer.Apply(applySigmoidPrime, output)
slopeHiddenLayer := new(mat.Dense)
slopeHiddenLayer.Apply(applySigmoidPrime, InputHiddenLayerActivation)
//
dOutput := new(mat.Dense)
dOutput.MulElem(networkError, slopeOutputLayer)
errorAtHiddenLayer := new(mat.Dense)
errorAtHiddenLayer.Mul(dOutput, weightHiddenOut.T())
dHiddenLayer := new(mat.Dense)
dHiddenLayer.MulElem(errorAtHiddenLayer, slopeHiddenLayer)
// Ajusta os parâmetros
weightOutAdj := new(mat.Dense)
weightOutAdj.Mul(InputHiddenLayerActivation.T(), dOutput)
weightOutAdj.Scale(nn.config.learningRate, weightOutAdj)
weightHiddenOut.Add(weightHiddenOut, weightOutAdj)
biasOutAdj, err := sumAlongAxis(0, dOutput)
if err != nil {
return err
}
biasOutAdj.Scale(nn.config.learningRate, biasOutAdj)
biasHiddenOut.Add(biasHiddenOut, biasOutAdj)
weightHiddenAdj := new(mat.Dense)
weightHiddenAdj.Mul(inputs.T(), dHiddenLayer)
weightHiddenAdj.Scale(nn.config.learningRate, weightHiddenAdj)
weightInputHidden.Add(weightInputHidden, weightHiddenAdj)
biasHiddenAdj, err := sumAlongAxis(0, dHiddenLayer)
if err != nil {
return err
}
biasHiddenAdj.Scale(nn.config.learningRate, biasHiddenAdj)
biasInputHidden.Add(biasInputHidden, biasHiddenAdj)
}
// fmt.Println(output.Dims())
// fmt.Println("output\n", mat.Formatted(output, mat.Squeeze()))
return nil
}
// sumAlongAxis soma uma matriz ao longo de uma dimensão específica,
// preservando a outra dimensão.
func sumAlongAxis(axis int, m *mat.Dense) (*mat.Dense, error) {
numRows, numCols := m.Dims()
var output *mat.Dense
switch axis {
case 0:
data := make([]float64, numCols)
for i := 0; i < numCols; i++ {
col := mat.Col(nil, i, m)
data[i] = floats.Sum(col)
}
output = mat.NewDense(1, numCols, data)
case 1:
data := make([]float64, numRows)
for i := 0; i < numRows; i++ {
row := mat.Row(nil, i, m)
data[i] = floats.Sum(row)
}
output = mat.NewDense(numRows, 1, data)
default:
return nil, errors.New("invalid axis, must be 0 or 1")
}
return output, nil
}
// Implementação do feed forward para previsão
// predict faz uma previsão com base em uma rede
// neural treinada.
func (nn *neuralNet) predict(x *mat.Dense) (*mat.Dense, error) {
// Verifica se o valor neuralNet representa um modelo treinado.
if nn.weightHidden == nil || nn.weightOut == nil {
return nil, errors.New("the supplied weights are empty")
}
if nn.biasHidden == nil || nn.biasOut == nil {
return nil, errors.New("the supplied biases are empty")
}
// Defina a saída da rede neural.
output := new(mat.Dense)
// Completa o processo de feed forward.
hiddenLayerInput := new(mat.Dense)
hiddenLayerInput.Mul(x, nn.weightHidden)
addBHidden := func(_, col int, v float64) float64 { return v + nn.biasHidden.At(0, col) }
hiddenLayerInput.Apply(addBHidden, hiddenLayerInput)
hiddenLayerActivations := new(mat.Dense)
applySigmoid := func(_, _ int, v float64) float64 { return sigmoid(v) }
hiddenLayerActivations.Apply(applySigmoid, hiddenLayerInput)
outputLayerInput := new(mat.Dense)
outputLayerInput.Mul(hiddenLayerActivations, nn.weightOut)
addBOut := func(_, col int, v float64) float64 { return v + nn.biasOut.At(0, col) }
outputLayerInput.Apply(addBOut, outputLayerInput)
output.Apply(applySigmoid, outputLayerInput)
return output, nil
}
// Lê o CSV
func makeInputsAndLabels(fileName string) (*mat.Dense, *mat.Dense) {
// Abra o arquivo do conjunto de dados.
f, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
defer f.Close()
// Crie um novo leitor de CSV lendo do arquivo aberto.
reader := csv.NewReader(f)
reader.FieldsPerRecord = 7
// Leia todos os registros CSV.
rawCSVData, err := reader.ReadAll()
if err != nil {
log.Fatal(err)
}
// inputsData e labelsData irão conter todos os
// valores de ponto flutuante que eventualmente serão
// usados para formar matrizes.
inputsData := make([]float64, 6*len(rawCSVData))
labelsData := make([]float64, 1*len(rawCSVData))
// Irá rastrear o índice atual dos valores da matriz.
var inputsIndex int
var labelsIndex int
// Move sequentially through the rows to a slice of floating-point values.
for idx, record := range rawCSVData {
// Skip the header row.
if idx == 0 {
continue
}
// Iterate over the floating-point columns.
for i, val := range record {
// Convert the value to float.
parsedVal, err := strconv.ParseFloat(val, 64)
if err != nil {
log.Fatal(err)
}
// Convert the value to a floating-point.
if i == 6 { // Assuming classe is the last column (index 6)
labelsData[labelsIndex] = parsedVal
labelsIndex++
continue
}
// Add to inputsData if relevant.
inputsData[inputsIndex] = parsedVal
inputsIndex++
}
}
inputs := mat.NewDense(len(rawCSVData), 6, inputsData)
labels := mat.NewDense(len(rawCSVData), 1, labelsData)
return inputs, labels
}
// Função para comparar previsões binarizadas com rótulos de teste e criar uma matriz 5x5
func comparePredictions(binarizedPredictions, testLabels *mat.Dense) *mat.Dense {
rows, _ := binarizedPredictions.Dims()
// Matriz para armazenar os índices onde o valor é 1 nas previsões binarizadas
indexMatrix := mat.NewDense(5, 5, nil)
for i := 0; i < rows; i++ {
// Encontre o índice onde o valor é 1 nas previsões binarizadas
_, predictedIdx := findMaxIndex(binarizedPredictions.RowView(i))
// Encontre o índice onde o valor é 1 nos rótulos de teste
_, trueIdx := findMaxIndex(testLabels.RowView(i))
// Incrementa em 1 na matriz zero onde a linha é predictedIdx e a coluna é trueIdx
currentValue := indexMatrix.At(predictedIdx, trueIdx)
indexMatrix.Set(predictedIdx, trueIdx, currentValue+1)
}
// Salvar a matriz resultante em um arquivo
saveResultToFile(indexMatrix)
return indexMatrix
}
// Função para salvar a matriz resultante em um arquivo
func saveResultToFile(matrix *mat.Dense) {
file, err := os.Create("resultado.txt")
if err != nil {
log.Fatal(err)
return
}
defer file.Close()
// Use mat.Format para formatar a matriz ao escrever no arquivo
fmt.Fprintln(file, "Matriz Gerada:")
fmt.Fprintln(file, mat.Formatted(matrix, mat.Squeeze()))
}