代码实现:
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]) y = a * w ** 2 + b * w + c [dy_dw] = tape.gradient(y,[w]) print(dy_dw)
执行结果:
本文来自博客园,作者:大码王,转载请注明原文链接:https://www.cnblogs.com/huanghanyu/