Tensorflow學習記錄(一)

2021-08-26 14:59:41 字數 4170 閱讀 3797

tensorflow 是乙個程式設計系統, 使用圖來表示計算任務. 圖中的節點被稱之為 op (operation 的縮寫). 乙個 op 獲得 0 個或多個tensor, 執行計算, 產生 0 個或多個tensor. 每個 tensor 是乙個型別化的多維陣列. 例如, 你可以將一小**像集表示為乙個四維浮點數陣列, 這四個維度分別是[batch, height, width, channels].為了進行計算, 圖必須在會話裡被啟動.會話將圖的 op 分發到諸如 cpu 或 gpu 之類的裝置上, 同時提供執行 op 的方法. 這些方法執行後, 將產生的 tensor 返回.

python 語言中, 返回的 tensor 是 numpyndarray物件; 在 c 和 c++ 語言中, 返回的 tensor 是tensorflow::tensor例項.

import tensorflow as tf

# 建立乙個常量 op, 產生乙個 1x2 矩陣. 這個 op 被作為乙個節點

# 加到預設圖中.

## 構造器的返回值代表該常量 op 的返回值.

matrix1 = tf.constant([3.,3.])

# 建立另外乙個常量 op, 產生乙個 2x1 矩陣.

matrix2 = tf.constant([[2.],[2.]])

# 建立乙個矩陣乘法 matmul op , 把 'matrix1' 和 'matrix2' 作為輸入.

# 返回值 'product' 代表矩陣乘法的結果.

product = tf.multiply(matrix1,matrix2)

# 啟動預設圖

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)

# 任務完成, 關閉會話.

sess.close()

#session 物件在使用完後需要關閉以釋放資源. 除了顯式呼叫 close 外, 也可以使用 "with" **塊 來自動完成關閉動作.

with tf.session() as sess:

result = sess.run([product])

print(result)

在實現上, 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.multiply(matrix1, matrix2)

...

裝置用字串進行標識. 目前支援的裝置包括:

為了便於使用諸如 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.subtract(x, a)

print(sub.eval())

variables for more details. 變數維護圖執行過程中的狀態資訊.

state = tf.variable(0,name="counter")

# 建立乙個 op

one = tf.constant(1)

# 使 state 增加 1

new_state = tf.add(state,one)

# 將new_state賦值給state

#assign()操作是圖所描繪的表示式的一部分, 正如add()操作一樣. 所以在呼叫run()執行表示式之前, 它並不會真正執行賦值操作.

update = tf.assign(state,new_state)

#初始化

init = tf.global_variables_initializer()

sess.run(init)

for _ in range(3):

print(new_state)

result = sess.run(update)

print(result)

為了取回操作的輸出內容, 可以在使用session物件的run()呼叫 執行圖時, 傳入一些 tensor, 這些 tensor 會幫助你取回結果. 在之前的例子裡, 我們只取回了單個節點state, 但是你也可以取回多個 tensor.需要獲取的多個 tensor 值,在 op 的一次執行中一起獲得(而不是逐個去獲取 tensor)。

input1 = tf.constant(3.0)

input2 = tf.constant(2.0)

input3 = tf.constant(5.0)

intermed = tf.add(input2, input3)

mul = tf.multiply(input1, intermed)

with tf.session():

result = sess.run([mul, intermed])

print(result)

feed 使用乙個 tensor 值臨時替換乙個操作的輸出結果. 你可以提供 feed 資料作為run()呼叫的引數. feed 只在呼叫它的方法內有效, 方法結束, feed 就會消失. 最常見的用例是將某些特殊的操作指定為 "feed" 操作, 標記的方法是使用 tf.placeholder() 為這些操作建立佔位符.

input1 = tf.placeholder(tf.float32)

input2 = tf.placeholder(tf.float32)

output = tf.multiply(input1, input2)

print(sess.run([output], feed_dict=))

Tensorflow學習記錄

tensorflow是乙個使用採用資料流圖進行數值計算的開源軟體庫。什麼是資料流圖?資料流圖用結點 nodes 和邊 edges 的有向圖來表示數學計算。結點一般用來表示施加的數學操作,但也可以表示資料輸入的起點和資料輸出的終點。邊表示結點之間的輸入 輸出關係。這些邊可以表示多維資料陣列,我們稱這些...

TensorFlow學習記錄

在電腦上按照官方的pip3安裝方法把tensorflow安裝起了。首先學習的是mnist資料集手寫數字分類。這個是入門級的優化方案。剛剛開始的時候對sess。run等很疑惑,反覆研究了幾天。終於知道,學習tensorflow的框架,主要看前面的輸入x和輸出y,以及待優化的引數w和bias項b的關係,...

Tensorflow學習記錄3

variable的使用 import tensorflow as tf state tf.variable 0,name counter 一定要定義成是個變數才是個變數,初始值0,名字counter print state.name 輸出 名字 第幾個變數 name index one tf.con...