-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDDQN_discrete.py
261 lines (208 loc) · 9.09 KB
/
DDQN_discrete.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import gym
import torch
import numpy as np
from torch import nn
import random
import torch.nn.functional as F
import collections
from torch.optim.lr_scheduler import StepLR
"""
Implementation of Double DQN for gym environments with discrete action space.
"""
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
"""
The Q-Network has as input a state s and outputs the state-action values q(s,a_1), ..., q(s,a_n) for all n actions.
"""
class QNetwork(nn.Module):
def __init__(self, action_dim, state_dim, hidden_dim):
super(QNetwork, self).__init__()
self.fc_1 = nn.Linear(state_dim, hidden_dim)
self.fc_2 = nn.Linear(hidden_dim, hidden_dim)
self.fc_3 = nn.Linear(hidden_dim, action_dim)
def forward(self, inp):
x1 = F.leaky_relu(self.fc_1(inp))
x1 = F.leaky_relu(self.fc_2(x1))
x1 = self.fc_3(x1)
return x1
"""
If the observations are images we use CNNs.
"""
class QNetworkCNN(nn.Module):
def __init__(self, action_dim):
super(QNetworkCNN, self).__init__()
self.conv_1 = nn.Conv2d(3, 32, kernel_size=8, stride=4)
self.conv_2 = nn.Conv2d(32, 64, kernel_size=4, stride=3)
self.conv_3 = nn.Conv2d(64, 64, kernel_size=3, stride=1)
self.fc_1 = nn.Linear(8960, 512)
self.fc_2 = nn.Linear(512, action_dim)
def forward(self, inp):
inp = inp.view((1, 3, 210, 160))
x1 = F.relu(self.conv_1(inp))
x1 = F.relu(self.conv_2(x1))
x1 = F.relu(self.conv_3(x1))
x1 = torch.flatten(x1, 1)
x1 = F.leaky_relu(self.fc_1(x1))
x1 = self.fc_2(x1)
return x1
"""
memory to save the state, action, reward sequence from the current episode.
"""
class Memory:
def __init__(self, len):
self.rewards = collections.deque(maxlen=len)
self.state = collections.deque(maxlen=len)
self.action = collections.deque(maxlen=len)
self.is_done = collections.deque(maxlen=len)
def update(self, state, action, reward, done):
# if the episode is finished we do not save to new state. Otherwise we have more states per episode than rewards
# and actions whcih leads to a mismatch when we sample from memory.
if not done:
self.state.append(state)
self.action.append(action)
self.rewards.append(reward)
self.is_done.append(done)
def sample(self, batch_size):
"""
sample "batch_size" many (state, action, reward, next state, is_done) datapoints.
"""
n = len(self.is_done)
idx = random.sample(range(0, n-1), batch_size)
state = np.array(self.state)
action = np.array(self.action)
return torch.Tensor(state)[idx].to(device), torch.LongTensor(action)[idx].to(device), \
torch.Tensor(state)[1+np.array(idx)].to(device), torch.Tensor(self.rewards)[idx].to(device), \
torch.Tensor(self.is_done)[idx].to(device)
def reset(self):
self.rewards.clear()
self.state.clear()
self.action.clear()
self.is_done.clear()
def select_action(model, env, state, eps):
state = torch.Tensor(state).to(device)
with torch.no_grad():
values = model(state)
# select a random action wih probability eps
if random.random() <= eps:
action = np.random.randint(0, env.action_space.n)
else:
action = np.argmax(values.cpu().numpy())
return action
def train(batch_size, current, target, optim, memory, gamma):
states, actions, next_states, rewards, is_done = memory.sample(batch_size)
q_values = current(states)
next_q_values = current(next_states)
next_q_state_values = target(next_states)
q_value = q_values.gather(1, actions.unsqueeze(1)).squeeze(1)
next_q_value = next_q_state_values.gather(1, torch.max(next_q_values, 1)[1].unsqueeze(1)).squeeze(1)
expected_q_value = rewards + gamma * next_q_value * (1 - is_done)
loss = (q_value - expected_q_value.detach()).pow(2).mean()
optim.zero_grad()
loss.backward()
optim.step()
def evaluate(Qmodel, env, repeats):
"""
Runs a greedy policy with respect to the current Q-Network for "repeats" many episodes. Returns the average
episode reward.
"""
Qmodel.eval()
perform = 0
for _ in range(repeats):
state = env.reset()
done = False
while not done:
state = torch.Tensor(state).to(device)
with torch.no_grad():
values = Qmodel(state)
action = np.argmax(values.cpu().numpy())
state, reward, done, _ = env.step(action)
perform += reward
Qmodel.train()
return perform/repeats
def update_parameters(current_model, target_model):
target_model.load_state_dict(current_model.state_dict())
def main(gamma=0.99, lr=1e-3, min_episodes=20, eps=1, eps_decay=0.998, eps_min=0.01, update_step=10, batch_size=64, update_repeats=50,
num_episodes=3000, seed=42, max_memory_size=5000, lr_gamma=1, lr_step=100, measure_step=100,
measure_repeats=100, hidden_dim=64, env_name='CartPole-v1', cnn=False, horizon=np.inf, render=True, render_step=50):
"""
Remark: Convergence is slow. Wait until around episode 2500 to see good performance.
:param gamma: reward discount factor
:param lr: learning rate for the Q-Network
:param min_episodes: we wait "min_episodes" many episodes in order to aggregate enough data before starting to train
:param eps: probability to take a random action during training
:param eps_decay: after every episode "eps" is multiplied by "eps_decay" to reduces exploration over time
:param eps_min: minimal value of "eps"
:param update_step: after "update_step" many episodes the Q-Network is trained "update_repeats" many times with a
batch of size "batch_size" from the memory.
:param batch_size: see above
:param update_repeats: see above
:param num_episodes: the number of episodes played in total
:param seed: random seed for reproducibility
:param max_memory_size: size of the replay memory
:param lr_gamma: learning rate decay for the Q-Network
:param lr_step: every "lr_step" episodes we decay the learning rate
:param measure_step: every "measure_step" episode the performance is measured
:param measure_repeats: the amount of episodes played in to asses performance
:param hidden_dim: hidden dimensions for the Q_network
:param env_name: name of the gym environment
:param cnn: set to "True" when using environments with image observations like "Pong-v0"
:param horizon: number of steps taken in the environment before terminating the episode (prevents very long episodes)
:param render: if "True" renders the environment every "render_step" episodes
:param render_step: see above
:return: the trained Q-Network and the measured performances
"""
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
env = gym.make(env_name)
env.seed(seed)
if cnn:
Q_1 = QNetworkCNN(action_dim=env.action_space.n).to(device)
Q_2 = QNetworkCNN(action_dim=env.action_space.n).to(device)
else:
Q_1 = QNetwork(action_dim=env.action_space.n, state_dim=env.observation_space.shape[0],
hidden_dim=hidden_dim).to(device)
Q_2 = QNetwork(action_dim=env.action_space.n, state_dim=env.observation_space.shape[0],
hidden_dim=hidden_dim).to(device)
# transfer parameters from Q_1 to Q_2
update_parameters(Q_1, Q_2)
# we only train Q_1
for param in Q_2.parameters():
param.requires_grad = False
optimizer = torch.optim.Adam(Q_1.parameters(), lr=lr)
scheduler = StepLR(optimizer, step_size=lr_step, gamma=lr_gamma)
memory = Memory(max_memory_size)
performance = []
for episode in range(num_episodes):
# display the performance
if (episode % measure_step == 0) and episode >= min_episodes:
performance.append([episode, evaluate(Q_1, env, measure_repeats)])
print("Episode: ", episode)
print("rewards: ", performance[-1][1])
print("lr: ", scheduler.get_last_lr()[0])
print("eps: ", eps)
state = env.reset()
memory.state.append(state)
done = False
i = 0
while not done:
i += 1
action = select_action(Q_2, env, state, eps)
state, reward, done, _ = env.step(action)
if i > horizon:
done = True
# render the environment if render == True
if render and episode % render_step == 0:
env.render()
# save state, action, reward sequence
memory.update(state, action, reward, done)
if episode >= min_episodes and episode % update_step == 0:
for _ in range(update_repeats):
train(batch_size, Q_1, Q_2, optimizer, memory, gamma)
# transfer new parameter from Q_1 to Q_2
update_parameters(Q_1, Q_2)
# update learning rate and eps
scheduler.step()
eps = max(eps*eps_decay, eps_min)
return Q_1, performance
if __name__ == '__main__':
main()