forked from riceluxs1t/KBOPrediction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainer.py
80 lines (68 loc) · 2.53 KB
/
trainer.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
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
import os
import sys
import argparse
import json
import tensorflow as tf
import numpy as np
from builder import SeLuModel
from builder import Runner
from constants import *
from formatter import Formatter
DIRNAME = os.path.dirname(os.path.realpath(__file__))
parser = argparse.ArgumentParser(description='KBO Score Prediction Trainer')
parser.add_argument('year', type=int, help='The year of data to train the model with')
parser.add_argument('train_size', type=float, help='The proportion of the training set to the test set')
parser.add_argument('model_name', type=str, help='The name of the model')
parser.add_argument('learn_rate', type=float, help='The learning rate')
parser.add_argument('sequence_length', type=int, help='Sequence length') #TODO
parser.add_argument('epoch', type=int, help='Training epoch')
parser.add_argument('drop_rate', type=float, help='Drop rate')
if __name__ == '__main__':
args = parser.parse_args()
# ====== Open data file ======
file_name = ''
if args.year == 2017:
file_name = DATA_17
elif args.year == 2016:
file_name = DATA_16
f = open(DIRNAME + "/" + file_name, 'r')
print("Loading JSON data")
data = json.load(f)
print("Preprocessing the data")
# formatter class that contains trainX, trainY, testX, testY for individual teams
formatter = Formatter(data, args.train_size, args.sequence_length)
trainX_home, trainX_away, trainY, testX_home, testX_away, testY = formatter.get_data()
## ======== Build model ======
with tf.Session() as sess:
kbo_pred_model = SeLuModel(
sess,
args.model_name,
learn_rate=args.learn_rate,
sequence_length=args.sequence_length
)
## ======== Train model ======
print("Started the training...")
kbo_runner = Runner()
kbo_runner.train_run(
kbo_pred_model,
trainX_home,
trainX_away,
trainY,
training_epoch=args.epoch,
keep_prob=(1 - args.drop_rate)
)
print("Training done.")
print("Start Testing")
## ======== Run test =========
accuracy = kbo_runner.get_accuracy(
kbo_pred_model,
testX_home,
testX_away,
testY
)
print("The percentage of the games predicted correctly")
print(accuracy)
## ======= Save the trained model ======
print("Saving the trained model...")
kbo_pred_model.save()
print("Save complete.")