变量Variable
变量用于存储和更新参数,是内存中用于存放张量(Tensor)的缓存区,必须明确的初始化,并能在训练过程种和结束后保存到磁盘.
* 创建
需要传入一个初始和shape
你将一个 张量 作为初始值传入 Variable() 构造函数。TensorFlow提供了一系列操作符来初始化张量,初始值是常量或是随机值
import tensorflow as tf
# Create a variable.
w = tf.Variable(<initial-value>, name=<optional-name>)
weights = tf. Variable(tf. random_normal([784, 200] , stddev=0.35) ,
name="weights")
biases = tf. Variable(tf. zeros([200] ) , name="biases")
* 调用 tf.Variable() 添加一些操作(Op, operation)到graph:
# Use the variable in the graph like any Tensor.
y = tf.matmul(w, ...another variable or tensor...)
# The overloaded operators are available too.
z = tf.sigmoid(w + b)
- 给w赋新值
# Assign a new value to the variable with `assign()` or a related method. w.assign(w + 1.0) w.assign_add(1.0)
初始化
# 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 all variables.添加一个节点用来初始化变量 init_op = tf.initialize_all_variables() # Later, when launching the model 运行模型 with tf. Session() as sess: # Run the init operation. sess. run(init_op) ... # Use the model
由另一个变量初始化
# 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() * 0.2, name="w_twice")
保存和加载
保存 tf.train.saver
# Create some variables. v1 = tf. Variable(... , name="v1") v2 = tf. Variable(... , name="v2") ... # Add an op to initialize the variables. init_op = tf. initialize_all_variables() # 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
恢复变量
用同一个 Saver 对象来恢复变量。注意,当你从文件中恢复变量时,不需要事先对它们做初始化。
save.restore# 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 ...
恢复部分变量
# Create some variables. v1 = tf. Variable(... , name="v1") v2 = tf. Variable(... , name="v2") ... # Add ops to save and restore only 'v2' using the name "my_v2" saver = tf. train. Saver({"my_v2": v2} ) # Use the saver object normally after that.