-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteach.go
More file actions
129 lines (115 loc) · 3.4 KB
/
Copy pathteach.go
File metadata and controls
129 lines (115 loc) · 3.4 KB
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
package main
import (
"fmt"
"log"
rprop "github.com/bestbug456/gorpropplus"
)
func CreateNewNeuralNetworkAndValidate(args interface{}) error {
infos := args.(*JobArgs)
NN, err := trainNewNeuralNetwork(infos.TrainData, infos.NeuronNumber)
if err != nil {
return err
}
QAresults := checkWeightsQuality(NN, infos.TestData)
infos.result <- NNmessage{
MatrixQA: QAresults,
NN: NN,
}
return nil
}
func orderPickByTeamAndCreateBitmask(picks []int) []float64 {
updatedpick := make([]int, len(picks))
for i := 0; i < len(picks); i++ {
updatedpick[i] = compressed[picks[i]]
}
team1Pick := []int{
updatedpick[0],
updatedpick[3],
updatedpick[5],
updatedpick[7],
updatedpick[8],
}
team2Pick := []int{
updatedpick[1],
updatedpick[2],
updatedpick[4],
updatedpick[6],
updatedpick[9],
}
bitmasks := createBitmasksForTeam(team1Pick)
supp := createBitmasksForTeam(team2Pick)
bitmasks = append(bitmasks, supp...)
return bitmasks
}
func trainNewNeuralNetwork(traindata []MatchInfos, neuron int) (*rprop.NeuralNetwork, error) {
var hiddenLayer []int
// neuron += 100
// if neuron > 125 {
// hiddenLayer = make([]int, 2)
// hiddenLayer[0] = 125
// hiddenLayer[1] = neuron - 125
// } else {
// hiddenLayer = make([]int, 1)
// hiddenLayer[0] = neuron
// }
hiddenLayer = make([]int, 1)
hiddenLayer[0] = neuron
args := rprop.NeuralNetworkArguments{
HiddenLayer: hiddenLayer,
InputSize: 230,
OutputSize: 1,
Threshold: 0.001,
StepMax: 999999999999999999,
LifeSignStep: 1000,
LinearOutput: false,
Minus: 0.5,
Plus: 1.2,
ActivationFunction: rprop.Logistic,
DerivateActivation: rprop.DerivateLogistic,
ErrorFunction: rprop.SSE,
DerivateError: rprop.DerivateSSE,
}
// Get a fresh new neural network
NN, err := rprop.NewNeuralNetworkAndSetup(args)
if err != nil {
return nil, fmt.Errorf("Error while creating a new neural network: %s", err.Error())
}
inputData := make([][]float64, len(traindata))
outputData := make([][]float64, len(traindata))
for i := 0; i < len(traindata); i++ {
inputData[i] = orderPickByTeamAndCreateBitmask(traindata[i].Picks)
outputData[i] = make([]float64, 1)
outputData[i] = []float64{float64(traindata[i].Win)}
}
err = NN.Train(inputData, outputData)
if err != nil {
return nil, fmt.Errorf("Error while training the neural network: %s", err.Error())
}
return NN, nil
}
func getStastFromMatrixQA(info *rprop.ValidationResult) (float64, float64) {
return float64(info.ConfusionMatrix[0][0]) / float64(info.ConfusionMatrix[0][0]+info.ConfusionMatrix[1][0]),
float64(info.ConfusionMatrix[1][1]) / float64(info.ConfusionMatrix[1][1]+info.ConfusionMatrix[0][1])
}
func checkWeightsQuality(NN *rprop.NeuralNetwork, testdata []MatchInfos) *rprop.ValidationResult {
inputData := make([][]float64, len(testdata))
outputData := make([][]float64, len(testdata))
for i := 0; i < len(testdata); i++ {
inputData[i] = orderPickByTeamAndCreateBitmask(testdata[i].Picks)
outputData[i] = make([]float64, 1)
outputData[i] = []float64{float64(testdata[i].Win)}
}
NN.Threshold = 0.15
ris, err := NN.Validate(inputData, outputData)
if err != nil {
log.Printf("Error: %s", err.Error())
}
return ris
}
func createBitmasksForTeam(team []int) []float64 {
bitmasks := make([]float64, 115)
for i := 0; i < len(team); i++ {
bitmasks[team[i]] = 1
}
return bitmasks
}