-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdqn.py
92 lines (73 loc) · 2.45 KB
/
dqn.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
import torch.nn as nn
class DQN(nn.Module):
def __init__(self, channels_in, num_actions):
super(DQN, self).__init__()
self.conv1 = nn.Conv2d(in_channels=channels_in,
out_channels=32,
kernel_size=8,
stride=4)
self.relu1 = nn.ReLU(True)
self.conv2 = nn.Conv2d(in_channels=32,
out_channels=64,
kernel_size=4,
stride=2)
self.relu2 = nn.ReLU(True)
self.conv3 = nn.Conv2d(in_channels=64,
out_channels=64,
kernel_size=3,
stride=1)
self.relu3 = nn.ReLU(True)
self.flat = Flatten()
self.fc4 = nn.Linear(in_features=64*7*7,
out_features=512)
self.relu4 = nn.ReLU(True)
self.fc5 = nn.Linear(in_features=512,
out_features=num_actions)
def forward(self, x):
"""
Forward pass of the dqn. Should not be called
manually but by calling a model instance directly.
Inputs:
- x: PyTorch input Variable
"""
x = self.conv1(x)
x = self.relu1(x)
x = self.conv2(x)
x = self.relu2(x)
x = self.conv3(x)
x = self.relu3(x)
x = self.flat(x) # change the view from 2d to 1d
x = self.fc4(x)
x = self.relu4(x)
x = self.fc5(x)
return x
@property
def is_cuda(self):
"""
Check if model parameters are allocated on the GPU.
"""
return next(self.parameters()).is_cuda
def save(self, path):
"""
Save model with its parameters to the given path. Conventionally the
path should end with "*.model".
Inputs:
- path: path string
"""
print('Saving model... %s' % path)
torch.save(self.state_dict(), path)
def load(self, path):
"""
Load model with its parameters from the given path. Conventionally the
path should end with "*.model".
Inputs:
- path: path string
"""
print('Loading model... %s' % path)
self.load_state_dict(torch.load(path, map_location=lambda storage, loc: storage))
class Flatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), -1)