……

代码实现:

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)

执行结果:

 

 posted on 2020-06-16 15:18  大码王  阅读(293)  评论(0编辑  收藏  举报
复制代码