-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsac.py
354 lines (274 loc) · 14.6 KB
/
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
# Code from spinning-up repository: https://github.com/openai/spinningup
from copy import deepcopy
import itertools
import numpy as np
import torch
from torch.optim import Adam
import gym
import time
import core as core
import torch.nn.functional as F
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class ReplayBuffer:
"""
A simple FIFO experience replay buffer for SAC agents.
"""
def __init__(self, obs_dim, act_dim, size=int(1e6)):
self.state = np.zeros(core.combined_shape(size, obs_dim), dtype=np.float32)
self.next_state = np.zeros(core.combined_shape(size, obs_dim), dtype=np.float32)
self.action = np.zeros(core.combined_shape(size, act_dim), dtype=np.float32)
self.reward = np.zeros(size, dtype=np.float32)
self.cost = np.zeros(size, dtype=np.float32)
self.done = np.zeros(size, dtype=np.float32)
self.ptr, self.size, self.max_size = 0, 0, size
def store(self, obs, act, rew, next_obs, done, cost=None):
self.state[self.ptr] = obs
self.next_state[self.ptr] = next_obs
self.action[self.ptr] = act
self.reward[self.ptr] = rew
if cost is not None:
self.cost[self.ptr] = cost
self.done[self.ptr] = done
self.ptr = (self.ptr+1) % self.max_size
self.size = min(self.size+1, self.max_size)
def sample_batch(self, batch_size=32):
idxs = np.random.randint(0, self.size, size=batch_size)
batch = dict(obs=self.state[idxs],
obs2=self.next_state[idxs],
act=self.action[idxs],
rew=self.reward[idxs],
cost=self.cost[idxs],
done=self.done[idxs])
return_dict = {k: torch.as_tensor(v, dtype=torch.float32).to(device) for k,v in batch.items()}
return_dict['idx']=idxs
return return_dict
class SAC:
def __init__(self,env_fn, models,replay_buffer, termination_function,actor_critic=core.MLPActorCritic, ac_kwargs=dict(), seed=0,
steps_per_epoch=4000, epochs=100, replay_size=int(1e6), gamma=0.99,
polyak=0.995, lr=1e-3, alpha=0.2, batch_size=256, start_steps=10000,
update_after=1000, update_every=50, num_test_episodes=10, max_ep_len=1000,automatic_alpha_tuning=True,
save_freq=1, A2=False, use_bc_loss=False):
"""
Soft Actor-Critic (SAC)
Args:
env_fn : A function which creates a copy of the environment.
The environment must satisfy the OpenAI Gym API.
actor_critic: The constructor method for a PyTorch Module with an ``act``
method, a ``pi`` module, a ``q1`` module, and a ``q2`` module.
The ``act`` method and ``pi`` module should accept batches of
observations as inputs, and ``q1`` and ``q2`` should accept a batch
of observations and a batch of actions as inputs. When called,
``act``, ``q1``, and ``q2`` should return:
=========== ================ ======================================
Call Output Shape Description
=========== ================ ======================================
``act`` (batch, act_dim) | Numpy array of actions for each
| observation.
``q1`` (batch,) | Tensor containing one current estimate
| of Q* for the provided observations
| and actions. (Critical: make sure to
| flatten this!)
``q2`` (batch,) | Tensor containing the other current
| estimate of Q* for the provided observations
| and actions. (Critical: make sure to
| flatten this!)
=========== ================ ======================================
Calling ``pi`` should return:
=========== ================ ======================================
Symbol Shape Description
=========== ================ ======================================
``a`` (batch, act_dim) | Tensor containing actions from policy
| given observations.
``logp_pi`` (batch,) | Tensor containing log probabilities of
| actions in ``a``. Importantly: gradients
| should be able to flow back into ``a``.
=========== ================ ======================================
ac_kwargs (dict): Any kwargs appropriate for the ActorCritic object
you provided to SAC.
seed (int): Seed for random number generators.
steps_per_epoch (int): Number of steps of interaction (state-action pairs)
for the agent and the environment in each epoch.
epochs (int): Number of epochs to run and train agent.
replay_size (int): Maximum length of replay buffer.
gamma (float): Discount factor. (Always between 0 and 1.)
polyak (float): Interpolation factor in polyak averaging for target
networks. Target networks are updated towards main networks
according to:
.. math:: \\theta_{\\text{targ}} \\leftarrow
\\rho \\theta_{\\text{targ}} + (1-\\rho) \\theta
where :math:`\\rho` is polyak. (Always between 0 and 1, usually
close to 1.)
lr (float): Learning rate (used for both policy and value learning).
alpha (float): Entropy regularization coefficient. (Equivalent to
inverse of reward scale in the original SAC paper.)
batch_size (int): Minibatch size for SGD.
start_steps (int): Number of steps for uniform-random action selection,
before running real policy. Helps exploration.
update_after (int): Number of env interactions to collect before
starting to do gradient descent updates. Ensures replay buffer
is full enough for useful updates.
update_every (int): Number of env interactions that should elapse
between gradient descent updates. Note: Regardless of how long
you wait between updates, the ratio of env steps to gradient steps
is locked to 1.
num_test_episodes (int): Number of episodes to test the deterministic
policy at the end of each epoch.
max_ep_len (int): Maximum length of trajectory / episode / rollout.
logger_kwargs (dict): Keyword args for EpochLogger.
save_freq (int): How often (in terms of gap between epochs) to save
the current policy and value function.
"""
torch.manual_seed(seed)
np.random.seed(seed)
self.env, self.test_env = env_fn(), env_fn()
self.obs_dim = self.env.observation_space.shape
self.act_dim = self.env.action_space.shape[0]
self.max_ep_len=max_ep_len
self.start_steps=start_steps
self.batch_size=batch_size
self.gamma=gamma
self.alpha=alpha
self.polyak=polyak
self.A2= A2
# Action limit for clamping: critically, assumes all dimensions share the same bound!
self.act_limit = self.env.action_space.high[0]
self.steps_per_epoch=steps_per_epoch
self.update_after=update_after
self.update_every=update_every
self.num_test_episodes=num_test_episodes
self.epochs = epochs
# Create actor-critic module and target networks
self.ac = actor_critic(self.env.observation_space, self.env.action_space, **ac_kwargs)
self.ac_targ = deepcopy(self.ac)
self.termination_func = termination_function
self.models = models
# Freeze target networks with respect to optimizers (only update via polyak averaging)
for p in self.ac_targ.parameters():
p.requires_grad = False
# List of parameters for both Q-networks (save this for convenience)
self.q_params = itertools.chain(self.ac.q1.parameters(), self.ac.q2.parameters())
# Experience buffer
self.replay_buffer = replay_buffer
# Count variables (protip: try to get a feel for how different size networks behave!)
self.var_counts = tuple(core.count_vars(module) for module in [self.ac.pi, self.ac.q1, self.ac.q2])
# Set up optimizers for policy and q-function
self.pi_optimizer = Adam(self.ac.pi.parameters(), lr=lr)
self.q_optimizer = Adam(self.q_params, lr=lr)
self.v_optimizer = Adam(self.ac.v.parameters(), lr=lr)
self.automatic_alpha_tuning = automatic_alpha_tuning
if self.automatic_alpha_tuning is True:
self.target_entropy = -torch.prod(torch.Tensor(self.env.action_space.shape).to(device)).item()
self.log_alpha = torch.zeros(1, requires_grad=True, device=device)
self.alpha_optim = Adam([self.log_alpha], lr=lr)
self.alpha = self.log_alpha.exp()
else:
self.alpha = alpha
self.use_bc_loss = use_bc_loss
# Set up function for computing SAC Q-losses
def compute_loss_q(self,data):
o, a, r, o2, d = data['obs'], data['act'], data['rew'], data['obs2'], data['done']
q1 = self.ac.q1(o,a)
q2 = self.ac.q2(o,a)
if(self.A2):
# Bellman backup for Q functions
idxs = data['idx']
next_idxs = (idxs+1)%self.replay_buffer.max_size
with torch.no_grad():
next_actions = torch.as_tensor(self.replay_buffer.action[next_idxs], dtype=torch.float32).to(device)
next_obs = torch.as_tensor(self.replay_buffer.state[next_idxs], dtype=torch.float32).to(device)
# Target actions come from *current* policy
q1_pi_targ = self.ac_targ.q1(o2,next_actions)
q2_pi_targ = self.ac_targ.q2(o2,next_actions)
q_pi_targ = torch.min(q1_pi_targ, q2_pi_targ)
backup = (((o2-next_obs).sum(1))==0).float()*(r + self.gamma*(1-d)*(q_pi_targ))
else:
# Bellman backup for Q functions
with torch.no_grad():
# Target actions come from *current* policy
a2, logp_a2 = self.ac.pi(o2)
# Target Q-values
q1_pi_targ = self.ac_targ.q1(o2, a2)
q2_pi_targ = self.ac_targ.q2(o2, a2)
q_pi_targ = torch.min(q1_pi_targ, q2_pi_targ)
backup = r + self.gamma * (1 - d) * (q_pi_targ - self.alpha * logp_a2)
# MSE loss against Bellman backup
loss_q1 = ((q1 - backup)**2).mean()
loss_q2 = ((q2 - backup)**2).mean()
loss_q = loss_q1 + loss_q2
# Useful info for logging
q_info = dict(Q1Vals=q1.cpu().detach().numpy(),
Q2Vals=q2.cpu().detach().numpy())
return loss_q, q_info
def getQ(self, state,action):
state = torch.FloatTensor(state.reshape(1, -1)).to(device)
action = torch.FloatTensor(action.reshape(1, -1)).to(device)
Q1,Q2 = self.ac.q1(state,action),self.ac.q2(state,action)
return Q1.cpu().data.numpy(),Q2.cpu().data.numpy()
def compute_loss_pi(self,data):
o = data['obs']
a = data['act']
pi, logp_pi = self.ac.pi(o)
q1_pi = self.ac.q1(o, pi)
q2_pi = self.ac.q2(o, pi)
q_pi = torch.min(q1_pi, q2_pi)
if self.use_bc_loss:
# BC loss inspired from TD3_BC offline RL algorithm
# Refer https://github.com/sfujim/TD3_BC
lmbda = 2.5/q_pi.abs().mean().detach()
loss_pi = (self.alpha * logp_pi - q_pi).mean() * lmbda + F.mse_loss(pi,a)
else:
# Entropy-regularized policy loss
loss_pi = (self.alpha * logp_pi - q_pi).mean()
pi_info = dict(LogPi=logp_pi.cpu().detach())
return loss_pi, pi_info
# Set up model saving
def update(self,data):
# First run one gradient descent step for Q1 and Q2
self.q_optimizer.zero_grad()
loss_q, q_info = self.compute_loss_q(data)
loss_q.backward()
self.q_optimizer.step()
# Freeze Q-networks so you don't waste computational effort
# computing gradients for them during the policy learning step.
for p in self.q_params:
p.requires_grad = False
# Next run one gradient descent step for pi.
self.pi_optimizer.zero_grad()
loss_pi, pi_info = self.compute_loss_pi(data)
log_pi = pi_info['LogPi']
loss_pi.backward()
self.pi_optimizer.step()
# Unfreeze Q-networks so you can optimize it at next DDPG step.
for p in self.q_params:
p.requires_grad = True
if self.automatic_alpha_tuning:
alpha_loss = -(self.log_alpha * (log_pi.to(device) + self.target_entropy)).mean()
self.alpha_optim.zero_grad()
alpha_loss.backward()
self.alpha_optim.step()
self.alpha = self.log_alpha.exp()
# Finally, update target networks by polyak averaging.
with torch.no_grad():
for p, p_targ in zip(self.ac.parameters(), self.ac_targ.parameters()):
# NB: We use an in-place operations "mul_", "add_" to update target
# params, as opposed to "mul" and "add", which would make new tensors.
p_targ.data.mul_(self.polyak)
p_targ.data.add_((1 - self.polyak) * p.data)
def get_action(self,o, deterministic=False):
return self.ac.act(torch.as_tensor(o, dtype=torch.float32).to(device),
deterministic)
def test_agent(self):
for j in range(self.num_test_episodes):
o, d, ep_ret, ep_len = self.test_env.reset(), False, 0, 0
while not(d or (ep_len == self.max_ep_len)):
# Take deterministic actions at test time
o, r, d, _ = self.test_env.step(self.get_action(o, True))
ep_ret += r
ep_len += 1
return ep_ret,ep_len
def reset(self):
pass
def train(self):
for j in range(self.update_every):
batch = self.replay_buffer.sample_batch(self.batch_size)
self.update(data=batch)