链式法则 梯度计算
公式
δ y δ x = δ y δ u δ u δ x \frac{\delta y}{\delta x} = \frac{\delta y}{\delta u} \frac{\delta u}{\delta x} δxδy=δuδyδxδu
"""
反向传播 链式法则
"""
import tensorflow as tf
# 准备数据
x = tf.constant(1.)
w1 = tf.constant(2.)
b1 = tf.constant(1.)
w2 = tf.constant(2.)
b2 = tf.constant(1.)
# 函数求导
with tf.GradientTape(persistent=True) as tape:
tape.watch([w1, b1, w2, b2])
y1 = w1 * x + b1
y2 = y1 * w2 + b2
# 显示求导结果
dy2_dy1 = tape.gradient(y2, [y1])[0]
dy1_dw1 = tape.gradient(y1, [w1])[0]
dy2_dw1 = tape.gradient(y2, [w1])[0]
print(dy2_dy1 * dy1_dw1)
print(dy2_dw1)