Tensorflow 自动求梯度 import tensorflow as tf import timeit a = tf.constant(1.) b = tf.constant(2.) c = tf.constant(3.) w = tf.constant(4.) with tf.GradientTape() as tape: tape.watch([w]) y = a * w **2 + b *w +c [dy_dw] = tape.gradient(y, [w]) print(dy_dw) 结果