gpt4 book ai didi

python - tf.keras 中的 A2C 算法 : actor loss function

转载 作者:行者123 更新时间:2023-12-01 06:37:54 25 4
gpt4 key购买 nike

我正在学习 Action-Critic 强化学习技术,特别是 A2C 算法。

我找到了该算法简单版本的良好描述(即没有经验重播、批处理或其他技巧),并在此处实现:https://link.medium.com/yi55uKWwV2 。该文章的完整代码是 available on GitHub .

我认为我了解这里发生的事情,但为了确保我确实了解,我正在尝试使用更高级别的 tf.keras API 从头开始​​重新实现它。我遇到的问题是如何正确实现训练循环,以及如何制定 Actor 的损失函数。

  1. 将行动和优势传递到损失函数中的正确方法是什么?
  2. Actor 的损失函数涉及计算在正态分布下采取的行动的概率。如何确保损失函数计算过程中正态分布的 mu 和 sigma 与预测过程中的 mu 和 sigma 实际匹配?
  3. 与原始版本一样, Actor 的损失函数不关心 y_pred,它只关心与环境交互时选择的 Action 。这似乎是错误的,但我不确定如何。

我目前拥有的代码:https://gist.github.com/nevkontakte/beb59f29e0a8152d99003852887e7de7

编辑:我想我的一些困惑源于对 Keras/TensorFlow 中梯度计算背后的魔力缺乏理解,因此任何指针都将不胜感激。

最佳答案

首先,值得赞扬的是:ralf htpSimon 提供的信息有助于帮助我最终找到正确的答案。

在详细回答我自己的问题之前,here's the original code我试图用 tf.keras 术语重写,并且 here's my result .

在 Keras 中将 Action 和优势传递给损失函数的正确方法是什么?

原始 TF 优化器所认为的损失函数与 Keras 所认为的损失函数之间存在差异。直接使用优化器时,它只需要一个张量(惰性张量或急切张量,具体取决于您的配置),该张量将在 tf.GradientTape() 下进行评估,以计算梯度和更新权重。

示例来自 https://medium.com/@asteinbach/actor-critic-using-deep-rl-continuous-mountain-car-in-tensorflow-4c1fb2110f7c :

# Below norm_dist is the output tensor of the neural network we are training.
loss_actor = -tfc.log(norm_dist.prob(action_placeholder) + 1e-5) * delta_placeholder
training_op_actor = tfc.train.AdamOptimizer(
lr_actor, name='actor_optimizer').minimize(loss_actor)

# Later, in the training loop...

_, loss_actor_val = sess.run([training_op_actor, loss_actor],
feed_dict={action_placeholder: np.squeeze(action),
state_placeholder: scale_state(state),
delta_placeholder: td_error})

在此示例中,它计算整个图,包括进行推理、捕获梯度和调整权重。因此,要将所需的任何值传递到损失函数/梯度计算中,只需将必要的值传递到计算图中即可。

Keras有点more formal损失函数应该是什么样子:

loss: String (name of objective function), objective function or tf.keras.losses.Loss instance. See tf.keras.losses. An objective function is any callable with the signature scalar_loss = fn(y_true, y_pred). If the model has multiple outputs, you can use a different loss on each output by passing a dictionary or a list of losses. The loss value that will be minimized by the model will then be the sum of all individual losses.

Keras 将为您进行推理(前向传递)并将输出传递到损失函数中。损失函数应该对预测值和 y_true 标签进行一些额外的计算,并返回结果。为了梯度计算的目的,整个过程将被跟踪。

虽然这对于传统训练来说非常方便,但是当我们想要传递一些额外的数据(例如 TD 误差)时,这有点限制。可以解决这个问题,将所有额外的数据放入 y_true 中,然后在损失函数中将其分解(我在网络上的某个地方找到了这个技巧,但不幸的是丢失了源链接)。

这是我最后重写上面的方法:

def loss(y_true, y_pred):
action_true = y_true[:, :n_outputs]
advantage = y_true[:, n_outputs:]
return -tfc.log(y_pred.prob(action_true) + 1e-5) * advantage

# Below, in the training loop...

# A trick to pass TD error *and* actual action to the loss function: join them into a tensor and split apart
# Inside the loss function.
annotated_action = tf.concat([action, td_error], axis=1)
actor_model.train_on_batch([scale_state(state)], [annotated_action])

Actor 的损失函数涉及计算在正态分布下采取的行动的概率。如何确保损失函数计算期间正态分布的 mu 和 sigma 实际上与预测期间的相符?

当我问这个问题时,我不太了解 TF 计算图的工作原理。所以答案很简单:每次调用 sess.run() 时,它都必须从头开始计算整个图。只要图形输入(例如观察到的状态)和神经网络权重相同(或相似),分布的参数就会相同(或相似)。

按照原来的方式, Actor 的损失函数不关心 y_pred,它只关心与环境交互时选择的 Action 。这似乎是错误的,但我不确定如何。

错误的是“ Actor 的损失函数不关心 y_pred”的假设:) Actor 的损失函数涉及 norm_dist (即 Action 概率分布),它实际上是 的模拟>y_pred 在这种情况下。

关于python - tf.keras 中的 A2C 算法 : actor loss function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59585026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com