TensorFlow梯度求解tf.gradients实例


Posted in Python onFebruary 04, 2020

我就废话不多说了,直接上代码吧!

import tensorflow as tf 

w1 = tf.Variable([[1,2]]) 
w2 = tf.Variable([[3,4]]) 

res = tf.matmul(w1, [[2],[1]]) 

grads = tf.gradients(res,[w1]) 

with tf.Session() as sess: 
 tf.global_variables_initializer().run()
 print sess.run(res)
 print sess.run(grads)

输出结果为:

[[4]]
[array([[2, 1]], dtype=int32)]

可以这样看res与w1有关,w1的参数设为[a1,a2],则:

2*a1 + a2 = res

所以res对a1,a2求导可得 [[2,1]]为w1对应的梯度信息。

import tensorflow as tf 
def gradient_clip(gradients, max_gradient_norm):
 """Clipping gradients of a model."""
 clipped_gradients, gradient_norm = tf.clip_by_global_norm(
   gradients, max_gradient_norm)
 gradient_norm_summary = [tf.summary.scalar("grad_norm", gradient_norm)]
 gradient_norm_summary.append(
  tf.summary.scalar("clipped_gradient", tf.global_norm(clipped_gradients)))

 return clipped_gradients
w1 = tf.Variable([[3.0,2.0]]) 
# w2 = tf.Variable([[3,4]]) 
params = tf.trainable_variables()
res = tf.matmul(w1, [[3.0],[1.]]) 
opt = tf.train.GradientDescentOptimizer(1.0)
grads = tf.gradients(res,[w1]) 
clipped_gradients = gradient_clip(grads,2.0)
global_step = tf.Variable(0, name='global_step', trainable=False)
#update = opt.apply_gradients(zip(clipped_gradients,params), global_step=global_step)
with tf.Session() as sess: 
 tf.global_variables_initializer().run()
 print sess.run(res)
 print sess.run(grads) 
 print sess.run(clipped_gradients)

以上这篇TensorFlow梯度求解tf.gradients实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python中尾递归用法实例详解
Apr 28 Python
举例讲解Python设计模式编程的代理模式与抽象工厂模式
Jan 16 Python
Python程序中设置HTTP代理
Nov 06 Python
Python文本处理之按行处理大文件的方法
Apr 09 Python
基于Python开发chrome插件的方法分析
Jul 07 Python
Python爬虫:url中带字典列表参数的编码转换方法
Aug 21 Python
Tensorflow 使用pb文件保存(恢复)模型计算图和参数实例详解
Feb 11 Python
解决Python中报错TypeError: must be str, not bytes问题
Apr 07 Python
Python-for循环的内部机制
Jun 12 Python
浅析python中的del用法
Sep 02 Python
Python3接口性能测试实例代码
Jun 20 Python
如何用六步教会你使用python爬虫爬取数据
Apr 06 Python
基于TensorFlow中自定义梯度的2种方式
Feb 04 #Python
tensorflow 查看梯度方式
Feb 04 #Python
opencv python图像梯度实例详解
Feb 04 #Python
TensorFlow设置日志级别的几种方式小结
Feb 04 #Python
Python 实现加密过的PDF文件转WORD格式
Feb 04 #Python
解决tensorflow打印tensor有省略号的问题
Feb 04 #Python
对Tensorflow中tensorboard日志的生成与显示详解
Feb 04 #Python
You might like
论建造顺序的重要性
2020/03/04 星际争霸
咖啡店都有些什么常规豆子呢?有什么风味在里面
2021/03/04 咖啡文化
高性能PHP框架Symfony2经典入门教程
2014/07/08 PHP
php curl登陆qq后获取用户信息时证书错误
2015/02/03 PHP
Zend Framework基于Command命令行建立ZF项目的方法
2017/02/18 PHP
下载网站打开页面后间隔多少时间才显示下载链接地址的代码
2010/04/25 Javascript
删除节点的jquery代码
2014/01/13 Javascript
了不起的node.js读书笔记之mongodb数据库交互
2014/12/22 Javascript
使用PHP+JavaScript将HTML页面转换为图片的实例分享
2016/04/18 Javascript
angularjs封装bootstrap时间插件datetimepicker
2016/06/20 Javascript
js实现自定义路由
2017/02/04 Javascript
解决vue+element 键盘回车事件导致页面刷新的问题
2018/08/25 Javascript
[19:24]DOTA2客户端使用指南 一分钟快速设置轻松超神
2013/09/24 DOTA
[02:51]DOTA2英雄基础教程 风暴之灵
2013/12/23 DOTA
[01:20]DOTA2 齐天大圣至宝动态展示
2016/12/13 DOTA
[03:15]DOTA2-DPC中国联赛1月22日Recap集锦
2021/03/11 DOTA
python实现得到一个给定类的虚函数
2014/09/28 Python
Python中MySQLdb和torndb模块对MySQL的断连问题处理
2015/11/09 Python
使用python调用zxing库生成二维码图片详解
2017/01/10 Python
Python算法输出1-9数组形成的结果为100的所有运算式
2017/11/03 Python
django rest framework之请求与响应(详解)
2017/11/06 Python
Python中GIL的使用详解
2018/10/03 Python
python获取服务器响应cookie的实例
2018/12/28 Python
Python2和3字符编码的区别知识点整理
2019/08/08 Python
python中通过selenium简单操作及元素定位知识点总结
2019/09/10 Python
Python中的四种交换数值的方法解析
2019/11/18 Python
基于selenium及python实现下拉选项定位select
2020/07/22 Python
解决python3.6用cx_Oracle库连接Oracle的问题
2020/12/07 Python
StubHub西班牙:购买和出售全球活动门票
2017/06/05 全球购物
教师队伍管理制度
2014/01/14 职场文书
先进工作者获奖感言
2014/02/08 职场文书
工作推荐信范文
2014/05/10 职场文书
学生检讨书怎么写
2014/10/09 职场文书
周年庆典答谢词
2015/01/20 职场文书
2015年教务工作总结
2015/05/23 职场文书
用Python爬取英雄联盟的皮肤详细示例
2021/12/06 Python