-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmodels.py
More file actions
57 lines (51 loc) · 1.82 KB
/
models.py
File metadata and controls
57 lines (51 loc) · 1.82 KB
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
import torch.nn as nn
import torch.nn.functional as F
class ResNet(nn.Module):
def __init__(self, game, num_resBlocks, num_hidden, device):
super().__init__()
self.device = device
self.startBlock = nn.Sequential(
nn.Conv2d(3, num_hidden, kernel_size=3, padding=1),
nn.BatchNorm2d(num_hidden),
nn.ReLU()
)
self.backBone = nn.ModuleList(
[ResBlock(num_hidden) for i in range(num_resBlocks)]
)
self.policyHead = nn.Sequential(
nn.Conv2d(num_hidden, 32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Flatten(),
nn.Linear(32 * game.row_count * game.column_count, game.action_size)
)
self.valueHead = nn.Sequential(
nn.Conv2d(num_hidden, 3, kernel_size=3, padding=1),
nn.BatchNorm2d(3),
nn.ReLU(),
nn.Flatten(),
nn.Linear(3 * game.row_count * game.column_count, 1),
nn.Tanh()
)
self.to(device)
def forward(self, x):
x = self.startBlock(x)
for resBlock in self.backBone:
x = resBlock(x)
policy = self.policyHead(x)
value = self.valueHead(x)
return policy, value
class ResBlock(nn.Module):
def __init__(self, num_hidden):
super().__init__()
self.conv1 = nn.Conv2d(num_hidden, num_hidden, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm2d(num_hidden)
self.conv2 = nn.Conv2d(num_hidden, num_hidden, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm2d(num_hidden)
def forward(self, x):
residual = x
x = F.relu(self.bn1(self.conv1(x)))
x = self.bn2(self.conv2(x))
x += residual
x = F.relu(x)
return x