-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_93_40_1.py
51 lines (40 loc) · 1.33 KB
/
net_93_40_1.py
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
#!/usr/bin/python
#-*- coding: utf-8 -*-
from main import neuralNetAutoMain
from neuralnet import NeuralNetwork
from statistics import mean
def log2kb(log, score):
"""
Transform the log from previous game to an array understandable by the neural network.
Arg:
-log : array containing the states a game has been in.
-score : score of the game associated with the log.
"""
kb = []
for state in log:
kb.append((state.toInputs(), [score]))
return kb
def trainOnGame(net):
"""
Play a game to save the best computed moves and feed the neural network .
Arg:
-model : our neural network.
"""
log = neuralNetAutoMain(neuralNet=net)
score = log[-1].getScore()
net.train(log2kb(log, score), doTests=False)
print(score)
return score
if __name__ == '__main__':
for j in range(10):
nn = NeuralNetwork(neuronsPerLayer=[93, 40, 1]) # 93 inputs with a hidden layer of 40 nodes and 1 output
scores = [0 for _ in range(5000)]
i = 0
while mean(scores) < 20:
scores[i % 5000] = trainOnGame(net=nn)
if i % 5000 == 0:
with open("scores" + str(j) + ".csv", mode='a') as file:
file.write(str(mean(scores)) + ";")
i += 1
print(i, end="\t")
file.close()