-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchess_neural_net.py
More file actions
83 lines (69 loc) · 2.44 KB
/
chess_neural_net.py
File metadata and controls
83 lines (69 loc) · 2.44 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
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
import torch
from torch import nn
class ChessNet(nn.Module):
def __init__(self, num_res_blocks=10) -> None:
super().__init__()
torch.backends.cudnn.benchmark = True
self.conv1 = nn.Conv2d(12, 64, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm2d(64)
self.lrelu = nn.LeakyReLU()
self.conv2 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm2d(128)
self.backbone = nn.ModuleList([ResidualBlock(128) for i in range(num_res_blocks)])
self.policy_head = nn.Sequential(
nn.Conv2d(128, 256, kernel_size=3, padding=1),
nn.BatchNorm2d(256),
nn.LeakyReLU(),
nn.Conv2d(256, 512, kernel_size=3, padding=1),
nn.BatchNorm2d(512),
nn.LeakyReLU(),
nn.Flatten(),
#nn.Linear(32_768, 4096),
nn.Linear(32_768, 1024),
#nn.Dropout(0.3),
#nn.LeakyReLU(),
#nn.Linear(4096, 1024),
nn.Dropout(0.3),
nn.LeakyReLU(),
nn.Linear(1024, 1972)
)
self.value_head = nn.Sequential(
nn.Conv2d(128, 32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.LeakyReLU(),
nn.Flatten(),
#nn.Linear(2048, 512),
#nn.Dropout(0.5),
#nn.LeakyReLU(),
#nn.Linear(512, 1),
nn.Linear(2048, 1),
nn.Tanh()
)
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.to(self.device)
def forward(self, x):
x = self.bn1(self.conv1(x))
x = self.lrelu(x)
x = self.bn2(self.conv2(x))
x = self.lrelu(x)
for block in self.backbone:
x = block(x)
policy_logits = self.policy_head(x)
value = self.value_head(x)
return policy_logits, value
class ResidualBlock(nn.Module):
def __init__(self, input) -> None:
super().__init__()
self.conv1 = nn.Conv2d(input, input, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm2d(input)
self.lrelu = nn.LeakyReLU()
self.conv2 = nn.Conv2d(input, input, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm2d(input)
def forward(self, x):
residual = x
x = self.bn1(self.conv1(x))
x = self.lrelu(x)
x = self.bn2(self.conv2(x))
x += residual
x = self.lrelu(x)
return x