tensorflow 基本用法

# 構建圖
# 1、構建圖的第一步, 是創建源 op (source op). 源 op 不需要任何輸入, 例如 常量 (Constant). 源 op 的輸出被
# 傳遞給其它 op 做運算.
# 2、Python 庫中, op 構造器的返回值代表被構造出的 op 的輸出, 這些返回值可以傳遞給其它 op 構造器作爲輸入.
# 3、TensorFlow Python 庫有一個默認圖 (default graph), op 構造器可以爲其增加節點. 這個默認圖對 許多程序
# 來說已經足夠用了. 閱讀 Graph 類(http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#Graph) 
# 文檔 來了解如何管理多個圖.
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2' #用於忽略級別 2 及以下的消息(級別 1 是提示,級別 2 是警告,級別 3 是錯誤)

# 創建一個常量 op, 產生一個 1x2 矩陣. 這個 op 被作爲一個節點
# 加到默認圖中.
# 構造器的返回值代表該常量 op 的返回值.
matrix1 = tf.constant([[3., 3.]])
# 創建另外一個常量 op, 產生一個 2x1 矩陣.
matrix2 = tf.constant([[2.],[2.]])
# 創建一個矩陣乘法 matmul op , 把 'matrix1' 和 'matrix2' 作爲輸入.
# 返回值 'product' 代表矩陣乘法的結果.
product = tf.matmul(matrix1, matrix2)
# 默認圖現在有三個節點, 兩個 constant() op, 和一個matmul() op. 爲了真正進行矩陣相乘運算, 
# 並得到矩陣乘法的 結果, 你必須在會話裏啓動這個圖.

# 在一個會話中啓動圖
# 構造階段完成後, 才能啓動圖. 啓動圖的第一步是創建一個 Session 對象, 如果無任何創建參數, 會話構造器將啓動默認圖.
# 欲瞭解完整的會話 API, 請閱讀Session 類(http://www.tensorfly.cn/tfdoc/api_docs/python/client.html#session-management)
# 啓動默認圖.
sess = tf.Session()
# 調用 sess 的 'run()' 方法來執行矩陣乘法 op, 傳入 'product' 作爲該方法的參數. 
# 上面提到, 'product' 代表了矩陣乘法 op 的輸出, 傳入它是向方法表明, 我們希望取回
# 矩陣乘法 op 的輸出.
# 整個執行過程是自動化的, 會話負責傳遞 op 所需的全部輸入. op 通常是併發執行的.
# 函數調用 'run(product)' 觸發了圖中三個 op (兩個常量 op 和一個矩陣乘法 op) 的執行.
# 返回值 'result' 是一個 numpy `ndarray` 對象.
result = sess.run(product)
print (result)
# ==> [[ 12.]]
# 任務完成, 關閉會話.
sess.close()

# Session 對象在使用完後需要關閉以釋放資源. 除了顯式調用 close 外, 也可以使用 "with" 代碼塊 來自動完成關閉動作.
with tf.Session() as sess:
  result = sess.run([product])
  print (result)
# 結果:
  # [[12.]]
  # [array([[12.]], dtype=float32)]

# 在實現上, TensorFlow 將圖形定義轉換成分佈式執行的操作, 以充分利用可用的計算資源(如 CPU 或 GPU). 一般你不需要顯式
# 指定使用 CPU 還是 GPU, TensorFlow 能自動檢測. 如果檢測到 GPU, TensorFlow 會儘可能地利用找到的第一個 GPU 來執行操作.
# 如果機器上有超過一個可用的 GPU, 除第一個外的其它 GPU 默認是不參與計算的. 爲了讓 TensorFlow 使用這些 GPU, 你必須將 
# op 明確指派給它們執行. with...Device 語句用來指派特定的 CPU 或 GPU 執行操作:
# with tf.Session() as sess:
#   with tf.device("/gpu:0"):
#     matrix1 = tf.constant([[3., 3.]])
#     matrix2 = tf.constant([[2.],[2.]])
#     product = tf.matmul(matrix1, matrix2)
    #print (result)

# 交互式使用
# 爲了便於使用諸如 IPython 之類的 Python 交互環境, 可以使用 InteractiveSession 代替 Session 類, 使用 
# Tensor.eval() 和 Operation.run() 方法代替 Session.run(). 這樣可以避免使用一個變量來持有會話
# 進入一個交互式 TensorFlow 會話.
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0])
# 使用初始化器 initializer op 的 run() 方法初始化 'x' 
x.initializer.run()
# 增加一個減法 sub op, 從 'x' 減去 'a'. 運行減法 op, 輸出結果 
#sub = tf.sub(x, a) #已改名
sub = tf.subtract(x,a)
print (sub.eval())
# ==> [-2. -1.]

# Tensor
# TensorFlow 程序使用 tensor 數據結構來代表所有的數據, 計算圖中, 操作間傳遞的數據都是 tensor. 你可以把 
# TensorFlow tensor 看作是一個 n 維的數組或列表. 一個 tensor 包含一個靜態類型 rank, 和 一個 shape. 想了解 
# TensorFlow 是如何處理這些概念的, 參見 Rank, Shape, 和 Type.
# 變量
# Variables for more details. 變量維護圖執行過程中的狀態信息. 下面的例子演示瞭如何使用變量實現一個簡單的計數器. 
# 參見 變量 章節瞭解更多細節
# 創建一個變量, 初始化爲標量 0.
state = tf.Variable(0, name="counter")
# 創建一個 op, 其作用是使 state 增加 1
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)
# 啓動圖後, 變量必須先經過`初始化` (init) op 初始化,
# 首先必須增加一個`初始化` op 到圖中.
init_op = tf.initialize_all_variables()
# 啓動圖, 運行 op
with tf.Session() as sess:
    #運行'init' op
    sess.run(init_op)
    #打印'state' 的初始值
    print(sess.run(state))
    # 運行 op, 更新 'state', 並打印 'state'
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))
# 輸出:
# 0
# 1
# 2
# 3   
# 1代碼中 assign() 操作是圖所描繪的表達式的一部分, 正如 add() 操作一樣. 所以在調用 run() 執行表達式之前, 
# 它並不會真正執行賦值操作.
# 2通常會將一個統計模型中的參數表示爲一組變量. 例如, 你可以將一個神經網絡的權重作爲某個變量存儲在一個 tensor 中.
#  在訓練過程中, 通過重複運行訓練圖, 更新這個 tensor.
        
# Fetch
# 爲了取回操作的輸出內容, 可以在使用 Session 對象的 run() 調用 執行圖時, 傳入一些 tensor, 這些 tensor 會幫助
# 你取回結果. 在之前的例子裏, 我們只取回了單個節點 state, 但是你也可以取回多個 tensor:
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
intermed = tf.add(input2, input3)
mul = tf.mul(input1, intermed)
with tf.Session():
  result = sess.run([mul, intermed])
  print (result)
# 輸出:
# [array([ 21.], dtype=float32), array([ 7.], dtype=float32)]
# 需要獲取的多個 tensor 值,在 op 的一次運行中一起獲得(而不是逐個去獲取 tensor)。

# Feed
# 1、上述示例在計算圖中引入了 tensor, 以常量或變量的形式存儲. TensorFlow 還提供了 feed 機制, 該機制 可以臨時
# 替代圖中的任意操作中的 tensor 可以對圖中任何操作提交補丁, 直接插入一個 tensor.
# 2、feed 使用一個 tensor 值臨時替換一個操作的輸出結果. 你可以提供 feed 數據作爲 run() 調用的參數. feed 只在
# 調用它的方法內有效, 方法結束, feed 就會消失. 最常見的用例是將某些特殊的操作指定爲 "feed" 操作, 標記的方法是使
# 用 tf.placeholder() 爲這些操作創建佔位符.
input1 = tf.placeholder(tf.types.float32)
input2 = tf.placeholder(tf.types.float32)
output = tf.mul(input1, input2)
with tf.Session() as sess:
  print sess.run([output], feed_dict={input1:[7.], input2:[2.]})

# 輸出:
# [array([ 14.], dtype=float32)]

# for a larger-scale example of feeds. 如果沒有正確提供 feed, placeholder() 操作將會產生錯誤. MNIST 全連通
#  feed 教程 (source code) 給出了一個更大規模的使用 feed 的例子.


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章