TensorFlow自动求导

在学习tensorflow的自动求导之前,先要知道什么是Variable对象。
Variable对象其实是对tensor对象的进一步封装,它在模型训练过程中自动记录梯度信息,由算法自动优化。它是一个可以被训练的变量,在机器学习中作为模型参数,可以通过assign()、assign_add()、assign_sub()等函数对它进行赋值。
函数原型:tf.Variable(initial_value,dtype)
其中,initial_value可以是数字,python列表,ndarray对象,tensor对象。dtype是数据类型。

自动求导:

with GradientTape() as tape:
     函数表达式
grad=tape.gradient(函数,自变量)

下面通过实例来看tensorflow如何自动求导。
1.简单求导
在这里插入图片描述

首先导入tensorflow:

import tensorflow as tf

接着将x声明为一个可赋值的变量:

x=tf.Variable(3.)

求导并打印结果:

with tf.GradientTape() as tape:
    y=tf.square(x)
dy_dx=tape.gradient(y,x)
print(y)
print(dy_dx)

输出结果:

tf.Tensor(9.0, shape=(), dtype=float32)
tf.Tensor(6.0, shape=(), dtype=float32)

与手算结果一致。需要注意的是,x=tf.Variable(3.)中必须要有小数点,否则会报这个错:

WARNING:tensorflow:The dtype of the target tensor must be floating (e.g. tf.float32) when calling GradientTape.gradient, got tf.int32

并且得到的是错误的结果:

tf.Tensor(9, shape=(), dtype=int32)
None

---------------分割线--------------------

2.多元函数求偏导数
在这里插入图片描述
在这里插入图片描述
代码实现:

import tensorflow as tf
x=tf.Variable(3.)
y=tf.Variable(4.)
with tf.GradientTape() as tape:
    f=tf.square(x)+2*tf.square(y)+1
df_dx,df_dy=tape.gradient(f,[x,y])
print("f=",f)
print("df_dx=",df_dx)
print("df_dy=",df_dy)

'''输出结果
f= tf.Tensor(42.0, shape=(), dtype=float32)
df_dx= tf.Tensor(6.0, shape=(), dtype=float32)
df_dy= tf.Tensor(16.0, shape=(), dtype=float32)
'''

---------------分割线--------------------
3.求二阶导数
在这里插入图片描述
求一阶导数结果:在这里插入图片描述
二阶导数结果:在这里插入图片描述
代码实现:

import tensorflow as tf
x=tf.Variable(3.)
y=tf.Variable(4.)

with tf.GradientTape() as tape2:
    with tf.GradientTape(persistent=True) as tape1:     
        f=tf.square(x)+2*tf.square(y)+1
    first_grads=tape1.gradient(f,[x,y])
second_grads=[tape2.gradient(first_grads,[x,y])]

print("f=",f)
print("first_grads=",first_grads)
print("second_grads=",second_grads)
del tape1
del tape2
'''输出结果
f= tf.Tensor(42.0, shape=(), dtype=float32)
first_grads= [<tf.Tensor: shape=(), dtype=float32, numpy=6.0>, <tf.Tensor: shape=(), dtype=float32, numpy=16.0>]
second_grads= [[<tf.Tensor: shape=(), dtype=float32, numpy=2.0>, <tf.Tensor: shape=(), dtype=float32, numpy=4.0>]]
'''

---------------分割线--------------------
4.对向量求偏导

import tensorflow as tf

x=tf.Variable([1.,2.,3.])
y=tf.Variable([4.,5.,6.])

with tf.GradientTape() as tape:    
    f=tf.square(x)+2*tf.square(y)+1
df_dx,df_dy=tape.gradient(f,[x,y])

print("f=",f)
print("df_dx=",df_dx)
print("df_dy=",df_dy)
'''输出结果
f= tf.Tensor([34. 55. 82.], shape=(3,), dtype=float32)
df_dx= tf.Tensor([2. 4. 6.], shape=(3,), dtype=float32)
df_dy= tf.Tensor([16. 20. 24.], shape=(3,), dtype=float32)
'''
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值