-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
278 lines (234 loc) · 9.28 KB
/
model.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import os
import time
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
import config
import game
from progress.bar import Bar
from progress.misc import AverageMeter
def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1):
"""3x3 convolution with padding"""
return nn.Conv2d(
in_planes,
out_planes,
kernel_size=3,
stride=stride,
padding=dilation,
groups=groups,
bias=False,
dilation=dilation,
)
def conv1x1(in_planes, out_planes, stride=1):
"""1x1 convolution"""
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
class ConvolutionalLayer(nn.Module):
def __init__(self, inplanes, planes, stride=1, norm_layer=None):
super(ConvolutionalLayer, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = norm_layer(planes)
self.relu = nn.LeakyReLU(inplace=True)
self.stride = stride
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
return out
class ResidualLayer(nn.Module):
def __init__(self, inplanes, planes, stride=1, norm_layer=None):
super(ResidualLayer, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = norm_layer(planes)
self.relu = nn.LeakyReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = norm_layer(planes)
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out += identity
out = self.relu(out)
return out
class ValueHead(nn.Module):
def __init__(self, inplanes, hidden_layer_size, width, height, norm_layer=None):
super(ValueHead, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
self.conv1 = conv1x1(inplanes, 1)
self.bn1 = norm_layer(1)
self.relu = nn.LeakyReLU(inplace=True)
self.fc1 = nn.Linear(width * height, hidden_layer_size)
self.fc2 = nn.Linear(hidden_layer_size, 1)
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = torch.flatten(out, 1)
out = self.fc1(out)
out = self.relu(out)
out = self.fc2(out)
out = torch.tanh(out)
return out
class PolicyHead(nn.Module):
def __init__(self, inplanes, width, height, norm_layer=None):
super(PolicyHead, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
self.conv1 = conv1x1(inplanes, 2)
self.bn1 = norm_layer(2)
self.relu = nn.LeakyReLU(inplace=True)
self.fc1 = nn.Linear(width * height * 2, width * height * 8)
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = torch.flatten(out, 1)
out = self.fc1(out)
out = F.log_softmax(out, dim=1)
return out
class ResidualCNN(nn.Module):
def __init__(
self,
inplanes,
planes,
residual_layers,
vh_hidden_layer_size,
width,
height,
norm_layer=None,
):
super(ResidualCNN, self).__init__()
self.cuda_available = torch.cuda.is_available()
self.width = width
self.height = height
if norm_layer is None:
norm_layer = nn.BatchNorm2d
self.conv_layer = ConvolutionalLayer(inplanes, planes, norm_layer=norm_layer)
res_layers = []
for _ in range(residual_layers):
res_layers.append(ResidualLayer(planes, planes, norm_layer=norm_layer))
self.seq_res_layer = nn.Sequential(*res_layers)
self.value_head = ValueHead(
planes, vh_hidden_layer_size, width, height, norm_layer=norm_layer
)
self.policy_head = PolicyHead(planes, width, height, norm_layer=norm_layer)
if self.cuda_available:
self.cuda()
def forward(self, x):
out = self.conv_layer(x)
out = self.seq_res_layer(out)
value_out = self.value_head(out)
policy_out = self.policy_head(out)
return policy_out, value_out
def train_from_samples(self, train_samples):
"""
samples: (state, policy, value)
"""
optimizer = optim.Adam(self.parameters())
for epoch in range(config.train_epochs):
print(f"epoch {str(epoch+1)}")
self.train()
data_time = AverageMeter()
batch_time = AverageMeter()
policy_losses = AverageMeter()
value_losses = AverageMeter()
size = int(len(train_samples) / config.train_bs)
bar = Bar("Training NN", max=size)
batch_idx = 0
while batch_idx < size:
start = time.time()
sample_ids = np.random.randint(len(train_samples), size=config.train_bs)
states, policies, values = list(
zip(*[train_samples[i] for i in sample_ids])
)
states = self._states_to_tensor(states)
target_policies = torch.FloatTensor(np.array(policies))
target_values = torch.FloatTensor(np.array(values).astype(np.float64))
if self.cuda_available:
states = states.contiguous().cuda()
target_policies = target_policies.contiguous().cuda()
target_values = target_values.contiguous().cuda()
data_time.update(time.time() - start)
# get output
out_policies, out_values = self(states)
loss_policy = self.loss_policy(target_policies, out_policies)
loss_value = self.loss_value(target_values, out_values)
total_loss = loss_policy + loss_value
policy_losses.update(loss_policy.item(), states.size(0))
value_losses.update(loss_value.item(), states.size(0))
# gradient and SGD
optimizer.zero_grad()
total_loss.backward()
optimizer.step()
batch_time.update(time.time() - start)
batch_idx += 1
# plot progress
bar.suffix = "({batch}/{size}) Data: {data:.3f}s | Batch: {bt:.3f}s | Total: {total:} | ETA: {eta:} | Loss_policy: {lp:.4f} | Loss_value: {lv:.3f}".format(
batch=batch_idx,
size=size,
data=data_time.avg,
bt=batch_time.avg,
total=bar.elapsed_td,
eta=bar.eta_td,
lp=policy_losses.avg,
lv=value_losses.avg,
)
bar.next()
bar.finish()
def loss_policy(self, targets, outputs):
return -torch.sum(targets * outputs) / targets.size()[0]
def loss_value(self, targets, outputs):
return torch.sum((targets - outputs.view(-1)) ** 2) / targets.size()[0]
def _states_to_tensor(self, states):
t = torch.zeros([len(states), 3, self.width, self.height], dtype=torch.int32)
for i, state in enumerate(states):
for coord, cell in state.grid.items():
if cell.race == game.VAMPIRE:
t[i, 0, coord.x, coord.y] = cell.count
elif cell.race == game.WEREWOLF:
t[i, 1, coord.x, coord.y] = cell.count
elif cell.race == game.HUMAN:
t[i, 2, coord.x, coord.y] = cell.count
# t doesn't have information about the current player because the states are always
# canonical here, so vampires are the current players
return t.float()
def predict(self, state):
t = self._states_to_tensor([state])
if self.cuda_available:
t = t.contiguous().cuda()
self.eval()
with torch.no_grad():
p, v = self(t)
return torch.exp(p).data.cpu().numpy()[0], v.data.cpu().numpy()[0]
def save_checkpoint(self, folder="checkpoint", filename="checkpoint.pth.tar"):
filepath = os.path.join(folder, filename)
if not os.path.exists(folder):
print(f"Checkpoint directory doesn't exist. Creating directory {folder}")
os.mkdir(folder)
torch.save({"state_dict": self.state_dict(),}, filepath)
def load_checkpoint(self, folder="checkpoint", filename="checkpoint.pth.tar"):
filepath = os.path.join(folder, filename)
if not os.path.exists(filepath):
raise ValueError(f"No model in path {filepath}")
map_location = None if self.cuda_available else "cpu"
checkpoint = torch.load(filepath, map_location=map_location)
self.load_state_dict(checkpoint["state_dict"])
def vamperouge_net(config):
return ResidualCNN(
config.nn_inplanes,
config.nn_planes,
config.nn_residual_layers,
config.nn_vh_hidden_layer_size,
config.board_width,
config.board_height,
)