forked from werner-duvaud/muzero-general
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
696 lines (616 loc) · 24.2 KB
/
models.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
import math
from abc import ABC, abstractmethod
import torch
class MuZeroNetwork:
def __new__(cls, config):
if config.network == "fullyconnected":
return MuZeroFullyConnectedNetwork(
config.observation_shape,
config.stacked_observations,
len(config.action_space),
config.encoding_size,
config.fc_reward_layers,
config.fc_value_layers,
config.fc_policy_layers,
config.fc_representation_layers,
config.fc_dynamics_layers,
config.support_size,
)
elif config.network == "resnet":
return MuZeroResidualNetwork(
config.observation_shape,
config.stacked_observations,
len(config.action_space),
config.blocks,
config.channels,
config.reduced_channels_reward,
config.reduced_channels_value,
config.reduced_channels_policy,
config.resnet_fc_reward_layers,
config.resnet_fc_value_layers,
config.resnet_fc_policy_layers,
config.support_size,
config.downsample,
)
else:
raise NotImplementedError(
'The network parameter should be "fullyconnected" or "resnet".'
)
def dict_to_cpu(dictionary):
cpu_dict = {}
for key, value in dictionary.items():
if isinstance(value, torch.Tensor):
cpu_dict[key] = value.cpu()
elif isinstance(value, dict):
cpu_dict[key] = dict_to_cpu(value)
else:
cpu_dict[key] = value
return cpu_dict
class AbstractNetwork(ABC, torch.nn.Module):
def __init__(self):
super().__init__()
pass
@abstractmethod
def initial_inference(self, observation):
pass
@abstractmethod
def recurrent_inference(self, encoded_state, action):
pass
def get_weights(self):
return dict_to_cpu(self.state_dict())
def set_weights(self, weights):
self.load_state_dict(weights)
##################################
######## Fully Connected #########
class MuZeroFullyConnectedNetwork(AbstractNetwork):
def __init__(
self,
observation_shape,
stacked_observations,
action_space_size,
encoding_size,
fc_reward_layers,
fc_value_layers,
fc_policy_layers,
fc_representation_layers,
fc_dynamics_layers,
support_size,
):
super().__init__()
self.action_space_size = action_space_size
self.full_support_size = 2 * support_size + 1
# support_size 表示的应该是一个选择的范围【-support_size, support_size】.最后+1是因为range最后不包含最后的数
self.representation_network = torch.nn.DataParallel(
mlp(
observation_shape[0]
* observation_shape[1]
* observation_shape[2]
* (stacked_observations + 1)
+ stacked_observations * observation_shape[1] * observation_shape[2],
fc_representation_layers,
encoding_size,
)
)
#dynamics的输入是encoding_size+action_space_size
self.dynamics_encoded_state_network = torch.nn.DataParallel(
mlp(
encoding_size + self.action_space_size,
fc_dynamics_layers,
encoding_size,
)
)
self.dynamics_reward_network = torch.nn.DataParallel(
mlp(encoding_size, fc_reward_layers, self.full_support_size) #最后的输出为full_support_size,因为范围是[-support_size, support_size]
)
self.prediction_policy_network = torch.nn.DataParallel(
mlp(encoding_size, fc_policy_layers, self.action_space_size) #输出action的概率
)
self.prediction_value_network = torch.nn.DataParallel(
mlp(encoding_size, fc_value_layers, self.full_support_size) #最后的输出为full_support_size,因为范围是[-support_size, support_size]
)
def prediction(self, encoded_state):
policy_logits = self.prediction_policy_network(encoded_state)
value = self.prediction_value_network(encoded_state)
return policy_logits, value
def representation(self, observation):
encoded_state = self.representation_network(
observation.view(observation.shape[0], -1)
)
# 正则化
# Scale encoded state between [0, 1] (See appendix paper Training)
min_encoded_state = encoded_state.min(1, keepdim=True)[0]
max_encoded_state = encoded_state.max(1, keepdim=True)[0]
scale_encoded_state = max_encoded_state - min_encoded_state
scale_encoded_state[scale_encoded_state < 1e-5] += 1e-5 # 防止为0,造成NAN
encoded_state_normalized = (
encoded_state - min_encoded_state
) / scale_encoded_state
return encoded_state_normalized
# dynamic同representation的唯一不同就是前者需要将encoded_state和action合并在一起作为输入,而representation不需要绑定action
def dynamics(self, encoded_state, action):
# Stack encoded_state with a game specific one hot encoded action (See paper appendix Network Architecture)
action_one_hot = (
torch.zeros((action.shape[0], self.action_space_size))
.to(action.device)
.float()
)
action_one_hot.scatter_(1, action.long(), 1.0) #将action的位置赋值为1
x = torch.cat((encoded_state, action_one_hot), dim=1)
next_encoded_state = self.dynamics_encoded_state_network(x)
reward = self.dynamics_reward_network(next_encoded_state)
# 正则化
# Scale encoded state between [0, 1] (See paper appendix Training)
min_next_encoded_state = next_encoded_state.min(1, keepdim=True)[0]
max_next_encoded_state = next_encoded_state.max(1, keepdim=True)[0]
scale_next_encoded_state = max_next_encoded_state - min_next_encoded_state
scale_next_encoded_state[scale_next_encoded_state < 1e-5] += 1e-5 # 防止为0,造成NAN
next_encoded_state_normalized = (
next_encoded_state - min_next_encoded_state
) / scale_next_encoded_state
return next_encoded_state_normalized, reward
def initial_inference(self, observation):
encoded_state = self.representation(observation)
policy_logits, value = self.prediction(encoded_state)
# reward equal to 0 for consistency 一致性奖励等于 0
reward = torch.log(
(
torch.zeros(1, self.full_support_size)
.scatter(1, torch.tensor([[self.full_support_size // 2]]).long(), 1.0)
.repeat(len(observation), 1)
.to(observation.device)
)
)
# reward的样子为[[0,0,...,0,1,0,...,0,0],...]。即中间值为1,其余全为0,然后重复于observation行数相同的次数
return (
value,
reward,
policy_logits,
encoded_state,
)
def recurrent_inference(self, encoded_state, action):
next_encoded_state, reward = self.dynamics(encoded_state, action)
policy_logits, value = self.prediction(next_encoded_state)
return value, reward, policy_logits, next_encoded_state
###### End Fully Connected #######
##################################
##################################
############# ResNet #############
def conv3x3(in_channels, out_channels, stride=1):
return torch.nn.Conv2d(
in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False
)
# Residual block
class ResidualBlock(torch.nn.Module):
def __init__(self, num_channels, stride=1):
super().__init__()
self.conv1 = conv3x3(num_channels, num_channels, stride)
self.bn1 = torch.nn.BatchNorm2d(num_channels)
self.conv2 = conv3x3(num_channels, num_channels)
self.bn2 = torch.nn.BatchNorm2d(num_channels)
def forward(self, x):
out = self.conv1(x)
out = self.bn1(out)
out = torch.nn.functional.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out += x
out = torch.nn.functional.relu(out)
return out
# Downsample observations before representation network (See paper appendix Network Architecture)
class DownSample(torch.nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.conv1 = torch.nn.Conv2d(
in_channels,
out_channels // 2,
kernel_size=3,
stride=2,
padding=1,
bias=False,
)
self.resblocks1 = torch.nn.ModuleList(
[ResidualBlock(out_channels // 2) for _ in range(2)]
)
self.conv2 = torch.nn.Conv2d(
out_channels // 2,
out_channels,
kernel_size=3,
stride=2,
padding=1,
bias=False,
)
self.resblocks2 = torch.nn.ModuleList(
[ResidualBlock(out_channels) for _ in range(3)]
)
self.pooling1 = torch.nn.AvgPool2d(kernel_size=3, stride=2, padding=1)
self.resblocks3 = torch.nn.ModuleList(
[ResidualBlock(out_channels) for _ in range(3)]
)
self.pooling2 = torch.nn.AvgPool2d(kernel_size=3, stride=2, padding=1)
def forward(self, x):
x = self.conv1(x)
for block in self.resblocks1:
x = block(x)
x = self.conv2(x)
for block in self.resblocks2:
x = block(x)
x = self.pooling1(x)
for block in self.resblocks3:
x = block(x)
x = self.pooling2(x)
return x
class DownsampleCNN(torch.nn.Module):
def __init__(self, in_channels, out_channels, h_w):
super().__init__()
mid_channels = (in_channels + out_channels) // 2
self.features = torch.nn.Sequential(
torch.nn.Conv2d(
in_channels, mid_channels, kernel_size=h_w[0] * 2, stride=4, padding=2
),
torch.nn.ReLU(inplace=True),
torch.nn.MaxPool2d(kernel_size=3, stride=2),
torch.nn.Conv2d(mid_channels, out_channels, kernel_size=5, padding=2),
torch.nn.ReLU(inplace=True),
torch.nn.MaxPool2d(kernel_size=3, stride=2),
)
self.avgpool = torch.nn.AdaptiveAvgPool2d(h_w)
def forward(self, x):
x = self.features(x)
x = self.avgpool(x)
return x
class RepresentationNetwork(torch.nn.Module):
def __init__(
self,
observation_shape,
stacked_observations,
num_blocks,
num_channels,
downsample,
):
super().__init__()
self.downsample = downsample
if self.downsample:
if self.downsample == "resnet":
self.downsample_net = DownSample(
observation_shape[0] * (stacked_observations + 1)
+ stacked_observations,
num_channels,
)
elif self.downsample == "CNN":
self.downsample_net = DownsampleCNN(
observation_shape[0] * (stacked_observations + 1)
+ stacked_observations,
num_channels,
(
math.ceil(observation_shape[1] / 16),
math.ceil(observation_shape[2] / 16),
),
)
else:
raise NotImplementedError('downsample should be "resnet" or "CNN".')
self.conv = conv3x3(
observation_shape[0] * (stacked_observations + 1) + stacked_observations,
num_channels,
)
self.bn = torch.nn.BatchNorm2d(num_channels)
self.resblocks = torch.nn.ModuleList(
[ResidualBlock(num_channels) for _ in range(num_blocks)]
)
def forward(self, x):
if self.downsample:
x = self.downsample_net(x)
else:
x = self.conv(x)
x = self.bn(x)
x = torch.nn.functional.relu(x)
for block in self.resblocks:
x = block(x)
return x
class DynamicsNetwork(torch.nn.Module):
def __init__(
self,
num_blocks,
num_channels,
reduced_channels_reward,
fc_reward_layers,
full_support_size,
block_output_size_reward,
):
super().__init__()
self.conv = conv3x3(num_channels, num_channels - 1)
self.bn = torch.nn.BatchNorm2d(num_channels - 1)
self.resblocks = torch.nn.ModuleList(
[ResidualBlock(num_channels - 1) for _ in range(num_blocks)]
)
self.conv1x1_reward = torch.nn.Conv2d(
num_channels - 1, reduced_channels_reward, 1
)
self.block_output_size_reward = block_output_size_reward
self.fc = mlp(
self.block_output_size_reward,
fc_reward_layers,
full_support_size,
)
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = torch.nn.functional.relu(x)
for block in self.resblocks:
x = block(x)
state = x
x = self.conv1x1_reward(x)
x = x.view(-1, self.block_output_size_reward)
reward = self.fc(x)
return state, reward
class PredictionNetwork(torch.nn.Module):
def __init__(
self,
action_space_size,
num_blocks,
num_channels,
reduced_channels_value,
reduced_channels_policy,
fc_value_layers,
fc_policy_layers,
full_support_size,
block_output_size_value,
block_output_size_policy,
):
super().__init__()
self.resblocks = torch.nn.ModuleList(
[ResidualBlock(num_channels) for _ in range(num_blocks)]
)
self.conv1x1_value = torch.nn.Conv2d(num_channels, reduced_channels_value, 1)
self.conv1x1_policy = torch.nn.Conv2d(num_channels, reduced_channels_policy, 1)
self.block_output_size_value = block_output_size_value
self.block_output_size_policy = block_output_size_policy
self.fc_value = mlp(
self.block_output_size_value, fc_value_layers, full_support_size
)
self.fc_policy = mlp(
self.block_output_size_policy,
fc_policy_layers,
action_space_size,
)
def forward(self, x):
for block in self.resblocks:
x = block(x)
value = self.conv1x1_value(x)
policy = self.conv1x1_policy(x)
value = value.view(-1, self.block_output_size_value)
policy = policy.view(-1, self.block_output_size_policy)
value = self.fc_value(value)
policy = self.fc_policy(policy)
return policy, value
class MuZeroResidualNetwork(AbstractNetwork):
def __init__(
self,
observation_shape,
stacked_observations,
action_space_size,
num_blocks,
num_channels,
reduced_channels_reward,
reduced_channels_value,
reduced_channels_policy,
fc_reward_layers,
fc_value_layers,
fc_policy_layers,
support_size,
downsample,
):
super().__init__()
self.action_space_size = action_space_size
self.full_support_size = 2 * support_size + 1
block_output_size_reward = (
(
reduced_channels_reward
* math.ceil(observation_shape[1] / 16)
* math.ceil(observation_shape[2] / 16)
)
if downsample
else (reduced_channels_reward * observation_shape[1] * observation_shape[2])
)
block_output_size_value = (
(
reduced_channels_value
* math.ceil(observation_shape[1] / 16)
* math.ceil(observation_shape[2] / 16)
)
if downsample
else (reduced_channels_value * observation_shape[1] * observation_shape[2])
)
block_output_size_policy = (
(
reduced_channels_policy
* math.ceil(observation_shape[1] / 16)
* math.ceil(observation_shape[2] / 16)
)
if downsample
else (reduced_channels_policy * observation_shape[1] * observation_shape[2])
)
self.representation_network = torch.nn.DataParallel(
RepresentationNetwork(
observation_shape,
stacked_observations,
num_blocks,
num_channels,
downsample,
)
)
self.dynamics_network = torch.nn.DataParallel(
DynamicsNetwork(
num_blocks,
num_channels + 1,
reduced_channels_reward,
fc_reward_layers,
self.full_support_size,
block_output_size_reward,
)
)
self.prediction_network = torch.nn.DataParallel(
PredictionNetwork(
action_space_size,
num_blocks,
num_channels,
reduced_channels_value,
reduced_channels_policy,
fc_value_layers,
fc_policy_layers,
self.full_support_size,
block_output_size_value,
block_output_size_policy,
)
)
def prediction(self, encoded_state):
policy, value = self.prediction_network(encoded_state)
return policy, value
def representation(self, observation):
encoded_state = self.representation_network(observation)
# Scale encoded state between [0, 1] (See appendix paper Training)
min_encoded_state = (
encoded_state.view(
-1,
encoded_state.shape[1],
encoded_state.shape[2] * encoded_state.shape[3],
)
.min(2, keepdim=True)[0]
.unsqueeze(-1)
)
max_encoded_state = (
encoded_state.view(
-1,
encoded_state.shape[1],
encoded_state.shape[2] * encoded_state.shape[3],
)
.max(2, keepdim=True)[0]
.unsqueeze(-1)
)
scale_encoded_state = max_encoded_state - min_encoded_state
scale_encoded_state[scale_encoded_state < 1e-5] += 1e-5
encoded_state_normalized = (
encoded_state - min_encoded_state
) / scale_encoded_state
return encoded_state_normalized
def dynamics(self, encoded_state, action):
# Stack encoded_state with a game specific one hot encoded action (See paper appendix Network Architecture)
action_one_hot = (
torch.ones(
(
encoded_state.shape[0],
1,
encoded_state.shape[2],
encoded_state.shape[3],
)
)
.to(action.device)
.float()
)
action_one_hot = (
action[:, :, None, None] * action_one_hot / self.action_space_size
)
x = torch.cat((encoded_state, action_one_hot), dim=1)
next_encoded_state, reward = self.dynamics_network(x)
# Scale encoded state between [0, 1] (See paper appendix Training)
min_next_encoded_state = (
next_encoded_state.view(
-1,
next_encoded_state.shape[1],
next_encoded_state.shape[2] * next_encoded_state.shape[3],
)
.min(2, keepdim=True)[0]
.unsqueeze(-1)
)
max_next_encoded_state = (
next_encoded_state.view(
-1,
next_encoded_state.shape[1],
next_encoded_state.shape[2] * next_encoded_state.shape[3],
)
.max(2, keepdim=True)[0]
.unsqueeze(-1)
)
scale_next_encoded_state = max_next_encoded_state - min_next_encoded_state
scale_next_encoded_state[scale_next_encoded_state < 1e-5] += 1e-5
next_encoded_state_normalized = (
next_encoded_state - min_next_encoded_state
) / scale_next_encoded_state
return next_encoded_state_normalized, reward
def initial_inference(self, observation):
encoded_state = self.representation(observation)
policy_logits, value = self.prediction(encoded_state)
# reward equal to 0 for consistency
reward = torch.log(
(
torch.zeros(1, self.full_support_size)
.scatter(1, torch.tensor([[self.full_support_size // 2]]).long(), 1.0) # 将support_size位置设为1
.repeat(len(observation), 1) # 根据observation的长度复制,保证reward的维度于observation的一致,即之前的observation也赋值
.to(observation.device)
)
)
return (
value,
reward,
policy_logits,
encoded_state,
)
def recurrent_inference(self, encoded_state, action):
next_encoded_state, reward = self.dynamics(encoded_state, action)
policy_logits, value = self.prediction(next_encoded_state)
return value, reward, policy_logits, next_encoded_state
########### End ResNet ###########
##################################
def mlp(
input_size,
layer_sizes,
output_size,
output_activation=torch.nn.Identity,
activation=torch.nn.ELU,
):
sizes = [input_size] + layer_sizes + [output_size]
layers = []
for i in range(len(sizes) - 1):
act = activation if i < len(sizes) - 2 else output_activation #激活函数,最后一层是output_activation,其余的都一样
layers += [torch.nn.Linear(sizes[i], sizes[i + 1]), act()]
return torch.nn.Sequential(*layers)
def support_to_scalar(logits, support_size): # logits 是 value的对数值,support_size是转换后的范围。
"""
Transform a categorical representation to a scalar
See paper appendix Network Architecture
"""
# Decode to a scalar
probabilities = torch.softmax(logits, dim=1) # softmax在指定的向量和为1,softmax扩大大的,缩小下的,shape为[stacked_size, fully_support_size]
support = (
torch.tensor([x for x in range(-support_size, support_size + 1)]) # 范围是-support_size, support_szie。因为support_size+1
.expand(probabilities.shape)
.float()
.to(device=probabilities.device)
) # shape 为【stacked_size, fully_support_size】,
x = torch.sum(support * probabilities, dim=1, keepdim=True) # 输出为【1,fully_support_size】,因为dim=1,另外keep_dim=True,所有是【1,fully_support_size】而不是【fully_support_size]
# Invert the scaling (defined in https://arxiv.org/abs/1805.11593)
x = torch.sign(x) * ( # sign函数为分段函数, x小于0为-1,大于0为1,0为0。主要是获取x的符号
((torch.sqrt(1 + 4 * 0.001 * (torch.abs(x) + 1 + 0.001)) - 1) / (2 * 0.001)) # (sqrt(1+0.04*(|x|+1.001))-1)/0.002
** 2
- 1
)
return x
def scalar_to_support(x, support_size):
"""
Transform a scalar to a categorical representation with (2 * support_size + 1) categories
See paper appendix Network Architecture
"""
# Reduce the scale (defined in https://arxiv.org/abs/1805.11593)
x = torch.sign(x) * (torch.sqrt(torch.abs(x) + 1) - 1) + 0.001 * x
# Encode on a vector
x = torch.clamp(x, -support_size, support_size) # 裁剪x的范围,使x的范围定为[-support_size, support_size]
floor = x.floor() # floor向下取整,类似的,ceil为向上取整
prob = x - floor # 减去整数,保留小数部分(因为在support_to_scala部分是index位置乘上概率)
logits = torch.zeros(x.shape[0], x.shape[1], 2 * support_size + 1).to(x.device)
logits.scatter_(
2, (floor + support_size).long().unsqueeze(-1), (1 - prob).unsqueeze(-1)
)
indexes = floor + support_size + 1
prob = prob.masked_fill_(2 * support_size < indexes, 0.0)
indexes = indexes.masked_fill_(2 * support_size < indexes, 0.0)
logits.scatter_(2, indexes.long().unsqueeze(-1), prob.unsqueeze(-1))
return logits