天天看點

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

繼續閱讀