-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhalfcheetah_pybullet_13_Soft_Actor-Critic_SAC.py
401 lines (315 loc) · 12.8 KB
/
halfcheetah_pybullet_13_Soft_Actor-Critic_SAC.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import os, sys
import time
import numpy as np
# import collections
import math
import gym
# pybullet_envs is required
import pybullet_envs
import ptan
from tensorboardX import SummaryWriter
import torch
import torch.nn as nn
import torch.nn.functional as F
# import torch.nn.utils as nn_utils
import torch.optim as optim
##############################################################################################
# --------------------------------------------------------------------------------------------
# ModelA2C, ModelCritic + ModelSACTwinQ
# --------------------------------------------------------------------------------------------
# Both the actor and critic are placed in the separate networks without sharing weights.
# Critic estimate the mean and the variance for the actions,
# but now the variance is not a separate head of the base network,
# it is just a single parameter of the model.
# This parameter will be adjusted during the training by SGD, but it does not depend on the observation.
### In original paper: HiD_SIZE = 256
# HID_SIZE = 256
HID_SIZE = 64
class ModelActor(nn.Module):
def __init__(self, obs_size, act_size):
super(ModelActor, self).__init__()
self.mu = nn.Sequential(
nn.Linear(obs_size, HID_SIZE),
nn.Tanh(),
nn.Linear(HID_SIZE, HID_SIZE),
nn.Tanh(),
nn.Linear(HID_SIZE, act_size),
nn.Tanh(),
)
# The variance is modeled as a separate network parameter
# and interpreted as a logarithm of the standard deviation.
self.logstd = nn.Parameter(torch.zeros(act_size))
def forward(self, x):
return self.mu(x)
class ModelCritic(nn.Module):
def __init__(self, obs_size):
super(ModelCritic, self).__init__()
self.value = nn.Sequential(
nn.Linear(obs_size, HID_SIZE),
nn.ReLU(),
nn.Linear(HID_SIZE, HID_SIZE),
nn.ReLU(),
nn.Linear(HID_SIZE, 1),
)
def forward(self, x):
return self.value(x)
class ModelSACTwinQ(nn.Module):
def __init__(self, obs_size, act_size):
super(ModelSACTwinQ, self).__init__()
self.q1 = nn.Sequential(
nn.Linear(obs_size + act_size, HID_SIZE),
nn.ReLU(),
nn.Linear(HID_SIZE, HID_SIZE),
nn.ReLU(),
nn.Linear(HID_SIZE, 1),
)
self.q2 = nn.Sequential(
nn.Linear(obs_size + act_size, HID_SIZE),
nn.ReLU(),
nn.Linear(HID_SIZE, HID_SIZE),
nn.ReLU(),
nn.Linear(HID_SIZE, 1),
)
def forward(self, obs, act):
x = torch.cat([obs, act], dim=1)
return self.q1(x), self.q2(x)
# --------------------------------------------------------------------------------------------
# AgentDDPG
# --------------------------------------------------------------------------------------------
class AgentDDPG(ptan.agent.BaseAgent):
"""
Agent implementing Orstein-Uhlenbeck exploration process
"""
def __init__(self, net, device="cpu", ou_enabled=True,
ou_mu=0.0, ou_teta=0.15, ou_sigma=0.2,
ou_epsilon=1.0):
self.net = net
self.device = device
self.ou_enabled = ou_enabled
self.ou_mu = ou_mu
self.ou_teta = ou_teta
self.ou_sigma = ou_sigma
self.ou_epsilon = ou_epsilon
def initial_state(self):
return None
def __call__(self, states, agent_states):
states_v = ptan.agent.float32_preprocessor(states)
states_v = states_v.to(self.device)
mu_v = self.net(states_v)
actions = mu_v.data.cpu().numpy()
if self.ou_enabled and self.ou_epsilon > 0:
new_a_states = []
for a_state, action in zip(agent_states, actions):
if a_state is None:
a_state = np.zeros(
shape=action.shape, dtype=np.float32)
a_state += self.ou_teta * (self.ou_mu - a_state)
a_state += self.ou_sigma * np.random.normal(size=action.shape)
action += self.ou_epsilon * a_state
new_a_states.append(a_state)
else:
new_a_states = agent_states
actions = np.clip(actions, -1, 1)
return actions, new_a_states
##############################################################################################
# --------------------------------------------------------------------------------------------
# test_net
# --------------------------------------------------------------------------------------------
def test_net(net, env, count=10, device="cpu"):
rewards = 0.0
steps = 0
for _ in range(count):
obs = env.reset()
while True:
obs_v = ptan.agent.float32_preprocessor([obs]).to(device)
mu_v = net(obs_v)[0]
action = mu_v.squeeze(dim=0).data.cpu().numpy()
action = np.clip(action, -1, 1)
if np.isscalar(action):
action = [action]
obs, reward, done, _ = env.step(action)
rewards += reward
steps += 1
if done:
break
return rewards / count, steps / count
##############################################################################################
# --------------------------------------------------------------------------------------------
# unpack_batch_a2c + unpack_batch_sac
# --------------------------------------------------------------------------------------------
import torch.distributions as distr
def unpack_batch_a2c(batch, net, last_val_gamma, device="cpu"):
"""
Convert batch into training tensors
:param batch:
:param net:
:return: states variable, actions tensor, reference values variable
"""
states = []
actions = []
rewards = []
not_done_idx = []
last_states = []
for idx, exp in enumerate(batch):
states.append(exp.state)
actions.append(exp.action)
rewards.append(exp.reward)
if exp.last_state is not None:
not_done_idx.append(idx)
last_states.append(exp.last_state)
states_v = ptan.agent.float32_preprocessor(states).to(device)
actions_v = torch.FloatTensor(actions).to(device)
# handle rewards
rewards_np = np.array(rewards, dtype=np.float32)
if not_done_idx:
last_states_v = ptan.agent.float32_preprocessor(last_states).to(device)
last_vals_v = net(last_states_v)
last_vals_np = last_vals_v.data.cpu().numpy()[:, 0]
rewards_np[not_done_idx] += last_val_gamma * last_vals_np
ref_vals_v = torch.FloatTensor(rewards_np).to(device)
return states_v, actions_v, ref_vals_v
@torch.no_grad()
def unpack_batch_sac(batch, val_net, twinq_net, policy_net,
gamma: float, ent_alpha: float,
device="cpu"):
"""
Unpack Soft Actor-Critic batch
"""
states_v, actions_v, ref_q_v = unpack_batch_a2c(batch, val_net, gamma, device)
# references for the critic network
mu_v = policy_net(states_v)
act_dist = distr.Normal(mu_v, torch.exp(policy_net.logstd))
acts_v = act_dist.sample()
q1_v, q2_v = twinq_net(states_v, acts_v)
# element-wise minimum
# We give the agent a bonus for getting into situations
# where the entropy is at maximum, which is very similar to the
# advanced exploration methods.
ref_vals_v = torch.min(q1_v, q2_v).squeeze() - ent_alpha * act_dist.log_prob(acts_v).sum(dim=1)
return states_v, actions_v, ref_vals_v, ref_q_v
##############################################################################################
# --------------------------------------------------------------------------------------------
# HalfCheetah Agent Learning: Soft Actor-Critic (SAC)
# --------------------------------------------------------------------------------------------
# ----------
# device
device = torch.device("cuda")
# ----------
# environment
ENV_ID = "HalfCheetahBulletEnv-v0"
env = gym.make(ENV_ID)
test_env = gym.make(ENV_ID)
# ----------
base_path = '/home/kswada/kw/reinforcement_learning'
args_name = 'trial'
writer = SummaryWriter(comment="-halfcheetah_sac_" + args_name)
save_path = os.path.join(base_path, f'04_output/02_deep_reinforcement_learning_hands_on/halfcheetah/sac_{args_name}/model')
os.makedirs(save_path, exist_ok=True)
# ----------
# net: ModelA2C and ModelCritic
act_net = ModelActor(env.observation_space.shape[0], env.action_space.shape[0]).to(device)
crt_net = ModelCritic(env.observation_space.shape[0]).to(device)
twinq_net = ModelSACTwinQ(env.observation_space.shape[0], env.action_space.shape[0]).to(device)
print(act_net)
print(crt_net)
print(twinq_net)
tgt_crt_net = ptan.agent.TargetNet(crt_net)
# ----------
# agent
agent = AgentDDPG(act_net, device=device)
# ----------
# experience source and replay buffer
GAMMA = 0.99
# in original paper: 10**6
# REPLAY_SIZE = 100000*10
REPLAY_SIZE = 100000
exp_source = ptan.experience.ExperienceSourceFirstLast(env, agent, gamma=GAMMA, steps_count=1)
buffer = ptan.experience.ExperienceReplayBuffer(exp_source, buffer_size=REPLAY_SIZE)
# ----------
# optimizer
# In original paper: 3 * 1e-4
# LR_ACTS = 3e-4
# LR_VALS = 3e-4
LR_ACTS = 1e-4
LR_VALS = 1e-4
act_opt = optim.Adam(act_net.parameters(), lr=LR_ACTS)
crt_opt = optim.Adam(crt_net.parameters(), lr=LR_VALS)
twinq_opt = optim.Adam(twinq_net.parameters(), lr=LR_VALS)
# ----------
frame_idx = 0
best_reward = None
# temperature parameter to determine the relative importance
# of the entropy term against the reward, and thus
# controls the stochasticity of the optimal policy
SAC_ENTROPY_ALPHA = 0.1
# ----------
### In original paper: 256
# BATCH_SIZE = 256
BATCH_SIZE = 64
REPLAY_INITIAL = 10000
TEST_ITERS = 10000
batch_size_tracker = 10
with ptan.common.utils.RewardTracker(writer) as tracker:
with ptan.common.utils.TBMeanTracker(writer, batch_size=10) as tb_tracker:
while True:
frame_idx += 1
buffer.populate(1)
rewards_steps = exp_source.pop_rewards_steps()
if rewards_steps:
rewards, steps = zip(*rewards_steps)
tb_tracker.track("episode_steps", steps[0], frame_idx)
tracker.reward(rewards[0], frame_idx)
if len(buffer) < REPLAY_INITIAL:
continue
batch = buffer.sample(BATCH_SIZE)
states_v, actions_v, ref_vals_v, ref_q_v = unpack_batch_sac(
batch, tgt_crt_net.target_model,
twinq_net, act_net, GAMMA,
SAC_ENTROPY_ALPHA, device)
tb_tracker.track("ref_v", ref_vals_v.mean(), frame_idx)
tb_tracker.track("ref_q", ref_q_v.mean(), frame_idx)
# train TwinQ
twinq_opt.zero_grad()
q1_v, q2_v = twinq_net(states_v, actions_v)
q1_loss_v = F.mse_loss(q1_v.squeeze(), ref_q_v.detach())
q2_loss_v = F.mse_loss(q2_v.squeeze(), ref_q_v.detach())
q_loss_v = q1_loss_v + q2_loss_v
q_loss_v.backward()
twinq_opt.step()
tb_tracker.track("loss_q1", q1_loss_v, frame_idx)
tb_tracker.track("loss_q2", q2_loss_v, frame_idx)
# Critic
crt_opt.zero_grad()
val_v = crt_net(states_v)
v_loss_v = F.mse_loss(val_v.squeeze(), ref_vals_v.detach())
v_loss_v.backward()
crt_opt.step()
tb_tracker.track("loss_v", v_loss_v, frame_idx)
# Actor
act_opt.zero_grad()
acts_v = act_net(states_v)
q_out_v, _ = twinq_net(states_v, acts_v)
act_loss = -q_out_v.mean()
act_loss.backward()
act_opt.step()
tb_tracker.track("loss_act", act_loss, frame_idx)
### In original paper alpha = 1 - 5e-3
tgt_crt_net.alpha_sync(alpha=1 - 1e-3)
if frame_idx % TEST_ITERS == 0:
ts = time.time()
rewards, steps = test_net(act_net, test_env, device=device)
print("Test done in %.2f sec, reward %.3f, steps %d" % (
time.time() - ts, rewards, steps))
writer.add_scalar("test_reward", rewards, frame_idx)
writer.add_scalar("test_steps", steps, frame_idx)
if best_reward is None or best_reward < rewards:
if best_reward is not None:
print("Best reward updated: %.3f -> %.3f" % (best_reward, rewards))
name = "best_%+.3f_%d.dat" % (rewards, frame_idx)
fname = os.path.join(save_path, name)
torch.save(act_net.state_dict(), fname)
best_reward = rewards
# ----------
name = "final_%d.dat" % (frame_idx)
fname = os.path.join(save_path, name)
torch.save(act_net.state_dict(), fname)