-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrainValueNetwork.py
213 lines (173 loc) · 7.51 KB
/
TrainValueNetwork.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
import torch
import numpy as np
import torch.nn as nn
import torch.utils.data as data_utils
from ChessConvNet import ChessConvNet
import ChessResNet
from ValueDataset import ValueDataset
import h5py
# inputs and outputs are numpy arrays. This method of checking accuracy only works with imported games.
# if it's not imported, accuracy will never be 100%, so it will just output the trained network after 10,000 epochs.
def trainValueNetwork(boards, outputs, EPOCHS=1, BATCH_SIZE=1, LR=0.001,
loadDirectory='none.pt',
saveDirectory='network1.pt', OUTPUT_ARRAY_LEN=4504):
outputs = torch.from_numpy(outputs)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)
data = ValueDataset(boards, outputs)
trainLoader = torch.utils.data.DataLoader(dataset=data, batch_size=BATCH_SIZE, shuffle=True)
# this is a residual network
model = ChessResNet.ValueResNet().double()
try:
model = torch.load(loadDirectory)
except:
print("Pretrained NN model not found!")
criterion = nn.MSELoss() # MSELoss // PoissonNLLLoss //
optimizer = torch.optim.Adam(model.parameters(), lr=LR) # , weight_decay=0.00001)
total_step = len(trainLoader)
trainNotFinished = True
for epoch in range(EPOCHS):
if trainNotFinished:
for i, (images, labels) in enumerate(trainLoader):
images = images.to(device)
labels = labels.to(device)
# Forward pass
outputMoves = model(images)
loss = criterion(outputMoves, labels)
if (i + 1) % 150 == 0:
# find predicted labels
predicted = model(images).data
actual = labels.data
predicted = predicted.numpy().flatten()
actual = actual.numpy().flatten()
print(predicted)
print(actual)
print(np.abs(predicted-actual))
correct = 0
for j in range(BATCH_SIZE):
if np.abs(predicted-actual)[j]<0.2:
correct += 1
print("Correct:", correct)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i + 1) % 1 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch + 1, EPOCHS, i + 1, total_step, loss.item()))
if (i + 1) % 200 == 0:
torch.save(model, saveDirectory)
print("Updated!")
torch.save(model, saveDirectory)
train = True
if train:
with h5py.File("Training Data/18-01Inputs.h5", 'r') as hf:
boards = hf["Inputs"][:]
print(len(boards))
with h5py.File("Training Data/18-01ValueOutputs.h5", 'r') as hf:
outputs = hf["Outputs"][:]
print(len(outputs))
trainValueNetwork(boards, outputs, loadDirectory="",
saveDirectory="New Networks/18011810-VALUE.pt", EPOCHS=1,
BATCH_SIZE=64, LR=0.001)
boards = []
outputs = []
with h5py.File("Training Data/18-02Inputs.h5", 'r') as hf:
boards = hf["Inputs"][:]
print(len(boards))
with h5py.File("Training Data/18-02ValueOutputs.h5", 'r') as hf:
outputs = hf["Outputs"][:]
print(len(outputs))
trainValueNetwork(boards, outputs, loadDirectory="New Networks/18011810-VALUE.pt",
saveDirectory="New Networks/18011810-VALUE.pt", EPOCHS=1,
BATCH_SIZE=64, LR=0.001)
boards = []
outputs = []
with h5py.File("Training Data/18-03Inputs.h5", 'r') as hf:
boards = hf["Inputs"][:]
print(len(boards))
with h5py.File("Training Data/18-03ValueOutputs.h5", 'r') as hf:
outputs = hf["Outputs"][:]
print(len(outputs))
trainValueNetwork(boards, outputs, loadDirectory="New Networks/18011810-VALUE.pt",
saveDirectory="New Networks/18011810-VALUE.pt", EPOCHS=1,
BATCH_SIZE=64, LR=0.001)
boards = []
outputs = []
with h5py.File("Training Data/18-04Inputs.h5", 'r') as hf:
boards = hf["Inputs"][:]
print(len(boards))
with h5py.File("Training Data/18-04ValueOutputs.h5", 'r') as hf:
outputs = hf["Outputs"][:]
print(len(outputs))
trainValueNetwork(boards, outputs, loadDirectory="New Networks/18011810-VALUE.pt",
saveDirectory="New Networks/18011810-VALUE.pt", EPOCHS=1,
BATCH_SIZE=64, LR=0.001)
boards = []
outputs = []
with h5py.File("Training Data/18-05Inputs.h5", 'r') as hf:
boards = hf["Inputs"][:]
print(len(boards))
with h5py.File("Training Data/18-05ValueOutputs.h5", 'r') as hf:
outputs = hf["Outputs"][:]
print(len(outputs))
trainValueNetwork(boards, outputs, loadDirectory="New Networks/18011810-VALUE.pt",
saveDirectory="New Networks/18011810-VALUE.pt", EPOCHS=1,
BATCH_SIZE=64, LR=0.001)
boards = []
outputs = []
with h5py.File("Training Data/18-06Inputs.h5", 'r') as hf:
boards = hf["Inputs"][:]
print(len(boards))
with h5py.File("Training Data/18-06ValueOutputs.h5", 'r') as hf:
outputs = hf["Outputs"][:]
print(len(outputs))
trainValueNetwork(boards, outputs, loadDirectory="New Networks/18011810-VALUE.pt",
saveDirectory="New Networks/18011810-VALUE.pt", EPOCHS=1,
BATCH_SIZE=64, LR=0.001)
boards = []
outputs = []
with h5py.File("Training Data/18-07Inputs.h5", 'r') as hf:
boards = hf["Inputs"][:]
print(len(boards))
with h5py.File("Training Data/18-07ValueOutputs.h5", 'r') as hf:
outputs = hf["Outputs"][:]
print(len(outputs))
trainValueNetwork(boards, outputs, loadDirectory="New Networks/18011810-VALUE.pt",
saveDirectory="New Networks/18011810-VALUE.pt", EPOCHS=1,
BATCH_SIZE=64, LR=0.001)
boards = []
outputs = []
with h5py.File("Training Data/18-08Inputs.h5", 'r') as hf:
boards = hf["Inputs"][:]
print(len(boards))
with h5py.File("Training Data/18-08ValueOutputs.h5", 'r') as hf:
outputs = hf["Outputs"][:]
print(len(outputs))
trainValueNetwork(boards, outputs, loadDirectory="New Networks/18011810-VALUE.pt",
saveDirectory="New Networks/18011810-VALUE.pt", EPOCHS=1,
BATCH_SIZE=64, LR=0.001)
boards = []
outputs = []
with h5py.File("Training Data/18-09Inputs.h5", 'r') as hf:
boards = hf["Inputs"][:]
print(len(boards))
with h5py.File("Training Data/18-09ValueOutputs.h5", 'r') as hf:
outputs = hf["Outputs"][:]
print(len(outputs))
trainValueNetwork(boards, outputs, loadDirectory="New Networks/18011810-VALUE.pt",
saveDirectory="New Networks/18011810-VALUE.pt", EPOCHS=1,
BATCH_SIZE=64, LR=0.001)
boards = []
outputs = []
with h5py.File("Training Data/18-10Inputs.h5", 'r') as hf:
boards = hf["Inputs"][:]
print(len(boards))
with h5py.File("Training Data/18-10ValueOutputs.h5", 'r') as hf:
outputs = hf["Outputs"][:]
print(len(outputs))
trainValueNetwork(boards, outputs, loadDirectory="New Networks/18011810-VALUE.pt",
saveDirectory="New Networks/18011810-VALUE.pt", EPOCHS=1,
BATCH_SIZE=64, LR=0.001)
boards = []
outputs = []