-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trainer.py
235 lines (195 loc) · 6.67 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import csv
import tensorflow as tf
import os
from numpy import genfromtxt
import numpy as np
import random
import math
import constants
import pickle
from tfx import v1 as tfx
ITERATIONS = 60
NODES = 43 # 42 locations on the board, and the last is the current turn
HIDDEN_NODES = 128
def get_next_model_file_name() -> str:
i = 0
while os.path.exists("models/win_estimate_%s.model" % i):
i += 1
return "models/win_estimate_%s.model" % i
def get_latest_model_file_name() -> str:
i = 0
while os.path.exists("models/win_estimate_%s.model" % i):
i += 1
return "models/win_estimate_%s.model" % (i-1)
def get_most_recent_model():
return tf.keras.models.load_model(get_latest_model_file_name())
def get_total_data_files() -> int:
i = 0
for filename in os.listdir('results'):
i += 1
return i - 1
def data_with_only_known_results(file_data):
known_lines = []
for line in file_data:
if line[0] == 0 or line[0] == 1:
known_lines.append(line)
return np.array(known_lines)
def load_data_newest():
total_files = get_total_data_files()
data = {
'labels': None,
'features': None
}
file_data = None
i = 0
for filename in os.listdir('results'):
only_take_known_results = True
if i > total_files - 20:
only_take_known_results = False
i += 1
file_data = genfromtxt('results/' + filename, delimiter=',')
if only_take_known_results:
file_data = data_with_only_known_results(file_data)
label_array = np.array(file_data[:, 0])
label_array = np.reshape(label_array, (-1, 1))
if data['labels'] is None:
data['labels'] = label_array
data['features'] = np.vstack([file_data[:, 1:]])
else:
data['labels'] = np.vstack([data['labels'], label_array])
data['features'] = np.vstack([data['features'], np.array(file_data[:,1:])])
return data
def load_pickled_data():
with open(constants.MOST_RECENT_PICKLE_FILE, 'rb') as f:
obj = pickle.load(f)
return obj
def load_data():
data = {
'labels': None,
'features': None
}
file_data = None
for filename in os.listdir('results'):
file_data = genfromtxt('results/' + filename, delimiter=',')
if data['labels'] is None:
label_array = np.array(file_data[:,0])
label_array = np.reshape(label_array, (-1, 1))
data['labels'] = label_array
data['features'] = np.vstack([file_data[:, 1:]])
else:
label_array = np.array(file_data[:,0])
label_array = np.reshape(label_array, (-1, 1))
data['labels'] = np.vstack([data['labels'], label_array])
data['features'] = np.vstack([data['features'], np.array(file_data[:,1:])])
return data
def get_thick_model():
hidden_nodes = 256
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(NODES, input_shape=(NODES,)),
tf.keras.layers.Dense(hidden_nodes, activation='relu'),
tf.keras.layers.Dense(hidden_nodes, activation='relu'),
tf.keras.layers.Dense(1)
])
return model
def get_long_model():
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(NODES, input_shape=(NODES,)),
tf.keras.layers.Dense(92, activation='relu'),
tf.keras.layers.Dense(42, activation='relu'),
tf.keras.layers.Dense(42, activation='relu'),
tf.keras.layers.Dense(7, activation='sigmoid'),
tf.keras.layers.Dense(1)
])
return model
def get_massive_model():
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(NODES, input_shape=(NODES,)),
tf.keras.layers.Dense(528, activation='relu'),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(7, activation='sigmoid'),
tf.keras.layers.Dense(1)
])
return model
def train_model(data):
model = get_massive_model()
loss_fn = tf.keras.losses.MeanSquaredError()
model.compile(optimizer='adam',
loss=loss_fn,
metrics=['accuracy'])
model.fit(data['train_features'], data['train_labels'],
epochs=ITERATIONS,
validation_data=(data['validation_features'], data['validation_labels']))
return model
def show_sample_output(data, model):
for i in range(40):
rand_row = random.randint(0, data['test_features'].shape[0])
print("looking at move number " + str(rand_row))
features = np.copy(data['test_features'][rand_row, :])
prediction = model.predict(np.reshape(features, (1, NODES)))
print("expected" + str(data['test_labels'][rand_row]) + " prediction " + str(prediction))
board = np.copy(features[0:42])
print(np.reshape(board, (7, 6)))
def get_num_moves(board) -> int:
total = 0
for val in board:
if val == 0 or val ==1:
total += 1
return total
def show_wrong_output(data, model):
print('=================== Wrong Labels ==============')
total_wrong = 0
very_wrong_total = 0
wrong_at_num = {}
for i in range(6*7 + 1):
wrong_at_num[i] = 0
for i in range(data['test_features'].shape[0]):
features = np.copy(data['test_features'][i, :])
prediction_arr = model.predict(np.reshape(features, (1, NODES)))
prediction = prediction_arr[0][0]
actual = data['test_labels'][i]
if abs(actual - prediction) > 0.25:
print("expected" + str(data['test_labels'][i]) + " prediction " + str(prediction))
board = np.copy(features[0:42])
print(np.reshape(board, (7, 6)))
total_wrong += 1
wrong_at_num[get_num_moves(board)] += 1
if abs(actual - prediction) > 0.7:
very_wrong_total +=1
print( ' ==========> WRONG ' + str(total_wrong) + ' / ' + str(data['test_features'].shape[0]))
print( ' ==========> VERY_WRONG ' + str(very_wrong_total) + ' / ' + str(data['test_features'].shape[0]))
print(' wrong by number of moves ' + str(wrong_at_num))
def evaluate_model(model, data):
predictions = model.predict(data['test_features'])
total = 0
total_wrong = 0
very_wrong = 0
for prediction, label in zip(predictions, data['test_labels']):
total += 1
p = prediction[0]
if p > 1:
p = 1
if p < 0:
p = 0
if (p-label) > 0.4:
total_wrong += 1
if (p-label) > 0.7:
very_wrong += 1
print ('wrong ' + str(total_wrong) + '/' + str(total) + " very wrong " + str(very_wrong) + "/" + str(total) )
def test_model(model, data):
results = model.evaluate(data['test_features'], data['test_labels'])
print(results)
def train_newest_model():
data = load_pickled_data()
model = train_model(data)
#show_sample_output(data, model)
#show_wrong_output(data,model)
evaluate_model(model, data)
test_model(model, data)
out_file_name = get_next_model_file_name()
model.save(out_file_name)
# TFX will call this function.
def run_fn(fn_args: tfx.components.FnArgs):
train_newest_model()
if __name__ == '__main__':
train_newest_model()