TensorFlow 基本数据类型

TensorFlow中最基本的单位是常量(Constant)、变量(Variable)和占位符(Placeholder)。

常量定义后值和维度不可变,变量定义后值可变而维度不可变。在神经网络中,变量一般可作为储存权重和其他信息的矩阵,而常量可作为储存超参数或其他结构信息的变量。

占位符属于变量

变量(Variable)

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

1.创建(Creation)

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

# Create two variables.
weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),
    name=”weights”)
biases = tf.Variable(tf.zeros([200]), name=”biases”)

2.设备安置(Device placement)

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

# Pin a variable to CPU.
with tf.device(“/cpu:0”):
    v = tf.Variable(…)

# Pin a variable to GPU.
with tf.device(“/gpu:0”):
    v = tf.Variable(…)

# Pin a variable to a particular parameter server task.
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.
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.
init_op = tf.global_variables_initializer()

#Later, when launching the model
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.
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() * 2.0, name=”w_twice”)

3.3 自定义初始化

可以给tf.global_variables_initializer()添加一个显示列表自定义要初始化的变量。参考Variables Documentation了解更多。

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.
v1 = tf.Variable(…, name=”v1”)
v2 = tf.Variable(…, name=”v2”)
…
# Add an op to initialize the variables.
init_op = tf.global_variables_initializer()

# 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: %s” % save_path)

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

4.3恢复变量

使用同样的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

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

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.
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.

猜你喜欢

转载自blog.csdn.net/u011026329/article/details/79188516