-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrainNetworkGPU.py
149 lines (119 loc) · 5.64 KB
/
TrainNetworkGPU.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
import torch
import numpy as np
import torch.nn as nn
import torch.utils.data as data_utils
import ChessResNet
from DoubleHeadDataset import DoubleHeadTrainingDataset
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 trainGPUNetwork(boards, policyOutputs, policyMag, valueOutputs, EPOCHS=1, BATCH_SIZE=1, LR=0.001,
loadDirectory='none.pt', saveDirectory='network1.pt'):
policyLossHistory = []
valueLossHistory = []
policyOutputs = torch.from_numpy(policyOutputs).double()
valueOutputs = torch.from_numpy(valueOutputs).double()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)
data = DoubleHeadTrainingDataset(boards, policyOutputs, policyMag,valueOutputs)
trainLoader = torch.utils.data.DataLoader(dataset=data, batch_size=BATCH_SIZE, shuffle=True)
# this is a residual network
model = ChessResNet.ResNetDoubleHead().double().cuda()
model.cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=LR)
try:
checkpoint = torch.load(loadDirectory)
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
totalLoss = checkpoint['loss']
except:
print("Pretrained NN model not found!")
policyCrit = nn.PoissonNLLLoss()
valueCrit = nn.MSELoss()
total_step = len(trainLoader)
trainNotFinished = True
for epoch in range(EPOCHS):
if trainNotFinished:
for i, (images, labels1, labels2) in enumerate(trainLoader):
images = images.to(device)
policyLabels = labels1.to(device)
valueLabels = labels2.to(device)
# Forward pass
outputPolicy, outputValue = model(images)
policyLoss = policyCrit(outputPolicy, policyLabels) * 4000
valueLoss = valueCrit(outputValue, valueLabels)
totalLoss = policyLoss + valueLoss
policyLossHistory.append(policyLoss.detach().cpu().numpy())
valueLossHistory.append(valueLoss.detach().cpu().numpy())
# Backward and optimize
optimizer.zero_grad()
totalLoss.backward()
optimizer.step()
if (i + 1) % 1 == 0:
print('Epoch [{}/{}], Step [{}/{}], Policy Loss: {:.4f}, Value Loss: {:.4f}'
.format(epoch + 1, EPOCHS, i + 1, total_step, policyLoss.item() / 4, valueLoss.item()))
if (i + 1) % 200 == 0:
# find predicted labels
values = np.exp((model(images)[0].data.detach().cpu().numpy()))
print("MAX:", np.amax(np.amax(values, axis=1)))
print("MIN:", np.amin(np.amin(values, axis=1)))
_, predicted = torch.max(model(images)[0].data, 1)
predicted = predicted.cpu().numpy()
print(predicted)
_, actual = torch.max(labels1.data, 1) # for poisson nll loss
actual = actual.numpy()
print(actual)
print("Correct:", (predicted == actual).sum())
if (i + 1) % 400 == 0:
# Save Model
torch.save({
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': totalLoss,
}, saveDirectory)
# Save Loss History
outF = open("Network Logs/policyLossHistory.txt", "w")
for k in range(len(policyLossHistory)):
# write line to output file
outF.write(str(policyLossHistory[k]))
outF.write("\n")
outF.close()
outF = open("Network Logs/valueLossHistory.txt", "w")
for l in range(len(valueLossHistory)):
# write line to output file
outF.write(str(valueLossHistory[l]))
outF.write("\n")
outF.close()
print("Updated!")
# Save Model
torch.save({
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': totalLoss,
}, saveDirectory)
# Save Loss History
outF = open("Network Logs/policyLossHistory.txt", "w")
for m in range(len(policyLossHistory)):
# write line to output file
outF.write(str(policyLossHistory[m]))
outF.write("\n")
outF.close()
outF = open("Network Logs/valueLossHistory.txt", "w")
for n in range(len(valueLossHistory)):
# write line to output file
outF.write(str(valueLossHistory[n]))
outF.write("\n")
outF.close()
train = True
if train:
with h5py.File("Training Data/StockfishInputs3[binaryConverted].h5", 'r') as hf:
boards = hf["Inputs"][:]
print(len(boards))
with h5py.File("Training Data/StockfishOutputs3.h5", 'r') as hf:
policy = hf["Policy Outputs"][:]
policyMag = hf["Policy Magnitude Outputs"][:]
value = hf["Value Outputs"][:]
print(len(value))
trainGPUNetwork(boards, policy, policyMag, value, loadDirectory="New Networks/[12x256_16_8]64fish.pt",
saveDirectory="New Networks/[12x256_16_8]64fish.pt", EPOCHS=2,
BATCH_SIZE=128, LR=0.001)