-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepql.py
114 lines (93 loc) · 3.58 KB
/
deepql.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
# import : basically PyTorch librabries and Numpy
import torch as T
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
# MLP for deep Q learning
# flake8: noqa
class DeepQN(nn.Module):
def __init__(self, lr, dim_input, fc1_d, fc2_d, actions_output):
super(DeepQN, self).__init__()
self.lr = lr
self.dim_input = dim_input
self.fc1_d = fc1_d
self.fc2_d = fc2_d
self.actions_output = actions_output
self.device = T.device("cuda" if T.cuda.is_available() else "cpu")
# layers
self.fc1 = nn.Linear(self.dim_input, self.fc1_d)
self.fc2 = nn.Linear(self.fc1_d, self.fc2_d)
self.fc3 = nn.Linear(self.fc2_d, self.actions_output)
# optim and loss
self.optimizer = optim.Adam(self.parameters(), lr=self.lr)
self.loss = nn.MSELoss()
# forward method
def forward(self, state):
r1 = F.relu(self.fc1(state))
r2 = F.relu(self.fc2(r1))
actions = self.fc3(r2)
return actions
class ContinuousDeepQN(nn.Module):
def __init__(self, lr, dim_input, fc1_d, fc2_d):
super(ContinuousDeepQN, self).__init__()
self.lr = lr
self.dim_input = dim_input
self.fc1_d = fc1_d
self.fc2_d = fc2_d
self.device = T.device("cuda" if T.cuda.is_available() else "cpu")
# layers
self.fc1 = nn.Linear(self.dim_input, self.fc1_d)
self.fc2 = nn.Linear(self.fc1_d, self.fc2_d)
# Output layer now has 4 neurons, one for each action
self.fc3 = nn.Linear(self.fc2_d, 4)
# optim and loss
self.optimizer = optim.Adam(self.parameters(), lr=self.lr)
self.loss = nn.MSELoss()
def forward(self, state):
r1 = F.relu(self.fc1(state))
r2 = F.relu(self.fc2(r1))
# The network now outputs 4 values
actions = self.fc3(r2)
return actions
class Actor(nn.Module):
def __init__(self, lr, dim_input, fc1_d, fc2_d, dim_action_output):
super(Actor, self).__init__()
self.lr = lr
self.dim_input = dim_input
self.fc1_d = fc1_d
self.fc2_d = fc2_d
self.dim_action_output = dim_action_output
self.device = T.device("cuda" if T.cuda.is_available() else "cpu")
# layers
self.fc1 = nn.Linear(self.dim_input, self.fc1_d)
self.fc2 = nn.Linear(self.fc1_d, self.fc2_d)
self.fc3 = nn.Linear(self.fc2_d, self.dim_action_output)
# optim and loss
self.optimizer = optim.Adam(self.parameters(), lr=self.lr)
def forward(self, state):
r1 = F.relu(self.fc1(state))
r2 = F.relu(self.fc2(r1))
actions = T.tanh(self.fc3(r2))
return actions
class Critic(nn.Module):
def __init__(self, lr, dim_input, fc1_d, fc2_d, dim_action_input):
super(Critic, self).__init__()
self.lr = lr
self.dim_input = dim_input
self.fc1_d = fc1_d
self.fc2_d = fc2_d
self.dim_action_input = dim_action_input
self.device = T.device("cuda" if T.cuda.is_available() else "cpu")
# layers
self.fc1 = nn.Linear(self.dim_input + self.dim_action_input, self.fc1_d)
self.fc2 = nn.Linear(self.fc1_d, self.fc2_d)
self.fc3 = nn.Linear(self.fc2_d, 1)
# optim and loss
self.optimizer = optim.Adam(self.parameters(), lr=self.lr)
self.loss = nn.MSELoss()
def forward(self, state, action):
state_action = T.cat([state, action], dim=1)
r1 = F.relu(self.fc1(state_action))
r2 = F.relu(self.fc2(r1))
q_value = self.fc3(r2)
return q_value