Tesorflow学习笔记(1)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lhanchao/article/details/51442181
参考网址: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)使用方法:
        import tensorflow as tf
        #create a variable
        w = tf.Variable(,
)
(3)变量的初始化
      with tf.Session() as sess
      init_op = tf.initialize_all_variables()#初始化所有的变量
      sess.run(init_op)#then you can run the ops that use all the variables

     with tf.Session as sess
           sess.run(w.initializer)#then you can run the ops that use variable 'w'
(4)使用一个变量初始化另一个变量
        w = tf.Variable(tf.random_normal([10,20],stddev = 0.35),name = 'weight')
        w_2 = tf.Variable(w.initialized_value(),name = 'w_2')


tf.train.Saver()类
tf.train.Saver()创建一个saver对象来管理模型中的所有变量
Tesorflow学习笔记(1)
第二个用处:恢复变量

用同一个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]})

没有更多推荐了,返回首页