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
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 | class DIAYN(SAC):
"""Implements DIAYN algorithm https://arxiv.org/abs/1802.06070.
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.
Since we are using categorical skills, the current loss function used
to train the discriminator is the categorical cross entropy loss.
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: DiaynConfig, action_size: int):
self._config: DiaynConfig = config
if self._config.normalize_observations:
raise NotImplementedError("Normalization is not implemented for DIAYN yet")
# define the networks
self._policy, self._critic, self._discriminator = make_diayn_networks(
num_skills=self._config.num_skills,
action_size=action_size,
policy_hidden_layer_size=self._config.policy_hidden_layer_size,
critic_hidden_layer_size=self._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._discriminator_loss_fn,
) = make_diayn_loss_fn(
policy_fn=self._policy.apply,
critic_fn=self._critic.apply,
discriminator_fn=self._discriminator.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._discriminator_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,
) -> DiaynTrainingState:
"""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 discriminator's input)
Returns:
the initial training state of DIAYN
"""
# define policy and critic params
dummy_obs = jnp.zeros((1, observation_size + self._config.num_skills))
dummy_action = jnp.zeros((1, action_size))
dummy_discriminator_obs = jnp.zeros((1, descriptor_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
)
key, subkey = jax.random.split(key)
discriminator_params = self._discriminator.init(
subkey, obs=dummy_discriminator_obs
)
policy_optimizer_state = self._policy_optimizer.init(policy_params)
critic_optimizer_state = self._critic_optimizer.init(critic_params)
discriminator_optimizer_state = self._discriminator_optimizer.init(
discriminator_params
)
log_alpha = jnp.asarray(jnp.log(self._config.alpha_init), dtype=jnp.float32)
alpha_optimizer_state = self._alpha_optimizer.init(log_alpha)
return DiaynTrainingState(
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,
discriminator_optimizer_state=discriminator_optimizer_state,
discriminator_params=discriminator_params,
key=key,
steps=jnp.array(0),
)
def _compute_diversity_reward(
self,
transition: QDTransition,
discriminator_params: Params,
add_log_p_z: bool = True,
) -> Reward:
"""Computes the diversity reward of DIAYN.
As we use discrete skills (one hot encoded vectors), the categorical
cross entropy is used here.
Args:
transition: a batch of transitions from the replay buffer
discriminator_params: the parameters of the discriminator
add_log_p_z: whether or not to add (minus) the probability of the skills'
prior distribution. Defaults to True.
Returns:
the diversity reward
"""
next_state_desc = transition.next_state_desc
skills = transition.next_obs[:, -self._config.num_skills :]
reward = jnp.sum(
jax.nn.log_softmax(
self._discriminator.apply(discriminator_params, next_state_desc)
)
* skills,
axis=1,
)
if add_log_p_z:
reward += jnp.log(self._config.num_skills)
return reward
def play_step_fn( # type: ignore
self,
env_state: EnvState,
training_state: DiaynTrainingState,
skills: Skill,
env: Env,
deterministic: bool = False,
) -> Tuple[EnvState, DiaynTrainingState, 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: whether or not to select action in a deterministic way.
Defaults to False.
Returns:
the new environment state
the new DIAYN 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))
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,
)
training_state = training_state.replace(key=key)
return next_env_state, training_state, transition
def eval_policy_fn( # type: ignore
self,
training_state: DiaynTrainingState,
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 DIAYN 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,
discriminator_params=training_state.discriminator_params,
).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: DiaynTrainingState
) -> 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 DIAYN diversity reward
"""
return self._compute_diversity_reward(
transition=transition,
discriminator_params=training_state.discriminator_params,
add_log_p_z=True,
)
def _update_networks(
self,
training_state: DiaynTrainingState,
transitions: QDTransition,
) -> Tuple[DiaynTrainingState, Metrics]:
"""Updates all the networks of the training state.
Args:
training_state: the current training state.
transitions: transitions sampled from the replay buffer.
key: a random key to handle stochastic operations.
Returns:
The update training state, metrics and a new random key.
"""
key = training_state.key
# Compute discriminator loss and gradients
discriminator_loss, discriminator_gradient = jax.value_and_grad(
self._discriminator_loss_fn
)(
training_state.discriminator_params,
transitions=transitions,
)
# update discriminator
(
discriminator_updates,
discriminator_optimizer_state,
) = self._discriminator_optimizer.update(
discriminator_gradient, training_state.discriminator_optimizer_state
)
discriminator_params = optax.apply_updates(
training_state.discriminator_params, discriminator_updates
)
# 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 = DiaynTrainingState(
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,
discriminator_optimizer_state=discriminator_optimizer_state,
discriminator_params=discriminator_params,
key=subkey,
steps=training_state.steps + 1,
)
metrics = {
"actor_loss": policy_loss,
"critic_loss": critic_loss,
"discriminator_loss": discriminator_loss,
"alpha_loss": alpha_loss,
}
return new_training_state, metrics
def update(
self,
training_state: DiaynTrainingState,
replay_buffer: ReplayBuffer,
) -> Tuple[DiaynTrainingState, ReplayBuffer, Metrics]:
"""Performs a training step to update the policy, the critic and the
discriminator parameters.
Args:
training_state: the current DIAYN 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 rewards and replace transitions
rewards = self._compute_reward(transitions, training_state)
transitions = transitions.replace(rewards=rewards)
# update params of networks in the training state
new_training_state, metrics = self._update_networks(
training_state, transitions=transitions
)
return new_training_state, replay_buffer, metrics
|