-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
134 lines (114 loc) · 4.16 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
import torch.nn as nn
import torch
import torch.nn.functional as F
class CNN_2048_MODEL(nn.Module):
def __init__(self, c_in_1, c_in_2, c_out_1, c_out_2):
super(CNN_2048_MODEL, self).__init__()
self.__c_in_1 = c_in_1
self.__c_in_2 = c_in_2
self.__c_out_1 = c_out_1
self.__c_out_2 = c_out_2
self.__expanded_size = (
2 * 4 * c_out_2 * 2 + 3 * 3 * c_out_2 * 2 + 4 * 3 * c_out_1 * 2
)
self.__dense_value_1 = nn.Linear(self.__expanded_size, 256)
self.__dense_value_2 = nn.Linear(256, 1)
self.__dense_advantage_1 = nn.Linear(self.__expanded_size, 256)
self.__dense_advantage_2 = nn.Linear(256, 4)
self.__cnn_1 = nn.Conv2d(
c_in_1,
c_out_1,
kernel_size=(1, 2),
stride=(1, 1),
padding=(0, 0),
dilation=(1, 1),
)
self.__cnn_1_2 = nn.Conv2d(
c_out_1,
c_out_2,
kernel_size=(1, 2),
stride=(1, 1),
padding=(0, 0),
dilation=(1, 1),
)
self.__cnn_2 = nn.Conv2d(
c_in_2,
c_out_2,
kernel_size=(2, 1),
stride=(1, 1),
padding=(0, 0),
dilation=(1, 1),
)
self.__cnn_2_2 = nn.Conv2d(
c_out_1,
c_out_2,
kernel_size=(2, 1),
stride=(1, 1),
padding=(0, 0),
dilation=(1, 1),
)
def forward(self, features, batch_size, size_board):
features_view = features.view(batch_size, 16, size_board, size_board)
conv1_output = F.elu(self.__cnn_1(features_view))
conv2_output = F.elu(self.__cnn_2(features_view))
conv1_2_1_output = F.elu(self.__cnn_1_2(conv1_output))
conv1_2_2_output = F.elu(self.__cnn_1_2(conv2_output))
conv2_2_1_output = F.elu(self.__cnn_2_2(conv1_output))
conv2_2_2_output = F.elu(self.__cnn_2_2(conv2_output))
conv1_output_shape = list(conv1_output.shape)
conv2_output_shape = list(conv2_output.shape)
conv1_2_1_output_shape = list(conv1_2_1_output.shape)
conv1_2_2_output_shape = list(conv1_2_2_output.shape)
conv2_2_1_output_shape = list(conv2_2_1_output.shape)
conv2_2_2_output_shape = list(conv2_2_2_output.shape)
hidden1 = conv1_output.view(
batch_size,
(conv1_output_shape[1] * conv1_output_shape[2] * conv1_output_shape[3]),
)
hidden2 = conv2_output.view(
batch_size,
(conv2_output_shape[1] * conv2_output_shape[2] * conv2_output_shape[3]),
)
hidden1_2_1 = conv1_2_1_output.view(
batch_size,
(
conv1_2_1_output_shape[1]
* conv1_2_1_output_shape[2]
* conv1_2_1_output_shape[3]
),
)
hidden1_2_2 = conv1_2_2_output.view(
batch_size,
(
conv1_2_2_output_shape[1]
* conv1_2_2_output_shape[2]
* conv1_2_2_output_shape[3]
),
)
hidden2_2_1 = conv2_2_1_output.view(
batch_size,
(
conv2_2_1_output_shape[1]
* conv2_2_1_output_shape[2]
* conv2_2_1_output_shape[3]
),
)
hidden2_2_2 = conv2_2_2_output.view(
batch_size,
(
conv2_2_2_output_shape[1]
* conv2_2_2_output_shape[2]
* conv2_2_2_output_shape[3]
),
)
hidden = torch.cat(
(hidden1, hidden2, hidden1_2_1, hidden1_2_2, hidden2_2_1, hidden2_2_2), 1
)
hidden_value_1 = F.elu(self.__dense_value_1(hidden))
hidden_value_2 = self.__dense_value_2(hidden_value_1)
advantage_action_1 = F.elu(self.__dense_advantage_1(hidden))
advantage_action_2 = self.__dense_advantage_2(advantage_action_1)
# Q(s,a) = V(s) + (A(s,a) - 1/|A| * sum A(s,a'))
reduced_mean = torch.mean(advantage_action_2, dim=1, keepdim=True)
output = hidden_value_2 + (advantage_action_2 - reduced_mean)
return output