自动求导 打开自动求导功能 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)