2017年6月8日 星期四

Tensorflow 使用筆記

Input 數據種類

tf.Variable 

通常用在model 中需要不斷更新的變數,像是需要training 的weights 或是在LSTM中不用training 但需更新的memory。

Creation :

宣告範例如下:
w = tf.Variable(<value>, name=<optional-name>)
name是變數的名稱,可加可不加
value 為要輸入的數據,格式為tensor。
以上只是宣告,但數據還沒有初始化。



初始化:

開始run data 前,必須先初始variable 裡的data。初始的方法我用過兩個,其中一個就是呼tensorflow 內建的 function " tf.global_variables_initializer() " 直接初始化。
ex:
with tf.Session() as session:
     tf.global_variables_initializer().run()
     ........

另一個方法是讀取之前儲存的model 參數:
ex:
saver = tf.train.Saver()
saved_file_path = "...saved model path..."
with tf.Session() as sess:
  # Restore variables from disk.
  ckpt = tf.train.get_checkpoint_state(saved_file_path)
  if ckpt and ckpt.model_checkpoint_path:
      saver.restore(sess,ckpt.model_checkpoint_path)