参考网址:http://www.turnote.com/doc/SOURCE/api_docs/python/state_ops.html
参考网址:http://www.turnote.com/how_tos/variables/index.html
Variable变量
(1)定义:
A variable maintains state in the graph across calls to run()
(2)使用方法:
(3)变量的初始化
(4)使用一个变量初始化另一个变量
tf.train.Saver()类
tf.train.Saver()创建一个saver对象来管理模型中的所有变量
第二个用处:恢复变量
用同一个Saver
对象来恢复变量。注意,当你从文件中恢复变量时,不需要事先对它们做初始化。
恢复变量时变量的个数和名称必须与保存时相同,否则会报错!!
# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., 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
Saver()如果什么参数都不加,则会保存所有的变量值,如果想只保存一部分变量名,可以用如下方法:
v1 = tf.Variable(..., name='v1')
v2 = tf.Variable(..., name='v2')
# Pass the variables as a dict:
saver = tf.train.Saver({'v1': v1, 'v2': v2})
# Or pass them as a list.
saver = tf.train.Saver([v1, v2])
# Passing a list is equivalent to passing a dict with the variable op names
# as keys:
saver = tf.train.Saver({v.op.name: v for v in [v1, v2]})