【发布时间】:2022-01-23 10:47:28
【问题描述】:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/eager/backprop.py#L994-L1095这里是'GradientTape'函数的源代码,但是修改源代码不影响后续回答中的函数。
flat_grad = imperative_grad.imperative_grad(
self._tape,
flat_targets,
flat_sources,
output_gradients=output_gradients,
sources_raw=flat_sources_raw,
unconnected_gradients=unconnected_gradients)
if not self._persistent:
# Keep track of watched variables before setting tape to None
self._watched_variables = self._tape.watched_variables()
self._tape = None
grad = nest.pack_sequence_as(sources, flat_grad)
return 0
这里我改变了return语句。但是使用
x = tf.Variable(3.0)
with tf.GradientTape() as tape:
y = x**2
dy_dx = tape.gradient(y, x)
dy_dx.numpy()
输出为 6.0 还有什么需要改变的?
【问题讨论】:
标签: python keras gradient gradienttape