-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddpg-example.py
310 lines (269 loc) · 11 KB
/
ddpg-example.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
'''DLP DDPG Lab'''
__author__ = 'chengscott'
__copyright__ = 'Copyright 2020, NCTU CGI Lab'
import argparse
from collections import deque
import itertools
import random
import time
import torch.optim as optim
import gym
import numpy as np
import torch.nn.functional as F
import torch
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter
class GaussianNoise:
def __init__(self, dim, mu=None, std=None):
self.mu = mu if mu else np.zeros(dim)
self.std = std if std else np.ones(dim) * .1
def sample(self):
return np.random.normal(self.mu, self.std)
class ReplayMemory:
__slots__ = ['buffer']
def __init__(self, capacity):
self.buffer = deque(maxlen=capacity)
def __len__(self):
return len(self.buffer)
def append(self, *transition):
# (state, action, reward, next_state, done)
self.buffer.append(tuple(map(tuple, transition)))
def sample(self, batch_size, device):
'''sample a batch of transition tensors'''
## TODO ##
transitions = random.sample(self.buffer, batch_size)
return (torch.tensor(x, dtype=torch.float, device=device)
for x in zip(*transitions))
class ActorNet(nn.Module):
def __init__(self, state_dim=8, action_dim=2, hidden_dim=(400, 300)):
super().__init__()
h1, h2 = hidden_dim
self.actor_head = nn.Sequential(
nn.Linear(state_dim, h1),
nn.ReLU(),
)
self.actor = nn.Sequential(
nn.Linear(h1, h2),
nn.ReLU(),
)
self.actor_output = nn.Sequential(
nn.Linear(h2, action_dim),
nn.Tanh(),
)
def forward(self, x):
x = self.actor_head(x)
x = self.actor(x)
x = self.actor_output(x)
return x
class CriticNet(nn.Module):
def __init__(self, state_dim=8, action_dim=2, hidden_dim=(400, 300)):
super().__init__()
h1, h2 = hidden_dim
self.critic_head = nn.Sequential(
nn.Linear(state_dim + action_dim, h1),
nn.ReLU(),
)
self.critic = nn.Sequential(
nn.Linear(h1, h2),
nn.ReLU(),
nn.Linear(h2, 1),
)
def forward(self, x, action):
x = self.critic_head(torch.cat([x, action], dim=1))
return self.critic(x)
class DDPG:
def __init__(self, args):
# behavior network
self._actor_net = ActorNet().to(args.device)
self._critic_net = CriticNet().to(args.device)
# target network
self._target_actor_net = ActorNet().to(args.device)
self._target_critic_net = CriticNet().to(args.device)
# initialize target network
self._target_actor_net.load_state_dict(self._actor_net.state_dict())
self._target_critic_net.load_state_dict(self._critic_net.state_dict())
## TODO ##
self._actor_opt = optim.Adam(self._actor_net.parameters(), lr=args.lra)
self._critic_opt = optim.Adam(self._critic_net.parameters(), lr=args.lrc)
# action noise
self._action_noise = GaussianNoise(dim=2)
# memory
self._memory = ReplayMemory(capacity=args.capacity)
## config ##
self.device = args.device
self.batch_size = args.batch_size
self.tau = args.tau
self.gamma = args.gamma
def select_action(self, state, noise=True):
'''based on the behavior (actor) network and exploration noise'''
## TODO ##
state = torch.FloatTensor(state.reshape(1, -1)).to(self.device)
with torch.no_grad():
selected_action = self._actor_net(state).cpu().detach().numpy().flatten()
if noise:
add_noise = self._action_noise.sample()
selected_action = np.clip(selected_action + add_noise, -1.0, 1.0)
return selected_action
def append(self, state, action, reward, next_state, done):
self._memory.append(state, action, [reward / 100], next_state,
[int(done)])
def update(self):
# update the behavior networks
self._update_behavior_network(self.gamma)
# update the target networks
self._update_target_network(self._target_actor_net, self._actor_net,
self.tau)
self._update_target_network(self._target_critic_net, self._critic_net,
self.tau)
def _update_behavior_network(self, gamma):
actor_net, critic_net, target_actor_net, target_critic_net = self._actor_net, self._critic_net, self._target_actor_net, self._target_critic_net
actor_opt, critic_opt = self._actor_opt, self._critic_opt
# sample a minibatch of transitions
state, action, reward, next_state, done = self._memory.sample(self.batch_size, self.device)
## update critic ##
# critic loss
## TODO ##
q_value = self._critic_net(state, action)
with torch.no_grad():
a_next = target_actor_net(next_state).detach()
q_next = target_critic_net(next_state, a_next).detach()
q_target = reward + gamma * (1-done) * q_next
criterion = nn.MSELoss()
critic_loss = criterion(q_value, q_target)
# optimize critic
actor_net.zero_grad()
critic_net.zero_grad()
critic_loss.backward()
critic_opt.step()
## update actor ##
# actor loss
## TODO ##
action = actor_net(state)
actor_loss = -torch.mean(critic_net(state, action))
# optimize actor
actor_net.zero_grad()
critic_net.zero_grad()
actor_loss.backward()
actor_opt.step()
@staticmethod
def _update_target_network(target_net, net, tau):
'''update target network by _soft_ copying from behavior network'''
for target, behavior in zip(target_net.parameters(), net.parameters()):
## TODO ##
target.data.copy_(tau * behavior.data + (1.0 - tau) * target.data)
def save(self, model_path, checkpoint=False):
if checkpoint:
torch.save(
{
'actor': self._actor_net.state_dict(),
'critic': self._critic_net.state_dict(),
'target_actor': self._target_actor_net.state_dict(),
'target_critic': self._target_critic_net.state_dict(),
'actor_opt': self._actor_opt.state_dict(),
'critic_opt': self._critic_opt.state_dict(),
}, model_path)
else:
torch.save(
{
'actor': self._actor_net.state_dict(),
'critic': self._critic_net.state_dict(),
}, model_path)
def load(self, model_path, checkpoint=False):
model = torch.load(model_path)
self._actor_net.load_state_dict(model['actor'])
self._critic_net.load_state_dict(model['critic'])
if checkpoint:
self._target_actor_net.load_state_dict(model['target_actor'])
self._target_critic_net.load_state_dict(model['target_critic'])
self._actor_opt.load_state_dict(model['actor_opt'])
self._critic_opt.load_state_dict(model['critic_opt'])
def train(args, env, agent, writer):
print('Start Training')
total_steps = 0
ewma_reward = 0
for episode in range(args.episode):
total_reward = 0
state = env.reset()
for t in itertools.count(start=1):
# select action
if total_steps < args.warmup:
action = env.action_space.sample()
else:
action = agent.select_action(state)
# execute action
next_state, reward, done, _ = env.step(action)
# store transition
agent.append(state, action, reward, next_state, done)
if total_steps >= args.warmup:
agent.update()
state = next_state
total_reward += reward
total_steps += 1
if done:
ewma_reward = 0.05 * total_reward + (1 - 0.05) * ewma_reward
writer.add_scalar('Train/Episode Reward', total_reward,
total_steps)
writer.add_scalar('Train/Ewma Reward', ewma_reward,
total_steps)
print(
'Step: {}\tEpisode: {}\tLength: {:3d}\tTotal reward: {:.2f}\tEwma reward: {:.2f}'
.format(total_steps, episode, t, total_reward,
ewma_reward))
break
env.close()
def test(args, env, agent, writer):
print('Start Testing')
seeds = (args.seed + i for i in range(10))
rewards = []
for n_episode, seed in enumerate(seeds):
total_reward = 0
env.seed(seed)
state = env.reset()
## TODO ##
for t in itertools.count(start=1):
action = agent.select_action(state)
env.render()
state, reward, done, _ = env.step(action)
total_reward += reward
if done:
writer.add_scalar('Test/Episode Reward', total_reward, n_episode)
state = env.reset()
break
rewards.append(total_reward)
rewards = np.array(rewards)
print('Average Reward', np.mean(rewards))
env.close()
def main():
## arguments ##
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-d', '--device', default='cuda')
parser.add_argument('-m', '--model', default='ddpg.pth')
parser.add_argument('--logdir', default='log/ddpg')
# train
parser.add_argument('--warmup', default=50000, type=int)
# parser.add_argument('--warmup', default=10000, type=int)
# parser.add_argument('--episode', default=2000, type=int)
# parser.add_argument('--batch_size', default=64, type=int)
parser.add_argument('--episode', default=2800, type=int)
parser.add_argument('--batch_size', default=128, type=int)
parser.add_argument('--capacity', default=500000, type=int)
parser.add_argument('--lra', default=1e-3, type=float)
parser.add_argument('--lrc', default=1e-3, type=float)
parser.add_argument('--gamma', default=.99, type=float)
parser.add_argument('--tau', default=.005, type=float)
# test
parser.add_argument('--test_only', action='store_true')
parser.add_argument('--render', action='store_true')
parser.add_argument('--seed', default=20200519, type=int)
args = parser.parse_args()
## main ##
env = gym.make('LunarLanderContinuous-v2')
agent = DDPG(args)
writer = SummaryWriter(args.logdir)
if args.test_only:
train(args, env, agent, writer)
agent.save(args.model)
agent.load(args.model)
test(args, env, agent, writer)
if __name__ == '__main__':
main()