-
Notifications
You must be signed in to change notification settings - Fork 0
/
alpha_net.py
153 lines (133 loc) · 5.63 KB
/
alpha_net.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
#!/usr/bin/env python
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import os
import datetime
class board_data(Dataset):
def __init__(self, dataset): # dataset = np.array of (s, p, v)
self.X = dataset[:,0]
self.y_p, self.y_v = dataset[:,1], dataset[:,2]
def __len__(self):
return len(self.X)
def __getitem__(self,idx):
return self.X[idx].transpose(2,0,1), self.y_p[idx], self.y_v[idx]
class ConvBlock(nn.Module):
def __init__(self):
super(ConvBlock, self).__init__()
self.action_size = 8*8*73
self.conv1 = nn.Conv2d(22, 256, 3, stride=1, padding=1)
self.bn1 = nn.BatchNorm2d(256)
def forward(self, s):
s = s.view(-1, 22, 8, 8) # batch_size x channels x board_x x board_y
s = F.relu(self.bn1(self.conv1(s)))
return s
class ResBlock(nn.Module):
def __init__(self, inplanes=256, planes=256, stride=1, downsample=None):
super(ResBlock, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=3, stride=stride,
padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
def forward(self, x):
residual = x
out = self.conv1(x)
out = F.relu(self.bn1(out))
out = self.conv2(out)
out = self.bn2(out)
out += residual
out = F.relu(out)
return out
class OutBlock(nn.Module):
def __init__(self):
super(OutBlock, self).__init__()
self.conv = nn.Conv2d(256, 1, kernel_size=1) # value head
self.bn = nn.BatchNorm2d(1)
self.fc1 = nn.Linear(8*8, 64)
self.fc2 = nn.Linear(64, 1)
self.conv1 = nn.Conv2d(256, 128, kernel_size=1) # policy head
self.bn1 = nn.BatchNorm2d(128)
self.logsoftmax = nn.LogSoftmax(dim=1)
self.fc = nn.Linear(8*8*128, 8*8*73)
def forward(self,s):
v = F.relu(self.bn(self.conv(s))) # value head
v = v.view(-1, 8*8) # batch_size X channel X height X width
v = F.relu(self.fc1(v))
v = F.tanh(self.fc2(v))
p = F.relu(self.bn1(self.conv1(s))) # policy head
p = p.view(-1, 8*8*128)
p = self.fc(p)
p = self.logsoftmax(p).exp()
return p, v
class ChessNet(nn.Module):
def __init__(self):
super(ChessNet, self).__init__()
self.conv = ConvBlock()
for block in range(19):
setattr(self, "res_%i" % block,ResBlock())
self.outblock = OutBlock()
def forward(self,s):
s = self.conv(s)
for block in range(19):
s = getattr(self, "res_%i" % block)(s)
s = self.outblock(s)
return s
class AlphaLoss(torch.nn.Module):
def __init__(self):
super(AlphaLoss, self).__init__()
def forward(self, y_value, value, y_policy, policy):
value_error = (value - y_value) ** 2
policy_error = torch.sum((-policy*
(1e-6 + y_policy.float()).float().log()), 1)
total_error = (value_error.view(-1).float() + policy_error).mean()
return total_error
def train(net, dataset, epoch_start=0, epoch_stop=20, cpu=0):
torch.manual_seed(cpu)
cuda = torch.cuda.is_available()
net.train()
criterion = AlphaLoss()
optimizer = optim.Adam(net.parameters(), lr=0.003)
scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[100,200,300,400], gamma=0.2)
train_set = board_data(dataset)
train_loader = DataLoader(train_set, batch_size=30, shuffle=True, num_workers=0, pin_memory=False)
losses_per_epoch = []
for epoch in range(epoch_start, epoch_stop):
scheduler.step()
total_loss = 0.0
losses_per_batch = []
for i,data in enumerate(train_loader,0):
state, policy, value = data
if cuda:
state, policy, value = state.cuda().float(), policy.float().cuda(), value.cuda().float()
optimizer.zero_grad()
policy_pred, value_pred = net(state) # policy_pred = torch.Size([batch, 4672]) value_pred = torch.Size([batch, 1])
loss = criterion(value_pred[:,0], value, policy_pred, policy)
loss.backward()
optimizer.step()
total_loss += loss.item()
if i % 10 == 9: # print every 10 mini-batches of size = batch_size
print('Process ID: %d [Epoch: %d, %5d/ %d points] total loss per batch: %.3f' %
(os.getpid(), epoch + 1, (i + 1)*30, len(train_set), total_loss/10))
print("Policy:",policy[0].argmax().item(),policy_pred[0].argmax().item())
print("Value:",value[0].item(),value_pred[0,0].item())
losses_per_batch.append(total_loss/10)
total_loss = 0.0
losses_per_epoch.append(sum(losses_per_batch)/len(losses_per_batch))
if len(losses_per_epoch) > 100:
if abs(sum(losses_per_epoch[-4:-1])/3-sum(losses_per_epoch[-16:-13])/3) <= 0.01:
break
fig = plt.figure()
ax = fig.add_subplot(222)
ax.scatter([e for e in range(1,epoch_stop+1,1)], losses_per_epoch)
ax.set_xlabel("Epoch")
ax.set_ylabel("Loss per batch")
ax.set_title("Loss vs Epoch")
print('Finished Training')
plt.savefig(os.path.join("./model_data/", "Loss_vs_Epoch_%s.png" % datetime.datetime.today().strftime("%Y-%m-%d")))