這里的例子包含在:https://github.com/aymericdamien/TensorFlow-Examples/tree/master/examples/1_Introduction
官方的入門講解在:http://www.tensorfly.cn/tfdoc/get_started/basic_usage.html
使用 TensorFlow, 你必須明白 TensorFlow:
- 使用圖 (graph) 來表示計算任務(wù).
- 在被稱之為 會話 (Session) 的上下文 (context) 中執(zhí)行圖.
- 使用 tensor 表示數(shù)據(jù).
- 通過 變量 (Variable) 維護狀態(tài).
- 使用 feed 和 fetch 可以為任意的操作(arbitrary operation) 賦值或者從其中獲取數(shù)據(jù).
對于程序員來說,“hello world”形式的例子幾乎是一種學(xué)習(xí)新技術(shù)的儀式。
tensorflow版的"開機儀式"代碼如下:
import tensorflow as tf
#定義一個常量操作節(jié)點,
hello = tf.constant("hello, TensorFlow2018!")
#獲取一個會話
sess = tf.Session()
#啟動會話運行hello節(jié)點
print(sess.run(hello))
# 任務(wù)完成, 關(guān)閉會話.
sess.close()
#輸出內(nèi)容為:b'hello, TensorFlow2018!'
下面這個例子牽涉到的概念更全面:
import tensorflow as tf
#定義兩個常量操作
#構(gòu)造函數(shù)返回的值就是常量節(jié)點(Constant op)的輸出
a = tf.constant(2)
b = tf.constant(3)
#啟動默認的計算圖
with tf.Session() as sess:
print("a = 2, b = 3")
print("常量相加:{}".format(sess.run(a+b)))
print("常量相乘:{}".format(sess.run(a*b)))
#使用變量輸出值作為計算圖的輸入
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)
#定義一些操作
add = tf.add(a, b)
mul = tf.multiply(a, b)
#啟動默認的計算圖
with tf.Session() as sess:
print("變量相加:{}".format(sess.run(add, feed_dict={a:2, b:3})))
print("變量相乘:{}".format(sess.run(mul, feed_dict={a:2, b:3})))
#創(chuàng)建一個1X2的常量矩陣,該op會作為一個節(jié)點被加入到默認的計算圖
#構(gòu)造器返回的值代表這個op的輸出
matrix1 = tf.constant([[3., 3.]])
#創(chuàng)建一個2X1的常量矩陣
matrix2 = tf.constant([[2.], [2.]])
#創(chuàng)建一個矩陣乘法op,它的輸入為matrix1和matrix2
#返回的值product表示乘法的結(jié)果
product = tf.matmul(matrix1, matrix2)
#為了運行mutmul op我們運行會話的run()方法,使用product作為輸入,product代表mutmul op的輸出
#這表明我們想要matmul op的輸出
#op的所有輸入都會由會話自動運行。這些輸入一般都是并行運行的
#對'run(product)'的調(diào)用回引起這3個op的執(zhí)行:2個constants和一個matmul
#op的輸出值返回給result,這是一個numpy數(shù)組對象
with tf.Session() as sess:
result = sess.run(product)
print("矩陣常量相稱:{}".format(result))
輸出內(nèi)容為:
a = 2, b = 3
常量相加:5
常量相乘:6
變量相加:5
變量相乘:6
矩陣常量相稱:[[ 12.]]
知識總結(jié)
tensorflow的基本概念就是數(shù)據(jù)流圖,使用tensorflow的步驟就是:
- 構(gòu)建數(shù)據(jù)流圖
- 運行數(shù)據(jù)流圖
數(shù)據(jù)流圖由兩種基礎(chǔ)構(gòu)建組成:
- 節(jié)點(node):通常由圓圈表示,代表某種數(shù)學(xué)操作或運算(因此經(jīng)常簡稱op:operation)。
- 邊(edge):對應(yīng)向op傳入和從op傳出的數(shù)值。這些邊可以運輸多維數(shù)據(jù)(即張量)
下面借用http://www.tensorfly.cn/的一段描述.
什么是數(shù)據(jù)流圖(Data Flow Graph)?
數(shù)據(jù)流圖用“結(jié)點”(nodes)和“線”(edges)的有向圖來描述數(shù)學(xué)計算?!肮?jié)點” 一般用來表示施加的數(shù)學(xué)操作,但也可以表示數(shù)據(jù)輸入(feed in)的起點/輸出(push out)的終點,或者是讀取/寫入持久變量(persistent variable)的終點。“線”表示“節(jié)點”之間的輸入/輸出關(guān)系。這些數(shù)據(jù)“線”可以輸運“size可動態(tài)調(diào)整”的多維數(shù)據(jù)數(shù)組,即“張量”(tensor)。張量從圖中流過的直觀圖像是這個工具取名為“Tensorflow”的原因。一旦輸入端的所有張量準備好,節(jié)點將被分配到各種計算設(shè)備完成異步并行地執(zhí)行運算。
運行數(shù)據(jù)流圖需要在會話(session)中進行,常用的創(chuàng)建方式是:
with tf.Session() as sess或者sess = tf.Session()
運行數(shù)據(jù)流圖過程會執(zhí)行各種操作,借用studyAi網(wǎng)站的一副圖片:
如果機器上有超過一個可用的 GPU, 除第一個外的其它 GPU 默認是不參與計算的. 為了讓 TensorFlow 使用這些 GPU, 你必須將 op 明確指派給它們執(zhí)行. with...Device 語句用來指派特定的 CPU 或 GPU 執(zhí)行操作:
with tf.Session() as sess:
with tf.device("/gpu:1"):
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
...
設(shè)備用字符串進行標識. 目前支持的設(shè)備包括:
- "/cpu:0": 機器的 CPU.
- "/gpu:0": 機器的第一個 GPU, 如果有的話.
- "/gpu:1": 機器的第二個 GPU, 以此類推.
與數(shù)據(jù)相關(guān)的op由三種:
- tf.constant
- tf.placeholder
- tf.Variable
使用Variable的使用必須先經(jīng)過初始化 (init) op 初始化
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
# 運行 'init' op
sess.run(init_op)