1. tf.app.flags
The tf.app.flags module is not in fact implemented using python-gflags, this tf.app.flags is somehow used to configure a network
TensorFlow使用flags自定义命令行参数
简介
TensorFlow项目例子中经常出现tf.app.flags,它支持应用从命令行接受参数,可以用来指定集群配置等。
注意,目前TensorFlow的flags是基于gflag实现的,由于稳定版不一定使用gflag,因此官方文档并没有相关介绍,等API确定后才有官方文档。
使用实例
下面是最简单的tf.flags例子,可以指定多个参数和不同默认值。
如果不传参数,可以使用默认参数,传入参数后输出结果如下。
http://weibo.com/ttarticle/p/show?id=2309404008843351851848
2. tf. app.run()
在程序中看到下面两行代码:
知道 if __name == '__main__'的意思,可是不太明白 tf.app.run()是何用,查询结果如下:
处理flag解析,然后执行main函数 ,即在模块中应该有一个函数是:def main(), 第二个作用即是执行该函数
是对 开篇 1 的完美解决方案
from:http://blog.csdn.net/helei001/article/details/51859423
3. initial_state = lstm_cell.zero_state(batch_size, tf.float32)
接下来就需要给我们的multi lstm cell进行状态初始化。怎么做呢?Zaremba已经告诉我们了
We initialize the hidden states to zero. We then use the
final hidden states of the current minibatch as the initial hidden state of the subsequent minibatch
(successive minibatches sequentially traverse the training set).
也就是初始时全部赋值为0状态。
tf.nn.rnn_cell.MultiRNNCell.zero_state(batch_size, dtype)
那么就需要有一个self._initial_state来保存我们生成的全0状态,最后直接调用MultiRNNCell的zero_state()方法即可。
self._initial_state = cell.zero_state(batch_size, tf.float32)
注意:这里传入的是batch_size,我一开始没看懂为什么,那就看文档的解释吧!
state_size是我们在定义MultiRNNCell的时就设置好了的,只是我们的输入input shape=[batch_size, num_steps],我们刚刚定义好的cell会依次接收num_steps个输入然后产生最后的state(n-tuple,n表示堆叠的层数)但是一个batch内有batch_size这样的seq,因此就需要[batch_size,s]来存储整个batch每个seq的状态。
以上解释 from : http://blog.csdn.net/mydear_11000/article/details/52414342
以下是个人理解:
根据该句子,能想到的即是针对batch_size个句子,就初始化batch_size个initial_state, 每个句子都有一个initial_state,即它不是一个参数。
但是如果我想让它成为一个优化的参数怎么办?即所有的句子共享一个initial_state, which 在训练的过程中能够进行优化。千辛万苦终于给找到解决方案了
鼓掌欢庆一个:
initial_state_vector = tf.get_variable('initial_state_vector', [1, state_size])
initial_state = tf.tile(initial_state_vector, batch_size)
http://stackoverflow.com/questions/39650169/sparsetensor-equivalent-of-tf-tile
http://stackoverflow.com/questions/40167887/how-to-set-initial-state-of-rnn-as-parameter-in-tensorflow
4. tf.initialize_all_variables() 和 tf.initialize_all_variables().run()的区别
第一种完成变量赋值的需要下面三步:
init = tf.initialize_all_variables()
with tf.session() as sess:
sess.run(init)
第二个只需一句话即可完成赋值
tf.initialize_all_variables().run()
5. tensorflow 中rnn 和 dynamic_rnn的区别
(1):tf.nn.rnn的inputs是a list of tensor,这个list的长度是num_steps,也就是将每一个时刻的输入切分出来了,tensor的shape=[batch_size, input_size]【这里的input每一个都是word embedding,因此input_size=hidden_units_size】
dynamic_rnn的inputs不是list of tensors,而是一整个tensor,num_steps是inputs的一个维度
(2)tf.nn.rnn creates an unrolled graph for a fixed RNN length. That means, if you call tf.nn.rnn with inputs having 200 time steps you are creating a static graph with 200 RNN steps. First, graph creation is slow. Second, you’re unable to pass in longer sequences (> 200) than you’ve originally specified.
tf.nn.dynamic_rnn solves this. It uses a tf.While loop to dynamically construct the graph when it is executed. That means graph creation is faster and you can feed batches of variable size
在不同迭代中,batch的个数可以变化,采用dynamic_rnn
https://www.zhihu.com/question/52200883/answer/136317118
6. 如何理解TensorFlow中的batch和minibatch
深度学习的优化算法,说白了就是梯度下降。每次的参数更新有两种方式。第一种,遍历全部数据集算一次损失函数,然后算函数对各个参数的梯度,更新梯度。这种方法每更新一次参数都要把数据集里的所有样本都看一遍,计算量开销大,计算速度慢,不支持在线学习,这称为Batch gradient descent,批梯度下降。另一种,每看一个数据就算一下损失函数,然后求梯度更新参数,这个称为随机梯度下降,stochastic gradient descent。这个方法速度比较快,但是收敛性能不太好,可能在最优点附近晃来晃去,hit不到最优点。两次参数的更新也有可能互相抵消掉,造成目标函数震荡的比较剧烈。
为了克服两种方法的缺点,现在一般采用的是一种折中手段,mini-batch gradient decent,小批的梯度下降,这种方法把数据分为若干个批,按批来更新参数,这样,一个批中的一组数据共同决定了本次梯度的方向,下降起来就不容易跑偏,减少了随机性。另一方面因为批的样本数与整个数据集相比小了很多,计算量也不是很大。
http://hp.stuhome.net/index.php/2016/09/20/tensorflow_batch_minibatch/
7. tf.Variable 和 tf.get_variable
tf.get_variable 和tf.Variable不同的一点是,前者拥有一个变量检查机制,会检测已经存在的变量是否设置为共享变量,如果已经存在的变量没有设置为共享变量,TensorFlow 运行到第二个拥有相同名字的变量的时候,就会报错
http://blog.csdn.net/u013645510/article/details/53769689
8. 在训练过程中,每次运行sess.run(x)时的返回结果不一样
Tensorflow中如果直接打印tensor对象,会输出tensor对象的一些属性信息,而不是直接给出tensor对象的值:
tensorflow.python.ops.variables.Variable object at 0x4c63f90>
如果需要查看Variable或Constant的值,需要运行sess.run(x)。首先我们开一个交互式的Session,假设x是我们要查看的对象:
import tensorflow as tf
x = tf.Variable([1.0,2.0])
sess = tf.InteractiveSession()
x.initializer.run()
print sess.run(x)
1 两个二维tensor连接:
concat_dim:0表示行,1表示列
2 两个三维tensor连接
concat_dim:0表示纵向,1表示行,2表示列
http://blog.csdn.net/helei001/article/details/51363706