运行环境
Spyder4.0+Python3.7
来源
https://github.com/dragen1860/Deep-Learning-with-TensorFlow-book。
代码
import tensorflow as tf
# 创建4个张量
a = tf.constant(1.)
b = tf.constant(2.)
c = tf.constant(3.)
w = tf.constant(4.)
with tf.GradientTape() as tape:# 构建梯度环境
tape.watch([w]) # 将w加入梯度跟踪列表
# 构建计算过程
y = a * w**2 + b * w + c
# 求导
[dy_dw] = tape.gradient(y, [w])
print("answer is:")
print(dy_dw)