-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_network.py
236 lines (182 loc) · 5.98 KB
/
_network.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
import math
import torch
from torch import nn
from torch.nn import functional as F
NUM_CELLS = 16 # 4x4 board
NUM_CLASSES = 16 # EMPTY, 2, 4, 8, ..., up to 32768.
NUM_ACTIONS = 4
class CNNEncoder(nn.Module):
def __init__(
self,
out_features: int,
multiplier: int = 16,
) -> None:
super().__init__()
assert out_features >= 1 and out_features % 16 == 0, out_features
assert multiplier >= 1, multiplier
out_channels = out_features // 16
self.out_features = out_features
self._out_channels = out_channels
self._depthwise_full = nn.Conv1d(
NUM_CLASSES,
NUM_CLASSES * multiplier,
NUM_CLASSES,
groups=NUM_CLASSES,
)
self._pointwise_full = nn.Conv1d(
self._depthwise_full.out_channels,
out_channels * 4,
1,
)
self._depthwise_hori = nn.Conv2d(
NUM_CLASSES,
NUM_CLASSES * multiplier,
(1, 4),
groups=NUM_CLASSES,
)
self._pointwise_hori = nn.Conv2d(
self._depthwise_hori.out_channels,
out_channels,
1,
)
self._depthwise_vert = nn.Conv2d(
NUM_CLASSES,
NUM_CLASSES * multiplier,
(4, 1),
groups=NUM_CLASSES,
)
self._pointwise_vert = nn.Conv2d(
self._depthwise_vert.out_channels,
out_channels,
1,
)
self._conv_out = nn.Conv1d(
out_channels,
out_features,
12,
)
self.reset_parameters()
def reset_parameters(self):
sqrt2 = math.sqrt(2)
# nn.init.orthogonal_(self._depthwise_full.weight, sqrt2)
nn.init.zeros_(self._depthwise_full.bias)
# nn.init.orthogonal_(self._depthwise_hori.weight, sqrt2)
nn.init.zeros_(self._depthwise_hori.bias)
# nn.init.orthogonal_(self._depthwise_vert.weight, sqrt2)
nn.init.zeros_(self._depthwise_vert.bias)
nn.init.zeros_(self._conv_out.bias)
def forward(self, x: torch.LongTensor) -> torch.FloatTensor:
assert x.dtype == torch.long
assert x.ndim == 2 and x.shape[1] == 16, x.shape
# (N, 16) -> (N, 16, NUM_CLASSES)
x = F.one_hot(x, NUM_CLASSES)
x = x.float()
# -> (N, NUM_CLASSES, 16)
x = torch.permute(x, (0, 2, 1))
# -> (N, NUM_CLASSES * m, 1)
x_full = self._depthwise_full(x)
x_full = F.leaky_relu(x_full)
# -> (N, out * 4, 1)
x_full = self._pointwise_full(x_full)
x_full = F.leaky_relu(x_full)
# -> (N, NUM_CLASSES, 4, 4)
board = torch.reshape(x, (-1, NUM_CLASSES, 4, 4))
# -> (N, NUM_CLASSES * m, 4, 1)
x_hori = self._depthwise_hori(board)
x_hori = F.leaky_relu(x_hori)
# -> (N, out, 4, 1)
x_hori = self._pointwise_hori(x_hori)
x_hori = F.leaky_relu(x_hori)
# -> (N, NUM_CLASSES * m, 1, 4)
x_vert = self._depthwise_vert(board)
x_vert = F.leaky_relu(x_vert)
# -> (N, out, 1, 4)
x_vert = self._pointwise_vert(x_vert)
x_vert = F.leaky_relu(x_vert)
x = torch.cat(
(
torch.reshape(x_full, (-1, self._out_channels, 4)),
torch.flatten(x_hori, 2),
torch.flatten(x_vert, 2),
),
dim=2,
)
x = self._conv_out(x)
x = F.leaky_relu(x)
x = torch.flatten(x, 1)
return x.to(torch.float)
class CNNActorNetwork(nn.Module):
def __init__(
self,
in_features: int,
num_hidden: int,
num_hidden2: int,
) -> None:
super().__init__()
self._fc1 = nn.Linear(in_features, num_hidden)
self._fc2 = nn.Linear(self._fc1.out_features, num_hidden2)
# logits output
self._out = nn.Linear(self._fc2.out_features, NUM_ACTIONS)
self.reset_parameters()
def reset_parameters(self):
sqrt2 = math.sqrt(2)
nn.init.orthogonal_(self._fc1.weight, sqrt2)
nn.init.zeros_(self._fc1.bias)
nn.init.orthogonal_(self._fc2.weight, sqrt2)
nn.init.zeros_(self._fc2.bias)
nn.init.orthogonal_(self._out.weight, 0.01)
nn.init.zeros_(self._out.bias)
def forward(
self,
x: torch.FloatTensor,
valid_actions: torch.BoolTensor,
) -> torch.FloatTensor:
# -> (N, num_hidden)
x = self._fc1(x)
x = F.relu(x)
# -> (N, num_hidden2)
x = self._fc2(x)
x = F.relu(x)
# -> (N, 4)
logits = self._out(x)
# translation such that logits <= 0
# note that logit_max is a constant to the graph (detached)
logit_max, _ = torch.max(logits.detach(), dim=-1, keepdim=True)
logits = logits - logit_max
return logits
class CNNCriticNetwork(nn.Module):
def __init__(
self,
in_features: int,
num_hidden: int,
num_hidden2: int,
) -> None:
super().__init__()
self._fc1 = nn.Linear(in_features, num_hidden)
self._fc2 = nn.Linear(self._fc1.out_features, num_hidden2)
# value output
self._out = nn.Linear(self._fc2.out_features, 1)
self.reset_parameters()
def reset_parameters(self):
sqrt2 = math.sqrt(2)
nn.init.orthogonal_(self._fc1.weight, sqrt2)
nn.init.zeros_(self._fc1.bias)
nn.init.orthogonal_(self._fc2.weight, sqrt2)
nn.init.zeros_(self._fc2.bias)
nn.init.orthogonal_(self._out.weight, 1)
nn.init.zeros_(self._out.bias)
def forward(
self,
x: torch.FloatTensor,
valid_actions: torch.BoolTensor,
) -> torch.FloatTensor:
# -> (N, num_hidden)
x = self._fc1(x)
x = F.relu(x)
# -> (N, num_hidden2)
x = self._fc2(x)
x = F.relu(x)
# -> (N, 1)
x = self._out(x)
x = torch.squeeze(x, dim=-1)
return x