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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653 | class SAC:
def __init__(self, config: SacConfig, action_size: int) -> None:
self._config = config
self._action_size = action_size
# define the networks
self._policy, self._critic = make_sac_networks(
action_size=action_size,
critic_hidden_layer_size=self._config.critic_hidden_layer_size,
policy_hidden_layer_size=self._config.policy_hidden_layer_size,
)
# define the action distribution
self._parametric_action_distribution = NormalTanhDistribution(
event_size=action_size
)
self._sample_action_fn = self._parametric_action_distribution.sample
def init(
self, key: RNGKey, action_size: int, observation_size: int
) -> SacTrainingState:
"""Initialise the training state of the algorithm.
Args:
key: a jax random key
action_size: the size of the environment's action space
observation_size: the size of the environment's observation space
Returns:
the initial training state of SAC
"""
# define policy and critic params
dummy_obs = jnp.zeros((1, observation_size))
dummy_action = jnp.zeros((1, action_size))
key, subkey = jax.random.split(key)
policy_params = self._policy.init(subkey, dummy_obs)
key, subkey = jax.random.split(key)
critic_params = self._critic.init(subkey, dummy_obs, dummy_action)
target_critic_params = jax.tree.map(
lambda x: jnp.asarray(x.copy()), critic_params
)
# define initial optimizer states
optimizer = optax.adam(learning_rate=1.0)
policy_optimizer_state = optimizer.init(policy_params)
critic_optimizer_state = optimizer.init(critic_params)
log_alpha = jnp.asarray(jnp.log(self._config.alpha_init), dtype=jnp.float32)
alpha_optimizer_state = optimizer.init(log_alpha)
# create and retrieve the training state
training_state = SacTrainingState(
policy_optimizer_state=policy_optimizer_state,
policy_params=policy_params,
critic_optimizer_state=critic_optimizer_state,
critic_params=critic_params,
alpha_optimizer_state=alpha_optimizer_state,
alpha_params=log_alpha,
target_critic_params=target_critic_params,
normalization_running_stats=RunningMeanStdState(
mean=jnp.zeros(
observation_size,
),
var=jnp.ones(
observation_size,
),
count=jnp.zeros(()),
),
key=key,
steps=jnp.array(0),
)
return training_state
def select_action(
self,
obs: Observation,
policy_params: Params,
key: RNGKey,
deterministic: bool = False,
) -> Action:
"""Selects an action according to SAC policy.
Args:
obs: agent observation(s)
policy_params: parameters of the agent's policy
key: jax random key
deterministic: whether to select action in a deterministic way.
Defaults to False.
Returns:
The selected action and a new random key.
"""
dist_params = self._policy.apply(policy_params, obs)
if not deterministic:
actions = self._sample_action_fn(dist_params, key)
else:
# The first half of parameters is for mean and the second half for variance
actions = jax.nn.tanh(dist_params[..., : dist_params.shape[-1] // 2])
return actions
def play_step_fn(
self,
env_state: EnvState,
training_state: SacTrainingState,
env: Env,
deterministic: bool = False,
evaluation: bool = False,
) -> Tuple[EnvState, SacTrainingState, Transition]:
"""Plays a step in the environment. Selects an action according to SAC rule and
performs the environment step.
Args:
env_state: the current environment state
training_state: the SAC training state
env: the environment
deterministic: the whether to select action in a deterministic way.
Defaults to False.
evaluation: if True, collected transitions are not used to update training
state. Defaults to False.
Returns:
the new environment state
the new SAC training state
the played transition
"""
key = training_state.key
policy_params = training_state.policy_params
obs = env_state.obs
if self._config.normalize_observations:
normalized_obs = normalize_with_rmstd(
obs, training_state.normalization_running_stats
)
normalization_running_stats = update_running_mean_std(
training_state.normalization_running_stats, obs
)
else:
normalized_obs = obs
normalization_running_stats = training_state.normalization_running_stats
key, subkey = jax.random.split(key)
actions = self.select_action(
obs=normalized_obs,
policy_params=policy_params,
key=subkey,
deterministic=deterministic,
)
key, subkey = jax.random.split(key)
if not evaluation:
training_state = training_state.replace(
key=subkey,
normalization_running_stats=normalization_running_stats,
)
else:
training_state = training_state.replace(
key=subkey,
)
next_env_state = env.step(env_state, actions)
next_obs = next_env_state.obs
truncations = next_env_state.info["truncation"]
transition = Transition(
obs=env_state.obs,
next_obs=next_obs,
rewards=next_env_state.reward,
dones=next_env_state.done,
actions=actions,
truncations=truncations,
)
return (
next_env_state,
training_state,
transition,
)
def play_qd_step_fn(
self,
env_state: EnvState,
training_state: SacTrainingState,
env: Env,
deterministic: bool = False,
evaluation: bool = False,
) -> Tuple[EnvState, SacTrainingState, QDTransition]:
"""Plays a step in the environment. Selects an action according to SAC rule and
performs the environment step.
Args:
env_state: the current environment state
training_state: the SAC training state
env: the environment
deterministic: the whether to select action in a deterministic way.
Defaults to False.
evaluation: if True, collected transitions are not used to update training
state. Defaults to False.
Returns:
the new environment state
the new SAC training state
the played transition
"""
next_env_state, training_state, transition = self.play_step_fn(
env_state, training_state, env, deterministic, evaluation
)
actions = transition.actions
next_env_state = env.step(env_state, actions)
next_obs = next_env_state.obs
truncations = next_env_state.info["truncation"]
transition = QDTransition(
obs=env_state.obs,
next_obs=next_obs,
rewards=next_env_state.reward,
dones=next_env_state.done,
actions=actions,
truncations=truncations,
state_desc=env_state.info["state_descriptor"],
next_state_desc=next_env_state.info["state_descriptor"],
)
return (
next_env_state,
training_state,
transition,
)
def eval_policy_fn(
self,
training_state: SacTrainingState,
eval_env_first_state: EnvState,
play_step_fn: Callable[
[EnvState, Params],
Tuple[EnvState, SacTrainingState, Transition],
],
) -> Tuple[Reward, Reward]:
"""Evaluates the agent's policy over an entire episode, across all batched
environments.
Args:
training_state: the SAC training state
eval_env_first_state: the initial state for evaluation
play_step_fn: the play_step function used to collect the evaluation episode
Returns:
the true return averaged over batch dimension, shape: (1,)
the true return per environment, shape: (env_batch_size,)
"""
state, training_state, transitions = generate_unroll(
init_state=eval_env_first_state,
training_state=training_state,
episode_length=self._config.episode_length,
play_step_fn=play_step_fn,
)
transitions = get_first_episode(transitions)
true_returns = jnp.nansum(transitions.rewards, axis=0)
true_return = jnp.mean(true_returns, axis=-1)
return true_return, true_returns
def eval_qd_policy_fn(
self,
training_state: SacTrainingState,
eval_env_first_state: EnvState,
play_step_fn: Callable[
[EnvState, Params],
Tuple[EnvState, SacTrainingState, QDTransition],
],
descriptor_extraction_fn: Callable[[QDTransition, Mask], Descriptor],
) -> Tuple[Reward, Descriptor, Reward, Descriptor]:
"""
Evaluates the agent's policy over an entire episode, across all batched
environments for QD environments. Averaged descriptors are returned as well.
Args:
training_state: the SAC training state
eval_env_first_state: the initial state for evaluation
play_step_fn: the play_step function used to collect the evaluation episode
Returns:
the true return averaged over batch dimension, shape: (1,)
the descriptor averaged over batch dimension, shape: (num_descriptors,)
the true return per environment, shape: (env_batch_size,)
the descriptor per environment, shape: (env_batch_size, num_descriptors)
"""
state, training_state, transitions = generate_unroll(
init_state=eval_env_first_state,
training_state=training_state,
episode_length=self._config.episode_length,
play_step_fn=play_step_fn,
)
transitions = get_first_episode(transitions)
true_returns = jnp.nansum(transitions.rewards, axis=0)
true_return = jnp.mean(true_returns, axis=-1)
transitions = jax.tree.map(lambda x: jnp.swapaxes(x, 0, 1), transitions)
masks = jnp.isnan(transitions.rewards)
descriptors = descriptor_extraction_fn(transitions, masks)
mean_descriptor = jnp.mean(descriptors, axis=0)
return true_return, mean_descriptor, true_returns, descriptors
def _update_alpha(
self,
alpha_lr: float,
training_state: SacTrainingState,
transitions: Transition,
key: RNGKey,
) -> Tuple[Params, optax.OptState, jax.Array]:
"""Updates the alpha parameter if necessary. Else, it keeps the
current value.
Args:
alpha_lr: alpha learning rate
training_state: the current training state.
transitions: a sample of transitions from the replay buffer.
key: a random key to handle stochastic operations.
Returns:
New alpha params, optimizer state, and loss.
"""
if not self._config.fix_alpha:
# update alpha
alpha_loss, alpha_gradient = jax.value_and_grad(sac_alpha_loss_fn)(
training_state.alpha_params,
policy_fn=self._policy.apply,
parametric_action_distribution=self._parametric_action_distribution,
action_size=self._action_size,
policy_params=training_state.policy_params,
transitions=transitions,
key=key,
)
alpha_optimizer = optax.adam(learning_rate=alpha_lr)
(
alpha_updates,
alpha_optimizer_state,
) = alpha_optimizer.update(
alpha_gradient, training_state.alpha_optimizer_state
)
alpha_params = optax.apply_updates(
training_state.alpha_params, alpha_updates
)
else:
alpha_params = training_state.alpha_params
alpha_optimizer_state = training_state.alpha_optimizer_state
alpha_loss = jnp.array(0.0)
return alpha_params, alpha_optimizer_state, alpha_loss
def _update_critic(
self,
critic_lr: float,
reward_scaling: float,
discount: float,
training_state: SacTrainingState,
transitions: Transition,
key: RNGKey,
) -> Tuple[Params, Params, optax.OptState, jax.Array]:
"""Updates the critic following the method described in the
Soft Actor Critic paper.
Args:
critic_lr: critic learning rate
reward_scaling: coefficient to scale rewards
discount: discount factor
training_state: the current training state.
transitions: a batch of transitions sampled from the replay buffer.
key: a random key to handle stochastic operations.
Returns:
New parameters of the critic and its target. New optimizer state,
loss and a new random key.
"""
# update critic
key, subkey = jax.random.split(key)
critic_loss, critic_gradient = jax.value_and_grad(sac_critic_loss_fn)(
training_state.critic_params,
policy_fn=self._policy.apply,
critic_fn=self._critic.apply,
parametric_action_distribution=self._parametric_action_distribution,
reward_scaling=reward_scaling,
discount=discount,
policy_params=training_state.policy_params,
target_critic_params=training_state.target_critic_params,
alpha=jnp.exp(training_state.alpha_params),
transitions=transitions,
key=subkey,
)
critic_optimizer = optax.adam(learning_rate=critic_lr)
(
critic_updates,
critic_optimizer_state,
) = critic_optimizer.update(
critic_gradient, training_state.critic_optimizer_state
)
critic_params = optax.apply_updates(
training_state.critic_params, critic_updates
)
target_critic_params = jax.tree.map(
lambda x1, x2: (1.0 - self._config.tau) * x1 + self._config.tau * x2,
training_state.target_critic_params,
critic_params,
)
return (
critic_params,
target_critic_params,
critic_optimizer_state,
critic_loss,
)
def _update_actor(
self,
policy_lr: float,
training_state: SacTrainingState,
transitions: Transition,
key: RNGKey,
) -> Tuple[Params, optax.OptState, jax.Array]:
"""Updates the actor parameters following the stochastic
policy gradient theorem with the method introduced in SAC.
Args:
policy_lr: policy learning rate
training_state: the current training state.
transitions: a batch of transitions sampled from the replay
buffer.
key: a random key to handle stochastic operations.
Returns:
New params and optimizer state. Current loss.
"""
policy_loss, policy_gradient = jax.value_and_grad(sac_policy_loss_fn)(
training_state.policy_params,
policy_fn=self._policy.apply,
critic_fn=self._critic.apply,
parametric_action_distribution=self._parametric_action_distribution,
critic_params=training_state.critic_params,
alpha=jnp.exp(training_state.alpha_params),
transitions=transitions,
key=key,
)
policy_optimizer = optax.adam(learning_rate=policy_lr)
(
policy_updates,
policy_optimizer_state,
) = policy_optimizer.update(
policy_gradient, training_state.policy_optimizer_state
)
policy_params = optax.apply_updates(
training_state.policy_params, policy_updates
)
return policy_params, policy_optimizer_state, policy_loss
def update(
self,
training_state: SacTrainingState,
replay_buffer: ReplayBuffer,
) -> Tuple[SacTrainingState, ReplayBuffer, Metrics]:
"""Performs a training step to update the policy and the critic parameters.
Args:
training_state: the current SAC training state
replay_buffer: the replay buffer
Returns:
the updated SAC training state
the replay buffer
the training metrics
"""
# sample a batch of transitions in the buffer
key = training_state.key
key, subkey = jax.random.split(key)
transitions = replay_buffer.sample(
subkey,
sample_size=self._config.batch_size,
)
# normalise observations if necessary
if self._config.normalize_observations:
normalization_running_stats = training_state.normalization_running_stats
normalized_obs = normalize_with_rmstd(
transitions.obs, normalization_running_stats
)
normalized_next_obs = normalize_with_rmstd(
transitions.next_obs, normalization_running_stats
)
transitions = transitions.replace(
obs=normalized_obs, next_obs=normalized_next_obs
)
# update alpha
key, subkey = jax.random.split(key)
(
alpha_params,
alpha_optimizer_state,
alpha_loss,
) = self._update_alpha(
alpha_lr=self._config.learning_rate,
training_state=training_state,
transitions=transitions,
key=subkey,
)
# update critic
key, subkey = jax.random.split(key)
(
critic_params,
target_critic_params,
critic_optimizer_state,
critic_loss,
) = self._update_critic(
critic_lr=self._config.learning_rate,
reward_scaling=self._config.reward_scaling,
discount=self._config.discount,
training_state=training_state,
transitions=transitions,
key=subkey,
)
# update actor
key, subkey = jax.random.split(key)
(
policy_params,
policy_optimizer_state,
policy_loss,
) = self._update_actor(
policy_lr=self._config.learning_rate,
training_state=training_state,
transitions=transitions,
key=subkey,
)
# create new training state
key, subkey = jax.random.split(key)
new_training_state = SacTrainingState(
policy_optimizer_state=policy_optimizer_state,
policy_params=policy_params,
critic_optimizer_state=critic_optimizer_state,
critic_params=critic_params,
alpha_optimizer_state=alpha_optimizer_state,
alpha_params=alpha_params,
normalization_running_stats=training_state.normalization_running_stats,
target_critic_params=target_critic_params,
key=subkey,
steps=training_state.steps + 1,
)
metrics = {
"actor_loss": policy_loss,
"critic_loss": critic_loss,
"alpha_loss": alpha_loss,
"obs_mean": jnp.mean(transitions.obs),
"obs_std": jnp.std(transitions.obs),
}
return new_training_state, replay_buffer, metrics
|