-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpolicy_value_network.py
165 lines (149 loc) · 6.67 KB
/
policy_value_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
from typing import Tuple, Union
import torch
import torch.nn as nn
from utttpy.selfplay.policy_value_loss import policy_value_loss_function
class PolicyValueNetwork(nn.Module):
def __init__(self, in_channels: int = 4, num_planes: int = 256, onnx_export: bool = False):
super(PolicyValueNetwork, self).__init__()
# encoder
self.conv_block_1 = nn.Sequential(
nn.Conv2d(in_channels, num_planes // 2, kernel_size=3, stride=3, padding=0, bias=True),
nn.ELU(),
)
self.conv_block_2 = nn.Sequential(
nn.Conv2d(num_planes // 2, num_planes // 2, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(num_planes // 2),
nn.ELU(),
nn.Conv2d(num_planes // 2, num_planes, kernel_size=1, stride=1, padding=0, bias=True),
nn.ReLU(),
)
self.conv_block_3a = nn.Sequential(
nn.Conv2d(num_planes, num_planes * 2, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(num_planes * 2),
nn.ELU(),
nn.Conv2d(num_planes * 2, num_planes * 2, kernel_size=3, stride=1, padding=0, groups=num_planes * 2, bias=False),
nn.BatchNorm2d(num_planes * 2),
nn.ELU(),
nn.ConvTranspose2d(num_planes * 2, num_planes, kernel_size=3, stride=1, padding=0, bias=True),
)
self.conv_block_3b = nn.Sequential(
nn.Conv2d(num_planes, num_planes * 2, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(num_planes * 2),
nn.ELU(),
nn.Conv2d(num_planes * 2, num_planes * 2, kernel_size=3, stride=1, padding=0, groups=num_planes * 2, bias=False),
nn.BatchNorm2d(num_planes * 2),
nn.ELU(),
nn.ConvTranspose2d(num_planes * 2, num_planes, kernel_size=3, stride=1, padding=0, bias=True),
)
self.conv_block_3c = nn.Sequential(
nn.Conv2d(num_planes, num_planes * 2, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(num_planes * 2),
nn.ELU(),
nn.Conv2d(num_planes * 2, num_planes * 2, kernel_size=3, stride=1, padding=0, groups=num_planes * 2, bias=False),
nn.BatchNorm2d(num_planes * 2),
nn.ELU(),
nn.ConvTranspose2d(num_planes * 2, num_planes, kernel_size=3, stride=1, padding=0, bias=True),
)
# policy head
self.policy_head_conv = nn.Sequential(
nn.Conv2d(5 * num_planes // 2, num_planes, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(num_planes),
nn.ELU(),
)
self.policy_head_upsampling = nn.Sequential(
nn.ConvTranspose2d(num_planes, num_planes, kernel_size=3, stride=3, padding=0, bias=True),
nn.ELU(),
)
self.policy_head_logits = nn.Sequential(
nn.Conv2d(in_channels + num_planes, 128, kernel_size=1, stride=1, padding=0, bias=True),
nn.ELU(),
nn.Conv2d(128, 1, kernel_size=1, stride=1, padding=0, bias=True),
)
if not onnx_export:
self.policy_head_values = nn.Sequential(
nn.Conv2d(in_channels + num_planes, 128, kernel_size=1, stride=1, padding=0, bias=True),
nn.ELU(),
nn.Conv2d(128, 1, kernel_size=1, stride=1, padding=0, bias=True),
nn.Tanh(),
)
# value head
self.value_head_conv1 = nn.Sequential(
nn.Conv2d(num_planes, num_planes, kernel_size=1, stride=1, padding=0, bias=True),
nn.Conv2d(num_planes, num_planes, kernel_size=3, stride=1, padding=0, groups=num_planes, bias=False),
nn.BatchNorm2d(num_planes),
nn.ELU(),
)
self.value_head_conv2 = nn.Sequential(
nn.Conv2d(num_planes, num_planes, kernel_size=1, stride=1, padding=0, bias=True),
nn.Conv2d(num_planes, num_planes, kernel_size=3, stride=1, padding=0, groups=num_planes, bias=False),
nn.BatchNorm2d(num_planes),
nn.ELU(),
)
self.value_head_fc = nn.Sequential(
nn.Linear(2 * num_planes, 128, bias=True),
nn.ELU(),
nn.Linear(128, 1, bias=True),
nn.Tanh(),
)
self.in_channels = in_channels
self.num_planes = num_planes
self.onnx_export = onnx_export
def forward(
self, x: torch.Tensor
) -> Union[
Tuple[torch.Tensor, torch.Tensor, torch.Tensor],
Tuple[torch.Tensor, torch.Tensor],
]:
batch_size = x.size(0)
x1 = self.conv_block_1(x)
x2 = self.conv_block_2(x1)
x3 = torch.relu(self.conv_block_3a(x2) + x2)
x3 = torch.relu(self.conv_block_3b(x3) + x3)
x3 = torch.relu(self.conv_block_3c(x3) + x3)
p = torch.cat([x1, x2, x3], dim=1)
p = self.policy_head_conv(p)
p = self.policy_head_upsampling(p)
p = torch.cat([x, p], dim=1)
pl = self.policy_head_logits(p)
pl = pl.clamp(-32, 32)
pl = pl.squeeze(1)
if not self.onnx_export:
av = self.policy_head_values(p)
av = av.squeeze(1)
v1 = self.value_head_conv1(x2).view(batch_size, self.num_planes)
v2 = self.value_head_conv2(x3).view(batch_size, self.num_planes)
v = torch.cat([v1, v2], dim=1)
sv = self.value_head_fc(v)
sv = sv.squeeze(1)
if not self.onnx_export:
# policy_logits, action_values, state_value
return pl, av, sv
else:
# policy_logits, state_value
return pl, sv
def forward_loss(
self,
train_batch: dict,
policy_loss_type: str = "cross_entropy",
policy_loss_weight: float = 1.0,
action_values_loss_weight: float = 1.0,
state_value_loss_weight: float = 1.0,
) -> torch.Tensor:
policy_logits, action_values, state_value = self(train_batch["inputs"])
targets = train_batch["targets"]
return policy_value_loss_function(
policy_logits_predictions=policy_logits,
action_values_predictions=action_values,
state_value_predictions=state_value,
policy_proba_targets=targets["policy_targets"],
action_values_targets=targets["action_values"],
state_value_targets=targets["state_value"],
policy_mask=targets["policy_mask"],
policy_loss_type=policy_loss_type,
policy_loss_weight=policy_loss_weight,
action_values_loss_weight=action_values_loss_weight,
state_value_loss_weight=state_value_loss_weight,
)
@property
def device(self) -> torch.device:
return next(self.parameters()).device