![](/img/trans.png)
[英]Conversion from tf.gradients() to tf.GradientTape() returns None
[英]Is tf.GradientTape in TF 2.0 equivalent to tf.gradients?
我正在将我的训练循环迁移到Tensorflow 2.0 API 。 在急切的执行模式中, tf.GradientTape
取代了tf.gradients
。 问题是, 他们是否具有相同的功能? 特别:
在函数gradient()
:
output_gradients
等效于旧API中的grad_ys
? colocate_gradients_with_ops
怎么样? aggregation_method
, gate_gradients
的tf.gradients
? 他们因缺乏使用而被弃用了吗? 可以使用2.0 API中的其他方法替换它们吗? 它们是否需要Eager Execution? 函数jacobian()
等效于tf.python.ops.parallel_for.gradients
?
请在下面找到答案。
Output Gradients
和grad_ys
:是的,它们可以被认为是相同的。 详细说明: Github - > imperative_grad.py中提到了有关Output Gradients
信息,如下所示。
output_gradients:if not None,为每个Target提供的渐变列表,如果我们要使用目标的计算下游渐变,则为None,
TF Site中提到了有关grad_ys
信息,如下所示:
grad_ys:是一个与ys长度相同的张量列表,它包含ys中每个y的初始梯度。 当grad_ys为None时,我们在y中为每个y填充y的形状的'1'的张量。 用户可以提供他们自己的初始grad_ys以使用针对每个y的不同初始梯度来计算导数(例如,如果想要对每个y中的每个值不同地加权梯度)。
根据上面的解释,以及下面的代码,在本书的第394页提到, 使用Scikit-Learn和Tensorflow实现ML ,我们可以得出结论, Theta
初始值可以是随机值,我们可以使用参数传递, output_gradients
或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
指向Github中提到的以下代码- > ops.py。 控制流上下文与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)
try:
yield
finally:
self._control_flow_context.ExitGradientColocation(op, gradient_uid)
else:
yield
关于aggregation_method
:此参数的等价物已在2.0中实现,名为_aggregate_grads
,如Github链接所示
关于gate_gradients
:Eager不需要,因为这也与图形上下文有关。
详细说明:如下面的代码来自Github - > gradients_utils.py ,如果gate_gradients
为True
,则使用函数_colocate_with_for_gradient
将某些操作添加到图形中,而函数_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
:是的,他们是一样的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.