Tensorflow trick 与 细节
来源:互联网 发布:为什么手机网络不稳定 编辑:程序博客网 时间:2024/05/01 12:10
前后传播采用不同方式
How Can I Define Only the Gradient for a Tensorflow Subgraph?
Suppose you want group of ops that behave as f(x) in forward mode, but as g(x) in the backward mode. You implement it as
t = g(x)y = t + tf.stop_gradient(f(x) - t)
在一个bnn的实现中有如下一段:
def round_through(x): # g(x) = x back # f(x) = round(x) forward rounded = K.round(x) return x + K.stop_gradient(rounded - x)
这里正向传播用的是四舍五入后的x
值,而反向传播则保留了小数精度。
停止部分变量的梯度计算
在压缩的retrain步骤中,存在freeze部分变量,retrain另一部分的操作。而在Tensorflow中的tf.stop_gradient
函数只能把整个Tensor全部停止计算。
How to stop gradient for some entry of a tensor in tensorflow提供了一个较好的方法:
res_matrix = tf.stop_gradient(mask_h*E) + mask*E
其中mask
与E
对应,决定了梯度是否需要被保留。
def entry_stop_gradients(target, mask): mask_h = tf.abs(mask-1) return tf.stop_gradient(mask_h * target) + mask * target
Tensor 与 Variable
先看一段程序:
a = tf.Variable([1])with tf.device("/cpu:0"): with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print 'a:',a.eval() print 'type of a:',a a = a + 1 print 'a:',a.eval() print 'type of a:',a b = a + 1 print 'b:',b.eval() print 'type of b:',b
这是很简单的加法,结果肯定都知道,分别是1,2,3
。
但同时又加上了他们的类型,这就不太一样了。
a: [1]type of a: <tf.Variable 'Variable:0' shape=(1,) dtype=int32_ref>a: [2]type of a: Tensor("add:0", shape=(1,), dtype=int32, device=/device:CPU:0)b: [3]type of b: Tensor("add_1:0", shape=(1,), dtype=int32, device=/device:CPU:0)
一开始明明定义的是一个tf.Variable
,结果一加却变成了Tensor
,之后的b
也是这样。说明这些操作只是一些计算过程罢了,就像一些菜谱,没有原材料是无法做饭的。这样说的话,经过了计算,原来的变量a
的值其实是没有改变的,因为这些操作都不是针对的Variable
。像tf.scatter_update
这种操作,输入是Variable
,才能进行变量的更新。但当你加入这句话,还是不会有作用,因为这个函数返回的是一个Tensor
,也是一个“菜谱”,只有当执行时才会生效。
因此Tensorflow的Variable
和Tensor
还需要更深的理解啊。
- Tensorflow trick 与 细节
- 【Python Trick】set与list
- trick
- Trick
- trick
- TensorFlow 需注意的细节问题
- tensorflow 语法及 api 使用细节
- Tensorflow Cnn mnist 的一些细节
- 【Tensorflow】Tensorflow安装与配置
- 深度学习(deep learning)优化调参细节(trick)
- NIO trick and trap NIO的技巧与陷阱
- NIO trick and trap NIO的技巧与陷阱
- Trick(十三)—— 数学与函数
- 神经网络的Trick之Dropout的理解与实现
- 文本挖掘预处理之向量化与Hash Trick
- 基础与细节
- 程序与细节
- 程序与细节
- bind() to 0.0.0.0:443 failed (10013: An attempt was made to access a socket in a way forbidden by it
- 【算法思想】对于枚举类型如何来查找相应字符串
- 实验三 顺序队列
- ThreadLocal实现线程范围内的共享变量
- 人性贪婪
- Tensorflow trick 与 细节
- springmvc+mybatis项目报UnsatisfiedDependencyException异常
- 1036. 跟奥巴马一起编程(15)
- linux C二级指针
- 后缀数组 + LCP(最长公共前缀)
- 连续第四天总结
- 1071. 小赌怡情(15)
- Android面试题总结(二)
- eclipse创建maven工程