Fal*_*nUA 6 python tensorflow tensorflow2.0
我正在将训练循环迁移到Tensorflow 2.0 API。在紧急执行模式下,tf.GradientTape替换tf.gradients。问题是,它们具有相同的功能吗?特别:
在功能上gradient():
output_gradients等效grad_ys吗?colocate_gradients_with_ops?aggregation_method,gate_gradients是tf.gradients?是否由于缺乏使用而弃用?可以使用2.0 API中的其他方法替换它们吗?急切执行中是否需要它们?功能jacobian()等效tf.python.ops.parallel_for.gradients吗?
请在下面找到回复。
Output Gradientsand grad_ys:是的,它们可以被认为是相同的。详细说明:关于信息Output Gradients在Github->imperative_grad.py中提到,如下所示。
output_gradients:如果不是 None,则为每个 Target 提供一个梯度列表,如果我们要使用目标计算出的下游梯度,则为 None,
TF站点中grad_ys提到的信息如下所示:
grad_ys:是与 ys 长度相同的张量列表,它保存 ys 中每个 y 的初始梯度。当 grad_ys 为 None 时,我们为 ys 中的每个 y 填充一个 y 形状的“1”张量。用户可以提供他们自己的初始 grad_ys 来计算每个 y 使用不同初始梯度的导数(例如,如果想要为每个 y 中的每个值对梯度进行不同的加权)。
从上面的解释和下面的代码,在本书的第 394 页中提到,使用 Scikit-Learn 和 Tensorflow 进行机器学习,我们可以得出结论, 的初始值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)
Run Code Online (Sandbox Code Playgroud)
colocate_gradients_with_ops:是的,Eager Execution 不需要它,因为它与图的控制流上下文有关。详细说明:colocate_gradients_with_ops指向以下Github -> ops.py 中提到的代码。控制流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)
try:
yield
finally:
self._control_flow_context.ExitGradientColocation(op, gradient_uid)
else:
yield
Run Code Online (Sandbox Code Playgroud)
关于aggregation_method:这个参数的等价物已经在2.0中实现,命名_aggregate_grads如Github链接所示
关于gate_gradients:Eager 不需要,因为这也与图形上下文有关。
详细说明:如以下来自 Github -> gradients_utils.py 的代码所示,如果gate_gradients是True,则使用函数 将一些操作添加到图形中_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)
Run Code Online (Sandbox Code Playgroud)
jacobian:是的,它们是相同的。| 归档时间: |
|
| 查看次数: |
485 次 |
| 最近记录: |