- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在将我的训练循环迁移到 Tensorflow 2.0 API .在急切执行模式下,tf.GradientTape
thay thế tf.gradients
.问题是,它们是否具有相同的功能?具体来说:
在函数中 gradient()
:
output_gradients
是否等同于旧API中的grad_ys
?colocate_gradients_with_ops
。 aggregation_method
,gate_gradients
tf.gradients
?它们是否由于缺乏使用而被弃用? 2.0 API 可以用其他方法代替吗? Eager Execution 是否需要它们?chức năng jacobian()
是否等同于 tf.python.ops.parallel_for.gradients
?
câu trả lời hay nhất
请在下面找到回复。
Output Gradients
Và grad_ys
:是的,它们可以被认为是相同的。 详细说明:关于Output Gradients
的信息在Github -> imperative_grad.py中提到如下所示。
output_gradients: if not None, a list of gradient provided for each Target, or None if we are to use the target's computed downstream gradient,
Về grad_ys
的信息在 TF Site 中提到如下图:
grad_ys: is a list of tensors of the same length as ys that holds the initial gradients for each y in ys. When grad_ys is None, we fill in a tensor of '1's of the shape of y for each y in ys. A user can provide their own initial grad_ys to compute the derivatives using a different initial gradient for each y (e.g., if one wanted to weight the gradient differently for each value in each y).
从上面的解释和下面的代码中,在本书的第 394 页中提到,Hands on ML using Scikit-Learn & Tensorflow ,我们可以得出结论,Theta
的初始值可以是一个随机值,我们可以使用参数 output_gradients
hoặc grad_ys
传递它。
theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0), name="theta")
gradients = tf.gradients(mse, [theta])[0]
training_op = tf.assign(theta, theta - learning_rate * gradients)
colocate_gradients_with_ops
:是的,Eager Execution 不需要它,因为它与图形的控制流上下文相关。 详细解释:colocate_gradients_with_ops
định hướngGithub -> ops.py中提到的以下代码. Control flow Context与Context的概念相关,后者与Graphs相关,详见TF Site -> Graphs
def _colocate_with_for_gradient(self, op, gradient_uid,
ignore_existing=False):
with self.colocate_with(op, ignore_existing):
if gradient_uid is not None and self._control_flow_context is not None:
self._control_flow_context.EnterGradientColocation(op, gradient_uid)
thử:
năng suất
Cuối cùng:
self._control_flow_context.ExitGradientColocation(op, gradient_uid)
khác:
năng suất
Vềaggregation_method
:此参数的等效项已在 2.0 中实现,名为 _aggregate_grads
,如 Github link 所示。
Về gate_gradients
:Eager 不需要,因为这也与 Graph Context 相关。
详细说明:如下代码来自Github -> gradients_utils.py ,如果 gate_gradients
vì ĐÚNG VẬY
,则使用函数 _colocate_with_for_gradient
将一些操作添加到图形中,这又取决于图形的控制流上下文.
if gate_gradients and len([x for x in in_grads
if x is not None]) > 1:
with ops.device(None):
with ops._colocate_with_for_gradient( # pylint: disable=protected-access
None,
gradient_uid,
ignore_existing=True):
in_grads = control_flow_ops.tuple(in_grads)
jacobian
:是的,它们是一样的。
关于python - TF 2.0 中的 tf.GradientTape 是否等同于 tf.gradients?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55380219/
在查看 Tensorflow 1.15 中的 OptimizerV2 代码时,我注意到他们使用 backprop.GradientTape 来计算梯度。 我找不到任何关于此类的在线引用,只能找到 tf
晚上好 我想使用 tf2 和 Gradient Tape 函数为一个简单的回归问题实现一个玩具示例。使用 Model.fit 它可以正确学习,但与 GradientTape 一样可以做一些事情,但与
晚上好 我想使用 tf2 和 Gradient Tape 函数为一个简单的回归问题实现一个玩具示例。使用 Model.fit 它可以正确学习,但与 GradientTape 一样可以做一些事情,但与
背景 在 Tensorflow 2 中,存在一个名为 GradientTape 的类。它用于记录对张量的操作,然后可以将其结果微分并馈送到一些最小化算法。例如,from the documentati
我想使用GradientTape在急切执行模式下观察梯度。是否可以创建一个 GradientTape 一次,然后记录所有内容,就好像它具有全局上下文一样? 这是我想做的一个例子: import num
我正在尝试计算 TensorFlow 神经网络输出相对于其输入的雅可比行列式。使用 tf.GradientTape.jacobian 方法可以轻松实现这一点。 TensorFlow 文档中提供的简单示
我试图在每个时期更新权重,但我正在分批处理数据。问题是,为了规范化损失,我需要在训练循环之外记录 TensorFlow 变量(以进行跟踪和规范化)。但是当我这样做时,训练时间很长。 我认为,它将所有批
按照以下代码连接了 3 个神经网络,我们如何从初始网络中获取两个梯度?第一个梯度有效,但第二个梯度返回 None 张量。似乎它们彼此无关以获得梯度。我该如何解决这个问题? with tf.Gradie
我尝试在 Eager 模式下使用 Tensorflow 计算梯度,但是tf.GradientTape () 仅返回 None 值。我不明白为什么。梯度在 update_policy() 函数中计算。
我正在尝试使用 tf.GradientTape 计算梯度。当我尝试使用损失和 Model.variables (tf.keras.Model) 作为输入时,结果以 None 数组的形式返回给我。我究竟
我使用 tf.GradientTape 训练逻辑回归,但它无法收敛 import numpy as np import tensorflow as tf from tensorflow import
我想使用 keras 的预训练模型进行迁移学习 import tensorflow as tf from tensorflow import keras base_model = keras.appl
你好, 我目前正尝试在 Tensorflow 1.13.1 中计算梯度并使用 GradientTape official documentation 中解释的类, 但我得到一个 TypeError:
我正在尝试对使用 SavedModel 加载的模型进行对抗性攻击API。我想针对给定目标的模型损失对输入进行梯度下降。代码有点长,但它是说明问题的最低限度。 from __future__ impor
我在使用 TensorFlow 中的自动微分计算梯度时遇到问题。基本上我想创建一个神经网络,它只有一个输出值 f 并获得两个值(x,t)的输入。网络应该像一个数学函数,所以在这种情况下 f(x,t)
我发现计算的梯度取决于 tf.function 装饰器的相互作用,如下所示。 首先,我为二元分类创建了一些合成数据 tf.random.set_seed(42) np.random.seed(42)
我正在尝试切换到 TensorFlow 热切模式,我找到了 GradientTape 的文档, implicit_gradients , gradients_function和 implicit_va
我开始学习 Tensorflow2.0,我困惑的一个主要来源是什么时候使用 keras-like model.compile vs tf.GradientTape 训练模型。 在用于 MNIST 分类
在 tensorflow 版本 2.0.0-beta1 中,我正在尝试实现一个 keras 层,它具有从正态随机分布中采样的权重.我想将分布的均值作为可训练参数。 感谢 tensorflow-prob
当我使用 tf.Variable 的分配方法来更改变量的值时,它会阻止 tf.Gradient,例如。例如,请参阅下面的玩具示例代码: (注意:我只对 TensorFlow 2 感兴趣。) x = t
Tôi là một lập trình viên xuất sắc, rất giỏi!