tensorflow-gpu2.0之自动导数

import tensorflow as tf
'''
梯度:导数或偏导数
1.在什么点的导数:在点(a,b,c,w)=(1,2,3,4)点的导数
2.梯度环境
    对谁求导:      对w求导
    函数:          y = a*w**2+b*w+c

以上三条是自动导数的必要信息
'''
##1.什么点求导,需要创建定点张量
a = tf.constant(1.)
b = tf.constant(3.)
c = tf.constant(6.)
w = tf.constant(2.)
##2.求导环境,对谁求导
with tf.GradientTape() as tape: ##构建剃度环境
    tape.watch([w])             ##对w求导,[w]
    y = a*w**2+b*w+c            ##函数
##求导
[dy_dw]=tape.gradient(y,[w])
print(dy_dw)

输出:tf.Tensor(7.0, shape=(), dtype=float32)

说明:

1.常量的创建(张量)使用tf.constant

2.剃度环境:tf.GradientTape()

3.对谁求导:tape.watch([w])

猜你喜欢

转载自www.cnblogs.com/liuhuacai/p/12329188.html
今日推荐