您的位置:首页 > 其它

Tesorflow学习笔记(1)

2016-05-18 10:56 281 查看
参考网址: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对象来管理模型中的所有变量





第二个用处:恢复变量

用同一个
Saver
对象来恢复变量。注意,当你从文件中恢复变量时,不需要事先对它们做初始化。

恢复变量时变量的个数和名称必须与保存时相同,否则会报错!!
[code]# 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

[/code]
Saver()如果什么参数都不加,则会保存所有的变量值,如果想只保存一部分变量名,可以用如下方法:

[code]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]})

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: