一、前言:
目前谷歌已经开源了tensorflow2.0版本,本着铁粉的信念,学习记录一下新的征程。
二、GradientTape(自动求导)介绍:
tensorflow2.0版本提供tf.GradientTape api来实现对数学公式的自动求导功能。默认情况下GradientTape的资源会在执行tf.GradientTape()后被释放,即每个tape只能求一次导数。
with tf.GradientTape() as t:
如果想多次计算梯度,需要创建一个持久的GradientTape。
with tf.GradientTape(persistent=True) as t:
三、GradientTape(自动求导)代码实现:
import tensorflow as tf
x = tf.constant(3.0)
with tf.GradientTape(persistent=True) as t:
t.watch(x) # Ensures that `tensor` is being traced by this tape.
y = x * x
z = y * y
dz_dx = t.gradient(z, x) # 108.0 (4*x^3 at x = 3)
dy_dx = t.gradient(y, x) # 6.0
print("dz/dx=", dz_dx.numpy())
print("dy/dx=", dy_dx.numpy())
del t # Drop the reference to the tape
注:Calling GradientTape.gradient on a persistent tape inside its context is significantly less efficient than calling it outside the context (it causes the gradient ops to be recorded on the tape, leading to increased CPU and memory usage).
参考资料:
2.https://github.com/YunYang1994/TensorFlow2.0-Examples/blob/master/1-Introduction/GradientTape.py