TensorFlow tf.gradients的用法详细解析以及具体例子

tf.gradients

官方定义:

tf.gradients(
    ys,
    xs,
    grad_ys=None,
    name=‘gradients‘,
    stop_gradients=None,
)

Constructs symbolic derivatives of sum of ys w.r.t. x in xs.

ys and xs are each a Tensor or a list of tensors. grad_ys is a list of Tensor, holding the gradients received by theys. The list must be the same length as ys.

gradients() adds ops to the graph to output the derivatives of ys with respect to xs. It returns a list of Tensor of length len(xs) where each tensor is the sum(dy/dx) for y in ys.

grad_ys is a list of tensors of the same length as ys that holds the initial gradients for each y in ys. When grad_ysis None, we fill in a tensor of ‘1‘s of the shape of y for each y in ys. A user can provide their own initial grad_ys to compute the derivatives using a different initial gradient for each y (e.g., if one wanted to weight the gradient differently for each value in each y).

stop_gradients is a Tensor or a list of tensors to be considered constant with respect to all xs. These tensors will not be backpropagated through, as though they had been explicitly disconnected using stop_gradient. Among other things, this allows computation of partial derivatives as opposed to total derivatives.

翻译:

1. xs和ys可以是一个张量,也可以是张量列表,tf.gradients(ys,xs) 实现的功能是求ys(如果ys是列表,那就是ys中所有元素之和)关于xs的导数(如果xs是列表,那就是xs中每一个元素分别求导),返回值是一个与xs长度相同的列表。

例如ys=[y1,y2,y3], xs=[x1,x2,x3,x4],那么tf.gradients(ys,xs)=[d(y1+y2+y3)/dx1,d(y1+y2+y3)/dx2,d(y1+y2+y3)/dx3,d(y1+y2+y3)/dx4].具体例子见下面代码第16-17行。

2. grad_ys 是ys的加权向量列表,和ys长度相同,当grad_ys=[q1,q2,g3]时,tf.gradients(ys,xs,grad_ys)=[d(g1*y1+g2*y2+g3*y3)/dx1,d(g1*y1+g2*y2+g3*y3)/dx2,d(g1*y1+g2*y2+g3*y3)/dx3,d(g1*y1+g2*y2+g3*y3)/dx4].具体例子见下面代码第19-21行。

3. stop_gradients使得指定变量不被求导,即视为常量,具体的例子见官方例子,此处省略

 1 import tensorflow as tf
 2 w1 = tf.Variable([[1,2]])
 3 w2 = tf.Variable([[3,4]])
 4 res = tf.matmul(w1, [[2],[1]])
 5
 6 #ys必须与xs有关,否则会报错
 7 # grads = tf.gradients(res,[w1,w2])
 8 #TypeError: Fetch argument None has invalid type <class ‘NoneType‘>
 9
10 # grads = tf.gradients(res,[w1])
11 # # Result [array([[2, 1]])]
12
13 res2a=tf.matmul(w1, [[2],[1]])+tf.matmul(w2, [[3],[5]])
14 res2b=tf.matmul(w1, [[2],[4]])+tf.matmul(w2, [[8],[6]])
15
16 # grads = tf.gradients([res2a,res2b],[w1,w2])
17 #result:[array([[4, 5]]), array([[11, 11]])]
18
19 grad_ys=[tf.Variable([[1]]),tf.Variable([[2]])]
20 grads = tf.gradients([res2a,res2b],[w1,w2],grad_ys=grad_ys)
21 # Result: [array([[6, 9]]), array([[19, 17]])]
22
23 with tf.Session() as sess:
24     tf.global_variables_initializer().run()
25     re = sess.run(grads)
26     print(re)

原文地址:https://www.cnblogs.com/luckyscarlett/p/10570938.html

时间: 2024-08-26 06:10:40

TensorFlow tf.gradients的用法详细解析以及具体例子的相关文章

js中indexof的用法详细解析

本篇文章主要是对js中indexof的用法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 String.IndexOf 方法 (Char, [startIndex], [count]) 报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置. 参数 value 要查找的 Unicode 字符. 对 value 的搜索区分大小写. startIndex(Int32) 可选项,搜索起始位置.不设置则从0开始. count(Int32) 可选项,要

定义8:jquery.cookie用法详细解析

一个轻量级的cookie 插件,可以读取.写入.删除 cookie. jquery.cookie.js 的配置 首先包含jQuery的库文件,在后面包含 jquery.cookie.js 的库文件. <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script> <script type="text/javascript" src="

jquery.cookie实战用法详细解析

Cookie是由服务器端生成,发送给User-Agent(一般是浏览器),浏览器会将Cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站时就发送该Cookie给服务器(前提是浏览器设置为启用cookie). 例如购物网站存储用户曾经浏览过的产品列表,或者门户网站记住用户喜欢选择浏览哪类新闻. 在用户允许的情况下,还可以存储用户的登录信息,使得用户在访问网站时不必每次都键入这些信息? 怎么在js/jquery中操作处理cookie那?今天分享一个cookie操作类--jQ

tensorflow中gradients的使用以及TypeError: Fetch argument None has invalid type &lt;class &#39;NoneType&#39;&gt;错误解析

在反向传播过程中,神经网络需要对每一个loss对应的学习参数求偏导,算出的这个值也就是梯度,用来乘以学习率更新学习参数使用的,它是通过tensorflow中gradients函数使用的. 我们根据官方文档对函数原型进行解析 官方文档中函数原型以及参数如下: tf.gradients( ys, xs, grad_ys=None, name='gradients', colocate_gradients_with_ops=False, gate_gradients=False, aggregatio

Android AsyncTask 详细解析

结构 继承关系 public abstract class AsyncTask extends Object java.lang.Object android.os.AsyncTask<Params, Progress, Result> 类概述 AsyncTask能够适当地.简单地用于 UI线程. 这个类不需要操作线程(Thread)就可以完成后台操作将结果返回UI. 异步任务的定义是一个在后台线程上运行,其结果是在 UI线程上发布的计算. 异步任务被定义成三种泛型类型: Params,Pro

详细解析BluetoothAdapter的详细api

(1)开关状态值 (2)扫描状态值 (3)蓝牙操作接收的广播 (4)蓝牙操作请求的广播 (5)附加域 (6)错误码 (1)获取蓝牙适配器 (2)获取state状态方法 (3)蓝牙是否可用 (4)打开蓝牙 (5)关闭蓝牙 (1)开始扫描 (2)是否在扫描中 (3)取消查找 (4)获取扫描模式 (1)检查蓝牙地址 (2)获取本地蓝牙地址 (3)获取本地蓝牙名称 (4)获取绑定的蓝牙集合 (5)获取远程蓝牙设备 (6)创建监听 这篇文章将会详细解析BluetoothAdapter的详细api, 包括隐

tensorflow-底层梯度tf.AggregationMethod,tf.gradients

(1)tf.AggregationMethod是一个类 Class?AggregationMethod类拥有的方法主要用于聚集梯度 ?计算偏导数需要聚集梯度贡献,这个类拥有在计算图中聚集梯度的很多方法.比如: ADD_N: 所有的梯度被求和汇总,使用 "AddN"操作.有一个特点:所有的梯度在聚集之前必须要准备好,DEFAULT: 默认聚集方法类方法 ADD_N DEFAULT EXPERIMENTAL_ACCUMULATE_N EXPERIMENTAL_TREE TensorFlow

linux中的压缩命令详细解析(二)

我们在<Linux中的压缩命令详细解析(一)>中已经讲解了常见的三种压缩命令,下面我们开始讲解工作中最常用到的tar命令. 为了使压缩和解压缩变得简单,tar命令就应运而生了.那么究竟该如何使用呢? tar.gz格式: 压缩命令: tar -zcvf 压缩文件名 源文件名 举例: 把abc文件压缩成后缀为tar.gz格式的文件 tar -zcvf abc.tar.gz abc 解压缩命令: 举例:解压缩abc.tar.gz文件 tar -zxvf abc.tar.gz tar.bz2格式: 压

【转】UML中的几种关系详细解析

UML图中类之间的关系:依赖,泛化,关联,聚合,组合,实现 类与类图 1) 类(Class)封装了数据和行为,是面向对象的重要组成部分,它是具有相同属性.操作.关系的对象集合的总称. 2) 在系统中,每个类具有一定的职责,职责指的是类所担任的任务,即类要完成什么样的功能,要承担什么样的义务.一个类可以有多种职责,设计得好的类一般只有一种职责,在定义类的时候,将类的职责分解成为类的属性和操作(即方法). 3) 类的属性即类的数据职责,类的操作即类的行为职责 一.依赖关系(Dependence) 依