forked from dxyang/DQN_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
63 lines (44 loc) · 2.08 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
import torch
import torch.nn as nn
import numpy as np
class DQN(nn.Module):
def __init__(self, in_channels, num_actions):
super(DQN, self).__init__()
self.conv1 = nn.Conv2d(in_channels=in_channels, out_channels=32, kernel_size=8, stride=4)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=4, stride=2)
self.conv3 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1)
self.fc1 = nn.Linear(in_features=7*7*64, out_features=512)
self.fc2 = nn.Linear(in_features=512, out_features=num_actions)
self.relu = nn.ReLU()
def forward(self, x):
x = self.relu(self.conv1(x))
x = self.relu(self.conv2(x))
x = self.relu(self.conv3(x))
x = x.view(x.size(0), -1)
x = self.relu(self.fc1(x))
x = self.fc2(x)
return x
class Dueling_DQN(nn.Module):
def __init__(self, in_channels, num_actions):
super(Dueling_DQN, self).__init__()
self.num_actions = num_actions
self.conv1 = nn.Conv2d(in_channels=in_channels, out_channels=32, kernel_size=8, stride=4)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=4, stride=2)
self.conv3 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1)
self.fc1_adv = nn.Linear(in_features=7*7*64, out_features=512)
self.fc1_val = nn.Linear(in_features=7*7*64, out_features=512)
self.fc2_adv = nn.Linear(in_features=512, out_features=num_actions)
self.fc2_val = nn.Linear(in_features=512, out_features=1)
self.relu = nn.ReLU()
def forward(self, x):
batch_size = x.size(0)
x = self.relu(self.conv1(x))
x = self.relu(self.conv2(x))
x = self.relu(self.conv3(x))
x = x.view(x.size(0), -1)
adv = self.relu(self.fc1_adv(x))
val = self.relu(self.fc1_val(x))
adv = self.fc2_adv(adv)
val = self.fc2_val(val).expand(x.size(0), self.num_actions)
x = val + adv - adv.mean(1).unsqueeze(1).expand(x.size(0), self.num_actions)
return x