引自:http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/variables.html
在训练模型时,需要使用变量来存储和更新参数。
变量包含张量(Tensor)存放于内存的缓存区。
建模时它们需要明确地初始化,模型训练后它们必须被存储到磁盘。
这些变量的值可在之后的模型训练和分析时被加载。
1、创建
创建变量需要将一个张量作为初始值传入构造函数Variable()。TensorFlow提供了一系列操作符来初始化张量,初始值可以是常量或随机值。
注意,所有的这些操作符都需要你指定张量的shape。指定那个形状自动成为变量的shape。变量的shape通常是固定的,但TF也提供来高级机制来重新调整其行列数。
#Create two variables
weights = tf.Variable(tf.random_normal([784,200],stddev = 0.35),name="weights")
biases = tf.Variable(tf.zeros([200]),name ="biases")
调用tf.Variable()添加一些操作(Op)到graph
#Create two variables
weights =tf.Variable(tf.random_normal([784,200],stddev =0.35),name = "weights")
biases = tf.Variable(tf.zeros([200]),name="biases")
...
#Add an op to initialize the variables
init_op = tf.global_variables_initializer()
#later,when launching the model
with tf.Seeion() as sess:
#Run the init operation
sess.run(init_op)
...
#Use the model
....
3、由另一个变量初始化
有时候需要用另一个变量的初始化值给当前变量初始化。由于tf.global_variables_initializer()是并行地初始化所有变量,所以在这种需求的情况下需要小心。
用其他变量的值的初始化一个新的变量需要使用其他变量的initialize_value()属性。你可以直接把已初始化的值作为新变量的初始值,或者把它当作tensor计算得到一个值赋予新变量。
#Create a variable with a random value
weights = tf.Variable(tf.random_normal([784,200],stddev=0.35),name="weights")
#Create another variable with the same value as 'weights'
w2 = tf.Variable(weights.initialized_value(),name="w2")
#Create another variable with twice the value of 'weights'
w_twice = tf.Variable(weights.initialized_value()*2,name="w_twice")
4、自定义初始化
tf.global_variables_initializer()函数便捷地添加一个op来初始化模型的所有变量。你可以给它传入一组变量进行初始化。详情见Variables Documentation,包括检查变量是否被初始化。
5、保存和加载
最简单的保存和恢复模型的方法是使用tf.train.Saver对象。构造器给graph的所有变量,或是定义在列表里的 变量,添加save和restore ops。saver对象提供来方法来运行这些ops,定义检查点文件的读写路径。
6、检查点文件
变量存储在一个二进制文件里,主要包含从变量名到tensor值的映射关系。
当你创建一个Saver对象时,你可以选择性地为检查点文件中的变量挑选变量名。默认情况下,将每个变量variable.name属性的值。
7、保存变量
用tf.train.Saver()创建一个Saver来管理模型中的所有变量。
#Create some variables
v1 = tf.Variable(...,name="v1")
v2 = tf.Variable(...,name ="v2")
...
#Add an op to initialize the variables
init_op = tf.global_variables_initializer()
#Add ops to save and restore all the variables.
saver = tf.train.Saver()
#Later, launch the model,initialize the variables,do some work,save the variables to disk.
#稍后,启动模型,初始化变量,做一些工作,将变量保存到磁盘。
with tf.Session() as sess:
sess.run(init_op)
#Do some work with the model
..
#save the variables to disk
save_path =saver.save(sess,"/tmp/model.ckpt")
print"Model saved in file: ",save_path
8、恢复变量
用同一个Saver对象来恢复变量。注意,当你从文件中恢复变量时,不需要事先对它们做初始化。
#Create some variables
v1 =tf.Variables(...,name="v1")
v2 = tf.Variables(...,name ="v2")
...
#Add ops to save and restore all the variables
saver =tf.train.Saver()
#Later, launch the model, use the saver to restore variables from disk, and do some work with the model
with tf.Session() as sess:
#Restore variables from disk
saver.restore(sess,"/tmp/model.ckpt")
print "Model restored"
#Do some work with the model
...
9、选择存储和恢复那些变量
如果你不给tf.train.Saver()传入任何参数,那么saver将处理graph中的所有变量。其中每一个变量都以变量创建时传入的名称被保存。
有时候在检查点文件中明确定义变量的名称很有用。举个例子,你也许已经训练得到来一个模型,其中有个变量的命名为“weights”,你想把它恢复到一个新的变量“param”中。
有时候仅保存和恢复一部分变量很有用。eg:你也许训练得到来一个5层神经网络,现在想训练一个6层的新模型,可以将之前5层模型的参数导入到新模型的前5层中。
你可以通过给tf.train.Saver()构造函数传入Python字典,很容易地定义需要保持的变量及对应名称:键对应使用的名称,值对应被管理的变量。
注意:
#Create some variables
v1 = tf.Variables(...,name="v1")
v2 = tf.Variables(...,name="v2")
...
#Add ops to save and restore only 'v2' using name "my_v2"
saver = tf.train.Saver({"my_v2": v2})
#Use the saver object normally after that