(22)tensorflow自动求导

自动求导

打开自动求导功能

  • tf.GradientTape()
import tensorflow as tf
x = tf.constant([2.3])	   #初始化数据
z = tf.constant([1.])
with tf.GradientTape() as tape:
    tape.watch([x,z])     #加入梯度追踪列表
    x = z**2              #函数过程
    y = x**2+x*3
    grads = tape.gradient(y,[z])[0]   #求导
print(grads)
posted @ 2020-09-07 08:47  kuanleung  阅读(6)  评论(0)    收藏  举报  来源