-
Notifications
You must be signed in to change notification settings - Fork 0
/
dqn_model.py
92 lines (79 loc) · 3.06 KB
/
dqn_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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import torch.nn as nn
import torch.nn.functional as F
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class DQN(nn.Module):
"""Initialize a deep Q-learning network
Hints:
-----
Original paper for DQN
https://storage.googleapis.com/deepmind-data/assets/papers/DeepMindNature14236Paper.pdf
This is just a hint. You can build your own structure.
"""
def __init__(self, in_channels=4, num_actions=4):
"""
Parameters:
-----------
in_channels: number of channel of input.
i.e The number of most recent frames stacked together, here we use 4 frames, which means each state in Breakout is composed of 4 frames.
num_actions: number of action-value to output, one-to-one correspondence to action in game.
You can add additional arguments as you need.
In the constructor we instantiate modules and assign them as
member variables.
"""
super(DQN, self).__init__()
###########################
# YOUR IMPLEMENTATION HERE #
self.num_actions = num_actions
self.conv_layes = nn.Sequential(
nn.Conv2d(in_channels, 32, kernel_size=8, stride=4),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=4, stride=2),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1),
nn.ReLU(),
)
out_h = out_w = self._conv2d_size_out(
self._conv2d_size_out(self._conv2d_size_out(84, 8, 4), 4, 2),
3,
1
)
self.in_features =int(out_h*out_w*64)
self.value_stream = nn.Sequential(
nn.Linear(self.in_features, 512),
nn.ReLU(),
nn.Linear(512, 1)
)
self.advantage_stream = nn.Sequential(
nn.Linear(self.in_features, 512),
nn.ReLU(),
nn.Linear(512, self.num_actions)
)
#The output features of convolutional layer needs to be known for computing input to fully connected layer
self.fc_layers = nn.Sequential(
nn.Linear(self.in_features, 512),
nn.ReLU(),
nn.Linear(512, self.num_actions),
)
def forward(self, x):
"""
In the forward function we accept a Tensor of input data and we must return
a Tensor of output data. We can use Modules defined in the constructor as
well as arbitrary operators on Tensors.
"""
###########################
# YOUR IMPLEMENTATION HERE #
x = x.to(device)
conv_output = self.conv_layes(x)
conv_output = conv_output.view(x.size(0), -1)
# x = self.fc_layers(conv_output)
values = self.value_stream(conv_output)
advantages = self.advantage_stream(conv_output)
x = values + (advantages - advantages.mean())
###########################
return x
@staticmethod
def _conv2d_size_out(size, kernel_size, stride):
return (size - (kernel_size - 1) - 1) / stride + 1