[英]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.