python人工智能自定义求导tf_diffs详解
作者:plum_blossom 发布时间:2023-06-11 13:31:51
自定义求导:(近似求导数的方法)
让x向左移动eps得到一个点,向右移动eps得到一个点,这两个点形成一条直线,这个点的斜率就是x这个位置的近似导数。
eps足够小,导数就足够真。
def f(x):
return 3. * x ** 2 + 2. * x - 1
def approximate_derivative(f, x, eps=1e-3):
return (f(x + eps) - f(x - eps)) / (2. * eps)
print(approximate_derivative(f, 1.))
运行结果:
7.999999999999119
多元函数的求导
def g(x1, x2):
return (x1 + 5) * (x2 ** 2)
def approximate_gradient(g, x1, x2, eps=1e-3):
dg_x1 = approximate_derivative(lambda x: g(x, x2), x1, eps)
dg_x2 = approximate_derivative(lambda x: g(x1, x), x2, eps)
return dg_x1, dg_x2
print(approximate_gradient(g, 2., 3.))
运行结果:
(8.999999999993236, 41.999999999994486)
在tensorflow中的求导
x1 = tf.Variable(2.0)
x2 = tf.Variable(3.0)
with tf.GradientTape() as tape:
z = g(x1, x2)
dz_x1 = tape.gradient(z, x1)
print(dz_x1)
运行结果:
tf.Tensor(9.0, shape=(), dtype=float32)
但是tf.GradientTape()只能使用一次,使用一次之后就会被消解
try:
dz_x2 = tape.gradient(z, x2)
except RuntimeError as ex:
print(ex)
运行结果:
A non-persistent GradientTape can only be used to compute one set of gradients (or jacobians)
解决办法:设置persistent = True,记住最后要把tape删除掉
x1 = tf.Variable(2.0)
x2 = tf.Variable(3.0)
with tf.GradientTape(persistent = True) as tape:
z = g(x1, x2)
dz_x1 = tape.gradient(z, x1)
dz_x2 = tape.gradient(z, x2)
print(dz_x1, dz_x2)
del tape
运行结果:
tf.Tensor(9.0, shape=(), dtype=float32) tf.Tensor(42.0, shape=(), dtype=float32)
使用tf.GradientTape()
同时求x1,x2的偏导
x1 = tf.Variable(2.0)
x2 = tf.Variable(3.0)
with tf.GradientTape() as tape:
z = g(x1, x2)
dz_x1x2 = tape.gradient(z, [x1, x2])
print(dz_x1x2)
运行结果:
[<tf.Tensor: shape=(), dtype=float32, numpy=9.0>, <tf.Tensor: shape=(), dtype=float32, numpy=42.0>]
对常量求偏导
x1 = tf.constant(2.0)
x2 = tf.constant(3.0)
with tf.GradientTape() as tape:
z = g(x1, x2)
dz_x1x2 = tape.gradient(z, [x1, x2])
print(dz_x1x2)
运行结果:
[None, None]
可以使用watch函数关注常量上的导数
x1 = tf.constant(2.0)
x2 = tf.constant(3.0)
with tf.GradientTape() as tape:
tape.watch(x1)
tape.watch(x2)
z = g(x1, x2)
dz_x1x2 = tape.gradient(z, [x1, x2])
print(dz_x1x2)
运行结果:
[<tf.Tensor: shape=(), dtype=float32, numpy=9.0>, <tf.Tensor: shape=(), dtype=float32, numpy=42.0>]
也可以使用两个目标函数对一个变量求导:
x = tf.Variable(5.0)
with tf.GradientTape() as tape:
z1 = 3 * x
z2 = x ** 2
tape.gradient([z1, z2], x)
运行结果:
<tf.Tensor: shape=(), dtype=float32, numpy=13.0>
结果13是z1对x的导数加上z2对于x的导数
求二阶导数的方法
x1 = tf.Variable(2.0)
x2 = tf.Variable(3.0)
with tf.GradientTape(persistent=True) as outer_tape:
with tf.GradientTape(persistent=True) as inner_tape:
z = g(x1, x2)
inner_grads = inner_tape.gradient(z, [x1, x2])
outer_grads = [outer_tape.gradient(inner_grad, [x1, x2])
for inner_grad in inner_grads]
print(outer_grads)
del inner_tape
del outer_tape
运行结果:
[[None, <tf.Tensor: shape=(), dtype=float32, numpy=6.0>], [<tf.Tensor: shape=(), dtype=float32, numpy=6.0>, <tf.Tensor: shape=(), dtype=float32, numpy=14.0>]]
结果是一个2x2的矩阵,左上角是z对x1的二阶导数,右上角是z先对x1求导,在对x2求导
左下角是z先对x2求导,在对x1求导,右下角是z对x2的二阶导数
学会自定义求导就可以模拟梯度下降法了,梯度下降就是求导,再在导数的位置前进一点点 模拟梯度下降法:
learning_rate = 0.1
x = tf.Variable(0.0)
for _ in range(100):
with tf.GradientTape() as tape:
z = f(x)
dz_dx = tape.gradient(z, x)
x.assign_sub(learning_rate * dz_dx)
print(x)
运行结果:
<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=-0.3333333>
结合optimizers进行梯度下降法
learning_rate = 0.1
x = tf.Variable(0.0)
optimizer = keras.optimizers.SGD(lr = learning_rate)
for _ in range(100):
with tf.GradientTape() as tape:
z = f(x)
dz_dx = tape.gradient(z, x)
optimizer.apply_gradients([(dz_dx, x)])
print(x)
运行结果:
<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=-0.3333333>
来源:https://juejin.cn/post/7124959932725592072
猜你喜欢
- 如下所示:群里一朋友发了一个如上图的问题,解决方法如下data = {'a':[1,1,1,1,1,1,1,1,2,2,2,
- 图片缩放会失真是真理,在浏览器里也一样,貌似使用传说中的双三次插值可以让失真看起来比较不明显,但是真的想不通IE7已经实现了,却不默认打开,
- 常有新手问我该怎么备份数据库,下面介绍3种备份数据库的方法:(1)备份数据库文件MySQL中的每一个数据库和数据表分别对应文件系统中的目录和
- 自上一篇文章 Z Story : Using Django with GAE Python 后台抓取多个网站的页面全文 后,大体的进度如下:
- 利用百度api实现人像动漫化百度API地址:https://ai.baidu.com/tech/imageprocess/selfie_an
- 要真说出来哪一个函数能够做得到,还真难。但我们可用下面的代码来进行识别,返回“假”即偶数,返回“真”则奇数: function&n
- 切分文件最近遇到需要切分文件的需求,当然首选用python来解决,网上搜了下感觉都太复杂了,其实用python自带函数即可解决。f = op
- 布局管理就是管理图形窗口中各个部件的位置和排列。图形窗口中的大量部件也需要通过布局管理,对部件进行整理分组、排列定位,才能使界面整齐有序、美
- Python tkinter中label控件动态改变值最近在用tkinter做大作业,因为是第一次接触tkinter,所以会遇到很多问题。比
- 数字运算=:用于给变量赋值type(x):查看数据所属类型isinstance(x, A_tuple):判断数据是否为预期类型+:两个数相加
- 这篇文章主要介绍了Spring Cloud Feign高级应用实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 上次的故事是这样的前女友发来加密的"520快乐.pdf",我用python破解开之后,却发现...事情是这样的小哥哥还是
- 哲学家就餐问题:哲学家就餐问题是典型的同步问题,该问题描述的是五个哲学家共用一张圆桌,分别坐在五张椅子上,在圆桌上有五个盘子和五个叉子(如下
- 一、桥接模式桥接模式,希望能够将一个事物的两个维度分离(解耦),使其都可以独立地变化,并通过桥梁连接起来。(类)抽象部分(Abstracti
- 购物车是电子商务网站中不可缺少的组成部分,但目前大多数购物车只能作为一个顾客选中商品的展示,客户端无法将购物车里的内容提取出来满足自己事务处
- 环境:1 .Windows Server 2016 Datacenter 64位2 .SQL Server 2016 Enterprise
- webpack的loaders是一大特色,也是很重要的一部分。这遍博客我将分类讲解一些常用的laoder一、loaders之 预处理css-
- update :单表的更新不用说了,两者一样,主要说说多表的更新 O
- HTTP格式HTTP GET请求的格式:GET /path HTTP/1.1Header1: Value1Header2: Value2He
- PYTHON首先要安装scapy模块PY3的安装scapy-python3,使用PIP安装就好了,注意,PY3无法使用pyinstaller