-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdqn_model.py
More file actions
54 lines (44 loc) · 1.8 KB
/
dqn_model.py
File metadata and controls
54 lines (44 loc) · 1.8 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import torch.nn as nn
import torch.nn.functional as F
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__()
# Convolutional layers
self.conv1 = nn.Conv2d(in_channels, 32, kernel_size=8, stride=4)
self.conv2 = nn.Conv2d(32, 64, kernel_size=4, stride=2)
self.conv3 = nn.Conv2d(64, 64, kernel_size=3, stride=1)
# Fully connected layers
self.fc1 = nn.Linear(64 * 7 * 7, 512)
self.fc2 = nn.Linear(512, 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.
"""
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
x = x.flatten(1)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x