天天看点

Tensorflow变量与张量张量(tensor)变量:创建、初始化、保存、加载

张量(tensor)

  • 在tensorflow程序中所有的数据都通过张量的形式来表示。
  • 从功能的角度看,张量可以被理解为多维数组。其中零阶张量表示标量(scalar)也就是一个数;一阶张量为向量,也就是一维数组;n阶张量可以理解为一个n维数组。
  • 但张量的实现并不是直接采用数组的形式,它只是对TensorFlow中运算结果的引用。在张量中并没有保存数字,它保存的是如何得到这些数字的计算过程。
    >>> import tensorflow as tf
    >>> a = tf.constant([1.0,2.0],name='a')
    >>> b = tf.constant([2.0, 3.0], name='b')
    >>> result=tf.add(a,b,name='add')
    >>> print(result)
    Tensor("add:0", shape=(2,), dtype=float32)
               

Tensorflow计算的结果不是一个具体的数字而是一个张量结构,一个张量主要保存三个属性:名字(name)、维度(shape)和类型(type)。

张量的属性名字(name):是张量的唯一标志符,计算图中每一个节点代表一个计算,计算的结果就保存在张量中,比如上面“add:0”就说明result这个张量是计算节点“add”输出的第一个结果(编号从0开始)。

张量的阶、形状、数据类型

1. 数据类型

数据类型    Python 类型   描述
    DT_FLOAT    tf.float32  32 位浮点数.
    DT_DOUBLE   tf.float64  64 位浮点数.
    DT_INT64    tf.int64    64 位有符号整型.
    DT_INT32    tf.int32    32 位有符号整型.
    DT_INT16    tf.int16    16 位有符号整型.
    DT_INT8     tf.int8     8 位有符号整型.
    DT_UINT8    tf.uint8    8 位无符号整型.
    DT_STRING   tf.string   可变长度的字节数组.每一个张量元素都是一个字节数组.
    DT_BOOL     tf.bool     布尔型.
    DT_COMPLEX64    tf.complex64    由两个32位浮点数组成的复数:实数和虚数.
    DT_QINT32   tf.qint32   用于量化Ops的32位有符号整型.
    DT_QINT8    tf.qint8    用于量化Ops的8位有符号整型.
    DT_QUINT8   tf.quint8   用于量化Ops的8位无符号整型.
           

2.形状

TensorFlow文档中使用了三种记号来方便地描述张量的维度:阶,形状以及维数.下表展示了他们之间的关系:

阶   形状  维数  实例
0   [ ]     0-D     一个 0维张量. 一个纯量.
1   [D0]    1-D     一个1维张量的形式[5].
2   [D0, D1]    2-D     一个2维张量的形式[3, 4].
3   [D0, D1, D2]    3-D     一个3维张量的形式 [1, 4, 3].
n   [D0, D1, ... Dn]    n-D     一个n维张量的形式 [D0, D1, ... Dn].
           

变量:创建、初始化、保存、加载

  • 变量是包含张量的内存缓冲。
  • 主要使用两个类:tf.Variable类和tf.train.Saver类
  • 变量必须要先被初始化(initialize),而且可以在训练时和训练后保存(save)到磁盘中。之后可以再恢复(restore)保存的变量值来训练和测试模型。

1. 创建变量

相当于实例化一个tf.Variable类,将一个张量作为初始值传入构造函数Variable()。

实例:

>>> weights = tf.Variable(tf.random_normal([784,200],stddev=0.35),name="weights")
>>> biases = tf.Variable(tf.zeros([200]),name="biases")
           

2. 初始化

使用tf.global_variables_initializer()给所有变量初始化。

>>> init_op = tf.global_variables_initializer()
           

由另一个变量初始化

有时候会需要用另一个变量的初始化值给当前变量初始化。使用其它变量的initialized_value()属性。你可以直接把已初始化的值作为新变量的初始值,或者把它当做tensor计算得到一个值赋予新变量。

# 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")
           

3. 保存加载

最简单的保存和恢复模型的方法是使用tf.train.Saver对象。

保存变量

用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.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对象来恢复变量。注意,当你从文件中恢复变量时,不需要事先对它们做初始化。

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

继续阅读