-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
53 lines (41 loc) · 1.74 KB
/
train.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
52
53
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 5 22:05:00 2017
@author: taras
"""
import random
import os
from goban.io import loadTrainingData, saveNeuralNetwork
from goban.nnutils import asInputVector, asOutputVector, enrichWithFlips, enrichWithRotations
from goban.nn import Network
from goban.ui.console import yesno
import config
data = loadTrainingData(config.DATA_FILENAME, config.FRAGMENT_SIZE);
if config.TRAINING_ADD_ROTATIONS:
enrichWithRotations(data);
if config.TRAINING_ADD_FLIPS:
enrichWithFlips(data);
if len(data) <= config.VALIDATION_SET_SIZE:
print("Not enough data points for validation set size %d!" % config.VALIDATION_SET_SIZE);
exit(1);
print("Training set size: %d\nValidation set size: %d" % (len(data)-config.VALIDATION_SET_SIZE, config.VALIDATION_SET_SIZE));
random.shuffle(data);
validationData = [];
trainingData = [];
for i in range(0, len(data)):
x = asInputVector(data[i][0]);
y = asOutputVector(data[i][1]);
# First populate validation data, then - training data
if i < config.VALIDATION_SET_SIZE:
validationData.append((x, y));
else:
trainingData.append((x, y));
network = Network([config.FRAGMENT_SIZE[0]*config.FRAGMENT_SIZE[1], 128, 64, 3]);
network.SGD(trainingData, 50, 100, 0.5, test_data=validationData if config.LIVE_VALIDATION else None);
correctPredictions = network.evaluate(validationData);
totalPredictions = len(validationData);
successRate = correctPredictions / totalPredictions;
print("Resulting success rate: %d/%d (%f%%)" % (correctPredictions, totalPredictions, successRate*100.0));
if (not os.path.isfile(config.NETWORK_FILENAME)) or yesno("%s already exists. Overwrite?"):
saveNeuralNetwork(network, config.NETWORK_FILENAME);