-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
164 lines (142 loc) · 4.31 KB
/
main.go
File metadata and controls
164 lines (142 loc) · 4.31 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
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
package main
import (
"context"
"fmt"
"os"
"strconv"
wq "github.com/bestbug456/dotateacher/workingqueue"
"github.com/aws/aws-lambda-go/lambda"
"gopkg.in/mgo.v2"
)
var compressed map[int]int
var (
JOBNUMBER = 100
)
func init() {
compressed = make(map[int]int)
for i := 0; i < len(uncompressed); i++ {
compressed[uncompressed[i]] = int(i)
}
}
func main() {
trainAndValidateNN()
lambda.Start(HandleRequest)
}
func HandleRequest(ctx context.Context, data interface{}) (string, error) {
return trainAndValidateNN()
}
func trainAndValidateNN() (string, error) {
address := os.Getenv("address")
username := os.Getenv("username")
password := os.Getenv("password")
option := os.Getenv("option")
ssl := os.Getenv("ssl")
jobsNumber := os.Getenv("nrjobs")
if jobsNumber != "" {
var err error
JOBNUMBER, err = strconv.Atoi(jobsNumber)
if err != nil {
return "", fmt.Errorf("Error while converting jobsNumber: %s", err.Error())
}
}
var s *mgo.Session
var err error
if ssl == "false" {
s, err = mgo.Dial(fmt.Sprintf("mongodb://%s:%s@%s/%s", username, password, address, option))
if err != nil {
return "", fmt.Errorf("Error while connecting without ssl: %s", err.Error())
}
} else {
s, err = DialUsingSSL(address, option, username, password)
if err != nil {
return "", fmt.Errorf("Error while connecting via ssl: %s", err.Error())
}
}
defer s.Close()
err = s.DB("opendota-infos").C("matchs").EnsureIndex(mgo.Index{
Unique: true,
Key: []string{"id"},
})
traindata, testdata, err := getDatasetAndTrainSet(s)
if err != nil {
return "", fmt.Errorf("Error while get trainset and testset: %s", err.Error())
}
// Get old weights if exist or create new one
// if is the first run.
NN, err := getActualNewNeuralNetwork(s)
if err != nil && err != mgo.ErrNotFound {
return "", fmt.Errorf("Error while get actual weights: %s", err.Error())
}
var created bool
if err != nil {
created = true
NN, err = trainNewNeuralNetwork(traindata, 25)
}
// Check how many correct prediction we have
storedNNResult := checkWeightsQuality(NN, testdata)
accuracy, sensitivity := getStastFromMatrixQA(storedNNResult)
// If we didn't have at least 70% of accuracy
// try to find a better weights.
if accuracy < 0.8 || sensitivity < 0.8 {
wq := wq.NewWorkingQueue(50, 200, nil)
wq.Run()
responseChan := make(chan NNmessage, 0)
for i := 0; i < JOBNUMBER; i++ {
wq.SendJob(CreateNewNeuralNetworkAndValidate, &JobArgs{
TrainData: traindata,
TestData: testdata,
NeuronNumber: i,
result: responseChan,
})
}
var bestaccuracy float64
var bestsensitivity float64
var bestResult NNmessage
for i := 0; i < JOBNUMBER; i++ {
result := <-responseChan
// Ignore invalid matrix
if result.MatrixQA == nil ||
result.MatrixQA.ConfusionMatrix == nil ||
len(result.MatrixQA.ConfusionMatrix) != 2 ||
len(result.MatrixQA.ConfusionMatrix[0]) != 2 ||
len(result.MatrixQA.ConfusionMatrix[1]) != 2 {
continue
}
resultaccuracy, resultsensitivity := getStastFromMatrixQA(result.MatrixQA)
if resultaccuracy >= bestaccuracy && resultsensitivity >= bestsensitivity {
bestaccuracy = resultaccuracy
bestsensitivity = resultsensitivity
bestResult = result
}
}
// If the new weights have at least 70% of accuracy
// OR is better then the actual save it to the database
if bestResult.MatrixQA == nil {
return "", fmt.Errorf("MatrixQA have zero len (%+v)", bestResult.MatrixQA)
}
if bestaccuracy >= accuracy && bestsensitivity >= sensitivity {
err = storeNewNeuralNetworkAndQAResults(bestResult, s)
if err != nil {
return "", fmt.Errorf("Error while storing actual weights: %s", err.Error())
}
}
// We just created the new NN, store it to the database
if created {
if float64(bestResult.MatrixQA.CorrectPrediction)/float64(len(testdata)) > float64(storedNNResult.CorrectPrediction)/float64(len(testdata)) {
err = storeNewNeuralNetworkAndQAResults(bestResult, s)
if err != nil {
return "", fmt.Errorf("Error while storing actual weights: %s", err.Error())
}
} else {
err = storeNewNeuralNetworkAndQAResults(NNmessage{
NN: NN,
MatrixQA: storedNNResult,
}, s)
if err != nil {
return "", fmt.Errorf("Error while storing actual weights: %s", err.Error())
}
}
}
}
return "", nil
}