2019-05-20 16:46:05 -07:00
|
|
|
"""Note: Keep in sync with changes to VTraceTFPolicy."""
|
2018-08-01 20:53:53 -07:00
|
|
|
|
2018-06-25 22:33:57 -07:00
|
|
|
import ray
|
2021-01-19 14:22:36 +01:00
|
|
|
from ray.rllib.agents.ppo.ppo_tf_policy import ValueNetworkMixin
|
2019-05-20 16:46:05 -07:00
|
|
|
from ray.rllib.policy.sample_batch import SampleBatch
|
2021-01-19 14:22:36 +01:00
|
|
|
from ray.rllib.evaluation.postprocessing import compute_gae_for_sample_batch, \
|
2019-03-29 12:44:23 -07:00
|
|
|
Postprocessing
|
2019-06-02 14:14:31 +08:00
|
|
|
from ray.rllib.policy.tf_policy_template import build_tf_policy
|
|
|
|
from ray.rllib.policy.tf_policy import LearningRateSchedule
|
2021-01-19 14:22:36 +01:00
|
|
|
from ray.rllib.utils.deprecation import deprecation_warning
|
2020-06-16 08:52:20 +02:00
|
|
|
from ray.rllib.utils.framework import try_import_tf
|
2021-01-19 14:22:36 +01:00
|
|
|
from ray.rllib.utils.tf_ops import explained_variance
|
2019-05-10 20:36:18 -07:00
|
|
|
|
2020-06-30 10:13:20 +02:00
|
|
|
tf1, tf, tfv = try_import_tf()
|
2018-06-09 00:21:35 -07:00
|
|
|
|
|
|
|
|
2021-01-19 14:22:36 +01:00
|
|
|
def postprocess_advantages(policy,
|
|
|
|
sample_batch,
|
|
|
|
other_agent_batches=None,
|
|
|
|
episode=None):
|
|
|
|
|
|
|
|
# Stub serving backward compatibility.
|
|
|
|
deprecation_warning(
|
|
|
|
old="rllib.agents.a3c.a3c_tf_policy.postprocess_advantages",
|
|
|
|
new="rllib.evaluation.postprocessing.compute_gae_for_sample_batch",
|
|
|
|
error=False)
|
|
|
|
|
|
|
|
return compute_gae_for_sample_batch(policy, sample_batch,
|
|
|
|
other_agent_batches, episode)
|
|
|
|
|
|
|
|
|
2020-01-02 17:42:13 -08:00
|
|
|
class A3CLoss:
|
2018-07-19 15:30:36 -07:00
|
|
|
def __init__(self,
|
|
|
|
action_dist,
|
|
|
|
actions,
|
|
|
|
advantages,
|
|
|
|
v_target,
|
|
|
|
vf,
|
|
|
|
vf_loss_coeff=0.5,
|
2019-03-17 18:07:37 -07:00
|
|
|
entropy_coeff=0.01):
|
2018-06-26 13:17:15 -07:00
|
|
|
log_prob = action_dist.logp(actions)
|
2018-06-09 00:21:35 -07:00
|
|
|
|
2018-06-26 13:17:15 -07:00
|
|
|
# The "policy gradients" loss
|
2018-07-19 15:30:36 -07:00
|
|
|
self.pi_loss = -tf.reduce_sum(log_prob * advantages)
|
2018-06-09 00:21:35 -07:00
|
|
|
|
2018-06-26 13:17:15 -07:00
|
|
|
delta = vf - v_target
|
2020-06-25 19:01:32 +02:00
|
|
|
self.vf_loss = 0.5 * tf.reduce_sum(tf.math.square(delta))
|
2018-06-26 13:17:15 -07:00
|
|
|
self.entropy = tf.reduce_sum(action_dist.entropy())
|
2019-03-17 18:07:37 -07:00
|
|
|
self.total_loss = (self.pi_loss + self.vf_loss * vf_loss_coeff -
|
2018-06-26 13:17:15 -07:00
|
|
|
self.entropy * entropy_coeff)
|
2018-06-09 00:21:35 -07:00
|
|
|
|
|
|
|
|
2019-08-23 02:21:11 -04:00
|
|
|
def actor_critic_loss(policy, model, dist_class, train_batch):
|
|
|
|
model_out, _ = model.from_batch(train_batch)
|
|
|
|
action_dist = dist_class(model_out, model)
|
|
|
|
policy.loss = A3CLoss(action_dist, train_batch[SampleBatch.ACTIONS],
|
|
|
|
train_batch[Postprocessing.ADVANTAGES],
|
|
|
|
train_batch[Postprocessing.VALUE_TARGETS],
|
|
|
|
model.value_function(),
|
|
|
|
policy.config["vf_loss_coeff"],
|
|
|
|
policy.config["entropy_coeff"])
|
2019-06-02 14:14:31 +08:00
|
|
|
return policy.loss.total_loss
|
|
|
|
|
|
|
|
|
|
|
|
def add_value_function_fetch(policy):
|
2019-08-23 02:21:11 -04:00
|
|
|
return {SampleBatch.VF_PREDS: policy.model.value_function()}
|
2019-06-02 14:14:31 +08:00
|
|
|
|
|
|
|
|
2019-08-23 02:21:11 -04:00
|
|
|
def stats(policy, train_batch):
|
2019-06-02 14:14:31 +08:00
|
|
|
return {
|
2019-07-21 12:27:17 -07:00
|
|
|
"cur_lr": tf.cast(policy.cur_lr, tf.float64),
|
2019-06-02 14:14:31 +08:00
|
|
|
"policy_loss": policy.loss.pi_loss,
|
|
|
|
"policy_entropy": policy.loss.entropy,
|
2020-06-25 19:01:32 +02:00
|
|
|
"var_gnorm": tf.linalg.global_norm(
|
|
|
|
list(policy.model.trainable_variables())),
|
2019-06-02 14:14:31 +08:00
|
|
|
"vf_loss": policy.loss.vf_loss,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-23 02:21:11 -04:00
|
|
|
def grad_stats(policy, train_batch, grads):
|
2019-06-02 14:14:31 +08:00
|
|
|
return {
|
2020-06-25 19:01:32 +02:00
|
|
|
"grad_gnorm": tf.linalg.global_norm(grads),
|
2019-06-02 14:14:31 +08:00
|
|
|
"vf_explained_var": explained_variance(
|
2019-08-23 02:21:11 -04:00
|
|
|
train_batch[Postprocessing.VALUE_TARGETS],
|
|
|
|
policy.model.value_function()),
|
2019-06-02 14:14:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def clip_gradients(policy, optimizer, loss):
|
2019-08-23 02:21:11 -04:00
|
|
|
grads_and_vars = optimizer.compute_gradients(
|
|
|
|
loss, policy.model.trainable_variables())
|
|
|
|
grads = [g for (g, v) in grads_and_vars]
|
2019-06-02 14:14:31 +08:00
|
|
|
grads, _ = tf.clip_by_global_norm(grads, policy.config["grad_clip"])
|
2019-08-23 02:21:11 -04:00
|
|
|
clipped_grads = list(zip(grads, policy.model.trainable_variables()))
|
2019-06-02 14:14:31 +08:00
|
|
|
return clipped_grads
|
|
|
|
|
|
|
|
|
|
|
|
def setup_mixins(policy, obs_space, action_space, config):
|
2021-01-19 14:22:36 +01:00
|
|
|
ValueNetworkMixin.__init__(policy, obs_space, action_space, config)
|
2019-06-02 14:14:31 +08:00
|
|
|
LearningRateSchedule.__init__(policy, config["lr"], config["lr_schedule"])
|
|
|
|
|
|
|
|
|
|
|
|
A3CTFPolicy = build_tf_policy(
|
|
|
|
name="A3CTFPolicy",
|
|
|
|
get_default_config=lambda: ray.rllib.agents.a3c.a3c.DEFAULT_CONFIG,
|
|
|
|
loss_fn=actor_critic_loss,
|
|
|
|
stats_fn=stats,
|
|
|
|
grad_stats_fn=grad_stats,
|
|
|
|
gradients_fn=clip_gradients,
|
2021-01-19 14:22:36 +01:00
|
|
|
postprocess_fn=compute_gae_for_sample_batch,
|
2019-06-02 14:14:31 +08:00
|
|
|
extra_action_fetches_fn=add_value_function_fetch,
|
|
|
|
before_loss_init=setup_mixins,
|
|
|
|
mixins=[ValueNetworkMixin, LearningRateSchedule])
|