tensorflow中gradients基本用法
import tensorflow as tf """ 在反向传播过程中,神经网络需要对 loss 对应的学习参数求偏导(也叫梯度), 该值用来乘以学习率然后更新学习参数使用的。 它是通过 tf.gradients 函数来实现的 """ weight = tf.Variable([[1, 2]]) y = tf.matmul(weight, [[9], [10]]) """ 第一个参数:求导公式的结果 第二个参数:要求的偏导的参数 """ grads = tf.gradients(y, weight)with tf.Session() as sess:sess.run(tf.global_variables_initializer())grad_val = sess.run(grads)print(grad_val) # [array([[ 9, 10]], dtype=int32)] # 对多个公式的多个变量求偏导 tf.reset_default_graph() weight1 = tf.get_variable('weight1', shape=[2]) weight2 = tf.get_variable('weight2', shape=[2]) weight3 = tf.get_variable('weight3', shape=[2]) weight4 = tf.get_variable('weight4', shape=[2])y1 = weight1 + weight2 + weight3 y2 = weight3 + weight4# grad_ys 公式的结果 gradients = tf.gradients([y1, y2], [weight1, weight2, weight3, weight4], grad_ys=[tf.convert_to_tensor([1., 2.]), tf.convert_to_tensor([3., 4.])])with tf.Session() as sess:sess.run(tf.global_variables_initializer())print(sess.run(gradients))""" 梯度停止的实现 对于反向传播过程中某种特殊情况需要停止梯度的运算时, TensorFlow 中提供了 tf.stop_gradients 函数, 被它定义过的节点将没有梯度运算功能 """ a = weight1 + weight2 a_stopped = tf.stop_gradient(a) y3 = a_stopped + weight3gradients1 = tf.gradients(y3, [weight1, weight2, weight3], grad_ys=[tf.convert_to_tensor([1., 2.])]) gradients2 = tf.gradients(y3, [weight3], grad_ys=[tf.convert_to_tensor([1., 2.])]) print(gradients1) # [None, None, < tf.Tensor 'gradients_1/grad_ys_0:0' shape = (2,) dtype = float32 >] with tf.Session() as sess:sess.run(tf.global_variables_initializer())''' 下面代码会报错 因为weight1、weight2 的梯度被停止,程序试图去求一个None的梯度,所以报错 注释掉求 gradients2 就又正确了 ''' # print(sess.run(gradients1)) print(sess.run(gradients2))
tensorflow中gradients基本用法相关推荐
- tensorflow中Tensorboard的用法
tensorflow中Tensorboard的用法 下面代码定义了一个简单的用于实现常量加法和乘法的计算图. import tensorflow as tf input1 = tf.constant( ...
- tensorflow中的eval的用法
tensorflow中eval()的用法 做人工智能实验的过程中遇到这样一段代码不是很明白是什么意思: 查阅资料后明白了tensorflow中eval的用法: with tf.Session() as ...
- python中tensorflow的函数简单用法(未完)
tf.nn.softmax Softmax的含义:将一个N*1的向量归一化为(0,1)之间的值,由于其中采用指数运算,使得向量中数值较大的量特征更加明显. Softmax输出的就是该样本属于各个类的概 ...
- TensorFlow 中三种启动图用法
转自https://blog.csdn.net/lyc_yongcai/article/details/73467480 TensorFlow 中有三种启动图的方法:tf.Session(),tf.I ...
- tensorflow中batch normalization的用法
转载网址:如果侵权,联系我删除 https://www.cnblogs.com/hrlnw/p/7227447.html https://www.cnblogs.com/eilearn/p/97806 ...
- Tensorflow中的各种梯度处理gradient
最近其实一直想自己手动创建op,这样的话好像得懂tensorflow自定义api/op的规则,设计前向与反向,注册命名,注意端口以及文件组织,最后可能还要需要重新编译才能使用.这一部分其实记得tens ...
- TensorFlow优化器及用法
TensorFlow优化器及用法 函数在一阶导数为零的地方达到其最大值和最小值.梯度下降算法基于相同的原理,即调整系数(权重和偏置)使损失函数的梯度下降. 在回归中,使用梯度下降来优化损失函数并获得系 ...
- TensorFlow中的语义分割套件
TensorFlow中的语义分割套件 描述 该存储库用作语义细分套件.目标是轻松实现,训练和测试新的语义细分模型!完成以下内容: 训练和测试方式 资料扩充 几种最先进的模型.轻松随插即用 能够使用任何 ...
- TensorFlow中设置学习率的方式
目录 1. 指数衰减 2. 分段常数衰减 3. 自然指数衰减 4. 多项式衰减 5. 倒数衰减 6. 余弦衰减 6.1 标准余弦衰减 6.2 重启余弦衰减 6.3 线性余弦噪声 6.4 噪声余弦衰减 ...
最新文章
- BestCoder Round #92 比赛记录
- 最新版GMP规范全文
- 对于shell脚本获取参数的一些小技巧
- Android开发--蓝牙操作
- python startswith
- SHA1加密(简单)
- iis php重定向循环,在windows机器IIS上如何设置301永久重定向
- 如何自动保存邮件草稿
- linq.js的用法
- 嵌入式linux启动根文件系统,嵌入式Linux根文件系统制作和挂载
- 精心总结了10个matplotlib绘图技巧,短小精悍,威力无穷!
- 20200120:(leetcode)盛最多水的容器 两种解法
- 进化计算-遗传算法-入门级最好教程
- POJ 2393 Yogurt factory
- HTTP协议和URLConnection使用
- COMSOL 专题学习
- PHP获取真实客户端的真实IP
- 栈Stack和段寄存器SS,SP(学习汇编)
- 【化解数据结构】详解字典结构,并实现一个字典
- 百度超级链作为专班核心成员 参与北京市政务区块链顶层设计