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
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 | class DADS(SAC):
"""Implements DADS algorithm https://arxiv.org/abs/1907.01657.
Note that the functions select_action, _update_alpha, _update_critic and
_update_actor are inherited from SAC algorithm.
In the current implementation, we suppose that the skills are fixed one
hot vectors, and do not support continuous skills at the moment.
Also, we suppose that the skills are evaluated in parallel in a fixed
manner: a batch of environments, containing a multiple of the number
of skills, is used to evaluate the skills in the environment and hence
to generate transitions. The sampling is hence fixed and perfectly uniform.
We plan to add continuous skill as an option in the future. We also plan
to release the current constraint on the number of batched environments
by sampling from the skills rather than having this fixed setting.
"""
def __init__(self, config: DadsConfig, action_size: int, descriptor_size: int):
self._config: DadsConfig = config
if self._config.normalize_observations:
raise NotImplementedError("Normalization in not implemented for DADS yet")
# define the networks
self._policy, self._critic, self._dynamics = make_dads_networks(
action_size=action_size,
descriptor_size=descriptor_size,
omit_input_dynamics_dim=config.omit_input_dynamics_dim,
policy_hidden_layer_size=config.policy_hidden_layer_size,
critic_hidden_layer_size=config.critic_hidden_layer_size,
)
# define the action distribution
self._action_size = action_size
self._parametric_action_distribution = NormalTanhDistribution(
event_size=action_size
)
self._sample_action_fn = self._parametric_action_distribution.sample
# define the losses
(
self._alpha_loss_fn,
self._policy_loss_fn,
self._critic_loss_fn,
self._dynamics_loss_fn,
) = make_dads_loss_fn(
policy_fn=self._policy.apply,
critic_fn=self._critic.apply,
dynamics_fn=self._dynamics.apply,
reward_scaling=self._config.reward_scaling,
discount=self._config.discount,
action_size=action_size,
num_skills=self._config.num_skills,
parametric_action_distribution=self._parametric_action_distribution,
)
# define the optimizers
self._policy_optimizer = optax.adam(learning_rate=self._config.learning_rate)
self._critic_optimizer = optax.adam(learning_rate=self._config.learning_rate)
self._alpha_optimizer = optax.adam(learning_rate=self._config.learning_rate)
self._dynamics_optimizer = optax.adam(learning_rate=self._config.learning_rate)
def init( # type: ignore
self,
key: RNGKey,
action_size: int,
observation_size: int,
descriptor_size: int,
) -> DadsTrainingState:
"""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
descriptor_size: the size of the environment's descriptor space (i.e. the
dimension of the dynamics network's input)
Returns:
the initial training state of DADS
"""
# Initialize params
dummy_obs = jnp.zeros((1, observation_size + self._config.num_skills))
dummy_action = jnp.zeros((1, action_size))
dummy_dyn_obs = jnp.zeros((1, descriptor_size))
dummy_skill = jnp.zeros((1, self._config.num_skills))
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
)
key, subkey = jax.random.split(key)
dynamics_params = self._dynamics.init(
subkey,
obs=dummy_dyn_obs,
skill=dummy_skill,
target=dummy_dyn_obs,
)
policy_optimizer_state = self._policy_optimizer.init(policy_params)
critic_optimizer_state = self._critic_optimizer.init(critic_params)
dynamics_optimizer_state = self._dynamics_optimizer.init(dynamics_params)
log_alpha = jnp.asarray(jnp.log(self._config.alpha_init), dtype=jnp.float32)
alpha_optimizer_state = self._alpha_optimizer.init(log_alpha)
return DadsTrainingState(
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,
dynamics_optimizer_state=dynamics_optimizer_state,
dynamics_params=dynamics_params,
key=key,
normalization_running_stats=RunningMeanStdState(
mean=jnp.zeros(
descriptor_size,
),
var=jnp.ones(
descriptor_size,
),
count=jnp.zeros(()),
),
steps=jnp.array(0),
)
def _compute_diversity_reward(
self, transition: QDTransition, training_state: DadsTrainingState
) -> Reward:
"""Computes the diversity reward of DADS.
Args:
transition: a batch of transitions from the replay buffer
training_state: the current training state
Returns:
the diversity reward
"""
active_skills = transition.obs[:, -self._config.num_skills :]
# Compute dynamics prob
next_state_desc = transition.next_state_desc
state_desc = transition.state_desc
target = next_state_desc - state_desc
if self._config.normalize_target:
target = normalize_with_rmstd(
target, training_state.normalization_running_stats
)
log_q_phi = self._dynamics.apply(
training_state.dynamics_params,
state_desc,
active_skills,
target,
)
# Estimate prior skill
skill_samples = jnp.tile(
jnp.eye(self._config.num_skills), (state_desc.shape[0], 1)
)
state_descriptors = jnp.repeat(state_desc, self._config.num_skills, axis=0)
target = jnp.repeat(target, self._config.num_skills, axis=0)
log_p_s = self._dynamics.apply(
training_state.dynamics_params,
state_descriptors,
skill_samples,
target,
)
log_p_s = log_p_s.reshape((-1, self._config.num_skills))
# Compute the reward according to DADS official implementation
reward = jnp.log(self._config.num_skills) - jnp.log(
jnp.exp(jnp.clip(log_p_s - log_q_phi.reshape((-1, 1)), -50, 50)).sum(axis=1)
)
return reward
def play_step_fn( # type: ignore
self,
env_state: EnvState,
training_state: DadsTrainingState,
env: Env,
skills: Skill,
deterministic: bool = False,
evaluation: bool = False,
) -> Tuple[EnvState, DadsTrainingState, QDTransition]:
"""Plays a step in the environment. Concatenates skills to the observation
vector, selects an action according to SAC rule and performs the environment
step.
Args:
env_state: the current environment state
training_state: the DIAYN training state
skills: the skills concatenated to the observation vector
env: the environment
deterministic: the whether or not 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 DADS training state
the played transition
"""
key = training_state.key
policy_params = training_state.policy_params
obs = jnp.concatenate([env_state.obs, skills], axis=1)
# If the env does not support state descriptor, we set it to (0,0)
if "state_descriptor" in env_state.info:
state_desc = env_state.info["state_descriptor"]
else:
state_desc = jnp.zeros((env_state.obs.shape[0], 2))
key, subkey = jax.random.split(key)
actions = self.select_action(
obs=obs,
policy_params=policy_params,
key=subkey,
deterministic=deterministic,
)
next_env_state = env.step(env_state, actions)
next_obs = jnp.concatenate([next_env_state.obs, skills], axis=1)
if "state_descriptor" in next_env_state.info:
next_state_desc = next_env_state.info["state_descriptor"]
else:
next_state_desc = jnp.zeros((next_env_state.obs.shape[0], 2))
if self._config.normalize_target:
if self._config.descriptor_full_state:
_state_desc = obs[:, : -self._config.num_skills]
_next_state_desc = next_obs[:, : -self._config.num_skills]
target = _next_state_desc - _state_desc
else:
target = next_state_desc - state_desc
target *= jnp.expand_dims(1 - next_env_state.done, -1)
normalization_running_stats = update_running_mean_std(
training_state.normalization_running_stats, target
)
else:
normalization_running_stats = training_state.normalization_running_stats
truncations = next_env_state.info["truncation"]
transition = QDTransition(
obs=obs,
next_obs=next_obs,
state_desc=state_desc,
next_state_desc=next_state_desc,
rewards=next_env_state.reward,
dones=next_env_state.done,
actions=actions,
truncations=truncations,
)
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,
)
return next_env_state, training_state, transition
def eval_policy_fn( # type: ignore
self,
training_state: DadsTrainingState,
eval_env_first_state: EnvState,
play_step_fn: Callable[
[EnvState, Params],
Tuple[EnvState, Params, QDTransition],
],
env_batch_size: int,
) -> Tuple[Reward, Reward, Reward, StateDescriptor]:
"""Evaluates the agent's policy over an entire episode, across all batched
environments.
Args:
training_state: the DADS training state
eval_env_first_state: the initial state for evaluation
play_step_fn: the play_step function used to collect the evaluation episode
env_batch_size: the number of environments we play simultaneously
Returns:
true return averaged over batch dimension, shape: (1,)
true return per environment, shape: (env_batch_size,)
diversity return per environment, shape: (env_batch_size,)
state descriptors, shape: (episode_length, env_batch_size, descriptor_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)
reshaped_transitions = jax.tree.map(
lambda x: x.reshape((self._config.episode_length * env_batch_size, -1)),
transitions,
)
if self._config.descriptor_full_state:
state_desc = reshaped_transitions.obs[:, : -self._config.num_skills]
next_state_desc = reshaped_transitions.next_obs[
:, : -self._config.num_skills
]
reshaped_transitions = reshaped_transitions.replace(
state_desc=state_desc, next_state_desc=next_state_desc
)
diversity_rewards = self._compute_diversity_reward(
transition=reshaped_transitions, training_state=training_state
).reshape((self._config.episode_length, env_batch_size))
diversity_returns = jnp.nansum(diversity_rewards, axis=0)
return true_return, true_returns, diversity_returns, transitions.state_desc
def _compute_reward(
self, transition: QDTransition, training_state: DadsTrainingState
) -> Reward:
"""Computes the reward to train the networks.
Args:
transition: a batch of transitions from the replay buffer
training_state: the current training state
Returns:
the DADS diversity reward
"""
return self._compute_diversity_reward(
transition=transition, training_state=training_state
)
def _update_dynamics(
self, operand: Tuple[DadsTrainingState, QDTransition]
) -> Tuple[Params, float, optax.OptState]:
"""Update the dynamics network, independently of other networks. Called every
`dynamics_update_freq` training steps.
"""
training_state, transitions = operand
dynamics_loss, dynamics_gradient = jax.value_and_grad(
self._dynamics_loss_fn,
)(
training_state.dynamics_params,
transitions=transitions,
)
(
dynamics_updates,
dynamics_optimizer_state,
) = self._dynamics_optimizer.update(
dynamics_gradient, training_state.dynamics_optimizer_state
)
dynamics_params = optax.apply_updates(
training_state.dynamics_params, dynamics_updates
)
return (
dynamics_params,
dynamics_loss,
dynamics_optimizer_state,
)
def _not_update_dynamics(
self, operand: Tuple[DadsTrainingState, QDTransition]
) -> Tuple[Params, float, optax.OptState]:
"""Fake update of the dynamics, called every time we don't want to update
the dynamics while we update the other networks.
"""
training_state, _transitions = operand
return (
training_state.dynamics_params,
jnp.nan,
training_state.dynamics_optimizer_state,
)
def _update_networks(
self,
training_state: DadsTrainingState,
transitions: QDTransition,
) -> Tuple[DadsTrainingState, Metrics]:
"""Updates the networks involved in DADS.
Args:
training_state: the current training state of the algorithm.
transitions: transitions sampled from a replay buffer.
key: a random key to handle stochasticity.
Returns:
The updated training state and training metrics.
"""
key = training_state.key
# Update skill-dynamics
(
dynamics_params,
dynamics_loss,
dynamics_optimizer_state,
) = jax.lax.cond(
training_state.steps % self._config.dynamics_update_freq == 0,
self._update_dynamics,
self._not_update_dynamics,
(training_state, transitions),
)
# 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 = DadsTrainingState(
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,
target_critic_params=target_critic_params,
dynamics_optimizer_state=dynamics_optimizer_state,
dynamics_params=dynamics_params,
key=subkey,
normalization_running_stats=training_state.normalization_running_stats,
steps=training_state.steps + 1,
)
metrics = {
"actor_loss": policy_loss,
"critic_loss": critic_loss,
"dynamics_loss": dynamics_loss,
"alpha_loss": alpha_loss,
"alpha": jnp.exp(alpha_params),
"training_observed_reward_mean": jnp.mean(transitions.rewards),
"target_mean": jnp.mean(transitions.next_state_desc),
"target_std": jnp.std(transitions.next_state_desc),
}
return new_training_state, metrics
def update(
self,
training_state: DadsTrainingState,
replay_buffer: ReplayBuffer,
) -> Tuple[DadsTrainingState, ReplayBuffer, Metrics]:
"""Performs a training step to update the policy, the critic and the
dynamics network parameters.
Args:
training_state: the current DADS training state
replay_buffer: the replay buffer
Returns:
the updated DIAYN 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,
)
# Optionally replace the state descriptor by the observation
if self._config.descriptor_full_state:
_state_desc = transitions.obs[:, : -self._config.num_skills]
_next_state_desc = transitions.next_obs[:, : -self._config.num_skills]
transitions = transitions.replace(
state_desc=_state_desc, next_state_desc=_next_state_desc
)
# Compute the reward
rewards = self._compute_reward(
transition=transitions, training_state=training_state
)
# Compute the target and optionally normalize it for the training
if self._config.normalize_target:
next_state_desc = normalize_with_rmstd(
transitions.next_state_desc - transitions.state_desc,
training_state.normalization_running_stats,
)
else:
next_state_desc = transitions.next_state_desc - transitions.state_desc
# Update the transitions
transitions = transitions.replace(
next_state_desc=next_state_desc, rewards=rewards
)
new_training_state, metrics = self._update_networks(
training_state, transitions=transitions
)
return new_training_state, replay_buffer, metrics
|