in run_exp.py [0:0]
def learn(actor_model,
model,
batch,
optimizer,
scheduler,
flags,
lock=threading.Lock()):
"""Performs a learning (optimization) step."""
with lock:
learner_outputs = model(batch)
# Use last baseline value (from the value function) to bootstrap.
bootstrap_value = learner_outputs['baseline'][-1]
# At this point, the environment outputs at time step `t` are the inputs
# that lead to the learner_outputs at time step `t`. After the following
# shifting, the actions in actor_batch and learner_outputs at time
# step `t` is what leads to the environment outputs at time step `t`.
batch = {key: tensor[1:] for key, tensor in batch.items()}
learner_outputs = {
key: tensor[:-1]
for key, tensor in learner_outputs.items()
}
rewards = batch['reward']
if flags.reward_clipping == 'abs_one':
clipped_rewards = torch.clamp(rewards, -1, 1)
elif flags.reward_clipping == 'soft_asymmetric':
squeezed = torch.tanh(rewards / 5.0)
# Negative rewards are given less weight than positive rewards.
clipped_rewards = torch.where(rewards < 0, 0.3 * squeezed,
squeezed) * 5.0
elif flags.reward_clipping == 'none':
clipped_rewards = rewards
discounts = (~batch['done']).float() * flags.discounting
# This could be in C++. In TF, this is actually slower on the GPU.
vtrace_returns = vtrace.from_logits(
behavior_policy_logits=batch['policy_logits'],
target_policy_logits=learner_outputs['policy_logits'],
actions=batch['action'],
discounts=discounts,
rewards=clipped_rewards,
values=learner_outputs['baseline'],
bootstrap_value=bootstrap_value)
# Compute loss as a weighted sum of the baseline loss, the policy
# gradient loss and an entropy regularization term.
pg_loss = compute_policy_gradient_loss(learner_outputs['policy_logits'],
batch['action'],
vtrace_returns.pg_advantages)
baseline_loss = flags.baseline_cost * compute_baseline_loss(
vtrace_returns.vs - learner_outputs['baseline'])
entropy_loss = flags.entropy_cost * compute_entropy_loss(
learner_outputs['policy_logits'])
aux_loss = learner_outputs['aux_loss'][0]
total_loss = pg_loss + baseline_loss + entropy_loss + aux_loss
episode_returns = batch['episode_return'][batch['done']]
episode_lens = batch['episode_step'][batch['done']]
won = batch['reward'][batch['done']] > 0.8
stats = {
'mean_win_rate': torch.mean(won.float()).item(),
'mean_episode_len': torch.mean(episode_lens.float()).item(),
'mean_episode_return': torch.mean(episode_returns).item(),
'total_loss': total_loss.item(),
'pg_loss': pg_loss.item(),
'baseline_loss': baseline_loss.item(),
'entropy_loss': entropy_loss.item(),
'aux_loss': aux_loss.item(),
}
optimizer.zero_grad()
model.zero_grad()
total_loss.backward()
nn.utils.clip_grad_norm_(model.parameters(), 40.0)
optimizer.step()
scheduler.step()
# Interestingly, this doesn't require moving off cuda first?
actor_model.load_state_dict(model.state_dict())
return stats