- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习 Action-Critic 强化学习技术,特别是 A2C 算法。
我找到了该算法简单版本的良好描述(即没有经验重播、批处理或其他技巧),并在此处实现:https://link.medium.com/yi55uKWwV2 。该文章的完整代码是 available on GitHub .
我认为我了解这里发生的事情,但为了确保我确实了解,我正在尝试使用更高级别的 tf.keras API 从头开始重新实现它。我遇到的问题是如何正确实现训练循环,以及如何制定 Actor 的损失函数。
我目前拥有的代码:https://gist.github.com/nevkontakte/beb59f29e0a8152d99003852887e7de7
编辑:我想我的一些困惑源于对 Keras/TensorFlow 中梯度计算背后的魔力缺乏理解,因此任何指针都将不胜感激。
最佳答案
首先,值得赞扬的是:ralf htp 和 Simon 提供的信息有助于帮助我最终找到正确的答案。
在详细回答我自己的问题之前,here's the original code我试图用 tf.keras 术语重写,并且 here's my result .
原始 TF 优化器所认为的损失函数与 Keras 所认为的损失函数之间存在差异。直接使用优化器时,它只需要一个张量(惰性张量或急切张量,具体取决于您的配置),该张量将在 tf.GradientTape() 下进行评估,以计算梯度和更新权重。
# 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])
当我问这个问题时,我不太了解 TF 计算图的工作原理。所以答案很简单:每次调用 sess.run() 时,它都必须从头开始计算整个图。只要图形输入(例如观察到的状态)和神经网络权重相同(或相似),分布的参数就会相同(或相似)。
错误的是“ Actor 的损失函数不关心 y_pred”的假设:) Actor 的损失函数涉及 norm_dist
(即 Action 概率分布),它实际上是 的模拟>y_pred
在这种情况下。
关于python - tf.keras 中的 A2C 算法 : actor loss function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59585026/
正如 Keras 手册中所述 -使用 Pyhton 进行深度学习-,对于多输出模型,我们需要为网络的不同头指定不同的损失函数。但由于梯度下降要求您最小化标量,因此您必须将这些损失组合成单个值才能训练模
我无法判断这个错误是由于技术错误还是超参数造成的,但我的 DC-GAN 的鉴别器损失一开始很低,然后逐渐攀升,在 8 左右减慢,而我的发电机损失则大幅下降。我在大约 60,000 epoch 时结束了
我对以下日志有点怀疑,这是我在训练深度神经网络时得到的回归目标值在 -1.0 和 1.0 之间,学习率为 0.001 和 19200/4800 训练/验证样本: ___________________
简而言之: 我训练了一个自动编码器,其验证损失始终高于其训练损失(见附图)。 我认为这是过度拟合的信号。然而,我的自动编码器在测试数据集上表现良好。我想知道是否: 1)引用下面提供的网络架构,任何人都
我正在处理文本序列,序列长度在 1-3 之间。标签是一个“分数”。我有超过 500 万个样本。我的网络如下所示(Keras): model.add(Embedding(word_count, 128,
我正在训练用于图像语义分割的 CNN U-net 模型,但是训练损失的减少速度似乎比验证损失的减少速度快得多,这正常吗? 我使用的是 0.002 的损失 下图可以看到训练和验证损失: 最佳答案 是的,
我目前正在通过研究 MNIST 示例等示例来学习卷积神经网络。在神经网络的训练过程中,我经常看到如下输出: Epoch | Train loss | Valid loss | Train
我设计了自己的损失函数。但是,当尝试恢复到训练期间遇到的最佳模型时 model = load_model("lc_model.h5") 我收到以下错误: -----------------------
在基于RAW套接字的数据包发送测试期间,我发现了非常令人讨厌的症状。 使用默认的RAW套接字设置(尤其是SO_SNDBUF大小), 原始套接字可以毫无问题地发送100,000个数据包,但大约花费了8秒
我用JAVA编写了以下方法: public static float surface(float r) { return(4*Math.PI*Math.pow(r,2));
我正在学习pytorch,并正在做anpr项目,它是基于tensorflow的(https://github.com/matthewearl/deep-anpr, 奥 git _a)作为练习,将其移植
我试图找出为什么我的 Java 程序中会出现这种精度丢失错误。 这是错误: error: possible loss of precision int digit = num/Mat
我刚开始在 tensorflow(r1.0) 中使用 ctc 损失层,对“标签”输入有点困惑 在tensorflow的API文档中是这样写的 labels: An int32 SparseTensor
我知道在 Java 中将 BigDecimal 值转换为 Double 时存在“精度问题”。使用 BigDecimal.doubleValue() 简单地从 BigDecimal 转换为 Double
我读了 related question keras 自定义损失函数必须为每个批处理项返回一个标量。 我写了一个损失函数,输出整个批处理的标量,网络似乎收敛了。但是,我找不到任何关于此的文档或代码中究
我有一个 C++ 应用程序,它使用 UDP 服务器(使用 Boost.Asio)以高频率(每秒 3500 个数据包)从千兆本地网络设备接收数据包。一些用户报告了一些数据包丢失。所以最后我选择并行运行
更换了 用response.sendRedirect("URL"); ,我发现我知道一旦重定向发生就会失去 session 。有没有办法通过重定向保留 session ,或者重建 session co
我正在使用 DQN 算法在我的环境中训练代理,如下所示: 代理通过选择离散 Action (左、右、上、下)来控制汽车 目标是以理想的速度行驶而不会撞到其他汽车 状态包含代理的汽车和周围汽车的速度和位
我正在实现简单的 DQN算法使用 pytorch , 解决来自 gym 的 CartPole 环境.我已经调试了一段时间,我无法弄清楚为什么模型没有学习。 观察: 使用 SmoothL1Loss性能比
我正在开发一个网络打印海报打印应用程序。 我正在考虑使用 PHP 来裁剪用户上传的图像,我们最终将打印 PHP 裁剪的图像。 我担心的是原始用户上传的图像与被 PHP 裁剪后的图像之间的“质量”会有所
我是一名优秀的程序员,十分优秀!