tensorflow-变量

        训练模型时,需要使用变量(Variables)保存和更新参数。Variables是包含张量(tensor)的内存缓冲。变量必须要先被初始化(initialize),而且可以在训练时和训练后保存(save)到磁盘中。之后可以再恢复(restore)保存的变量值来训练和测试模型。

1.创建(Creation)

        创建Variable,需将一个tensor传递给Variable()构造函数。可以使用TensorFlow提供的许多ops(操作)初始化张量,参考constants or random values。这些ops都要求指定tensor的shape(形状)。比如

Create two variables.

1
2
3
4
> weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35), 
> name=”weights”)
> biases = tf.Variable(tf.zeros([200]), name=”biases”)
>

>

 

 调用tf.Variable()函数在graph中增加以下几个ops:
- 一个Variable op ,负责保存变量值。
- 一个initializer op,负责将变量设为初始值,这实际是tf.assign op。
- 初始值的op,比如zeros op 。

tf.Variable()返回一个tf.Variable类的实例。

使用tf.Variable()时,如果系统检测到重名,会做自动处理,不会报错。

1
2
3
4
5
import tensorflow as tf
w_1 = tf.Variable(3,name="w")
w_2 = tf.Variable(1,name="w")
print(w_1.name)
print(w_2.name)

输出:

1
2
w:0
w_1:0

2.设备安置(Device placement)

        使用 with tf.device(…): block,将一个变量安置在一个设备上。

Pin a variable to CPU.

1
2
with tf.device(“/cpu:0”): 
v = tf.Variable(…)

Pin a variable to GPU.

1
2
with tf.device(“/gpu:0”): 
v = tf.Variable(…)

Pin a variable to a particular parameter server task.

1
2
with tf.device(“/job:ps/task:7”): 
v = tf.Variable(…)

改变变量的一些ops,比如v.assign()tf.train.Optimizer需要与变量在同一个设备上。

3.初始化(Initialization)

        在运行模型中其他操作之前,必须先对变量进行初始化。最简单的初始化方法是添加一个对所有变量进行初始化的op,然后再使用model前运行此op。

3.1全局初始化

使用tf.global_variables_initializer()添加一个op来运行初始化。要在完全构建完模型后,在一个对话(Session)中运行它。

Create two variables.

1
2
3
4
> 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 the variables.

1
2
> init_op = tf.global_variables_initializer()
>

>

Later, when launching the model

1
2
3
4
5
6
7
> with tf.Session() as sess: 
> # Run the init operation.
> sess.run(init_op)
> …
> # Use the model
> …
>

>

3.2 用其他变量值创建变量

        用变量A的值初始化另一个变量B,需使用变量A的属性(property)initialized_value()。可以直接使用变量A的初始值,也可以用之计算新的值。

Create a variable with a random value.

1
weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35), name=”weights”)

Create another variable with the same value as ‘weights’.

1
w2 = tf.Variable(weights.initialized_value(), name=”w2”)

Create another variable with twice the value of ‘weights’

1
w_twice = tf.Variable(weights.initialized_value() * 2.0, name=”w_twice”)

4.保存和恢复Saving and Restoring

        最简单的方法是用tf.train.Saver对象,此构造函数在graph中为所有变量(or a specified list)添加save和restore ops。saver对象提供运行这些ops的方法,并指定读写checkpoint files的路径。

4.1 checkpoint文件

        变量存储在一个二进制文件中,包含从变量名称到张量值的映射。

创建checkpoint files时,可以选择性地选择变量名称来保存。默认情况,它使用每个Variable的Variable.name属性。

可以使用inspect_checkpoint库查看checkpoint file中的变量,还有print_tensrs_in_checkpoint_file函数。

4.2保存变量

        用tf.train.Saver()创建一个Saver对象来管理模型中所有变量。

Create some variables.

1
2
3
4
> v1 = tf.Variable(…, name=”v1”) 
> v2 = tf.Variable(…, name=”v2”)
> …
>

>

Add an op to initialize the variables.

1
2
> init_op = tf.global_variables_initializer()
>

>

Add ops to save and restore all the variables.

1
2
> saver = tf.train.Saver()
>

>

Later, launch the model, initialize the variables, do some work, save the variables to disk.

1
2
3
> with tf.Session() as sess: 
> sess.run(init_op)
>

>

Do some work with the model.

..

Save the variables to disk.

1
2
3
> save_path = saver.save(sess, “/tmp/model.ckpt”) 
> print(“Model saved in file: %s” % save_path)
>

>

先初始化变量,再操作模型,最后保存变量。

4.3恢复变量

        使用同样的Saver对象恢复变量,恢复变量时,就不用先初始化变量了。

Create some variables.

1
2
3
4
> v1 = tf.Variable(…, name=”v1”) 
> v2 = tf.Variable(…, name=”v2”)
> …
>

>

Add ops to save and restore all the variables.

1
2
> saver = tf.train.Saver()
>

>

Later, launch the model, use the saver to restore variables from disk, and

do some work with the model.

1
2
> with tf.Session() as sess:
>

>

Restore variables from disk.

1
2
3
> saver.restore(sess, “/tmp/model.ckpt”) 
> print(“Model restored.”)
>

>

Do some work with the model

无初始化操作,先恢复变量,再操模型。

4.4选择保存和恢复的变量

        如果不给tf.train.Saver传递任何参数,Saver会在graph中处理所有变量。每个变量会存在他们创建时的name下。

通过给tf.train.Saver传递一个Python字典,可以指定保存变量的name。key是要在checkpoint file中使用的name, values指要管理的变量。

注意:

  • 可以创建多个saver,分别保存不同的变量集合。
  • 如果在对话开始时,只恢复了部分变量,就要对其他变量运行initializer op。参考tf.variables_initializer()

Create some variables.

1
2
3
4
> v1 = tf.Variable(…, name=”v1”) 
> v2 = tf.Variable(…, name=”v2”)
> …
>

>

Add ops to save and restore only ‘v2’ using the name “my_v2”

1
2
> saver = tf.train.Saver({“my_v2”: v2}) 
>

>

Use the saver object normally after that.

一个小例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import tensorflow as tf

state = tf.Variable(0, name='counter')
print(state.name)
one = tf.constant(1)

new_value = tf.add(state, one)
update = tf.assign(state, new_value)

init = tf.global_variables_initializer() # must have if define variable

with tf.Session() as sess:
sess.run(init)
for _ in range(3):
sess.run(update)
print(sess.run(state))

参考:

https://blog.csdn.net/muyiyushan/article/details/65442052

本文标题:tensorflow-变量

文章作者:goingcoder

发布时间:2018年04月04日 - 17:04

最后更新:2018年04月04日 - 18:04

原始链接:https://goingcoder.github.io/2018/04/04/tf3/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------本文结束感谢您的阅读-------------