Tesorflow学习笔记(1)

参考网址: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]})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值