Justin Yang
# get pip
$ sudo apt-get install python-pip python-dev
# Ubuntu/Linux 64-bit, CPU only, Python 2.7:
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl
# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7. Requires CUDA toolkit 7.5 and CuDNN v4.
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl
Environment:Ubuntu 14.04 with python 2.7
TensorFlow 的運算是基於圖
演算法常說,一張圖包含了點與邊
一個「點」,對應一個操作
Operation_A
Operation_B
Operation_C
Operation_D
operation
一條「邊」,對應某些張量流
Operation_A
Operation_B
Operation_C
Operation_D
Tensor flow
操作間透過輸入與輸出張量流互相溝通
Operation_A
Operation_B
Operation_C
Operation_D
operation
tensor flow
最後,我們會用一個會話來執行這張圖
Operation_A
Operation_B
Operation_C
Operation_D
session
Session_A
operation
點能分成兩種類型:
值來自自己
值來自他人的 tensor
創建常量點
my_constant = tf.constant(1.0) # create an operation with 1.0.
my_matrix = tf.constant([3.0, 3.0]) # create an operation with a 1*2 matrix.
my_matrix2 = tf.constant([[2.],[2.]]) # create an operation with a 2*1 matrix.
my_constant
my_matrix
my_matrix2
1
[ 3.0,3.0 ]
[ [2.] , [2.] ]
創建變數點
import tensorflow as tf
# Create a Variable node with a 1*2 matrix.
x = tf.Variable([1.0, 2.0])
# Create a Variable, that will be initialized to the scalar value 0.
namedNode = tf.Variable(0, name="counter")
# Variables must be initialized by running an `init` Op after having
# launched the graph. We first have to add the `init` Op to the graph.
init_op = tf.initialize_all_variables()
務必記得,要做一個初始化節點,去負責初始化所有的variable operation
若想創建變數又還沒有要賦值,可以使用占位符
import tensorflow as tf
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1, input2)
with tf.Session() as sess:
print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))
# output:
# [array([ 14.], dtype=float32)]
placeholder
怎麼多了一堆沒看過的東西?
import tensorflow as tf
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
"output = tf.mul(input1, input2)"
"with tf.Session() as sess:"
" print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))"
# output:
# [array([ 14.], dtype=float32)]
剛剛無論是變數還是常量,都是我們自主去創建的
import tensorflow as tf
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
"output = tf.mul(input1, input2)"
#with tf.Session() as sess:
# print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))
# output:
# [array([ 14.], dtype=float32)]
TensorFlow 操作是基於圖,函數會給我們一個處理好的點
import tensorflow as tf
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
"output = tf.mul(input1, input2)"
#with tf.Session() as sess:
# print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))
# output:
# [array([ 14.], dtype=float32)]
import tensorflow as tf
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
"output = tf.mul(input1, input2)"
input1's tensor
input2's tensor
Do multiplication
Input1
Input2
output
我們有點,也有張量流了,現在的關鍵是怎麼開始圖的遍歷
import tensorflow as tf
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1, input2)
"with tf.Session() as sess:"
"print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))"
# output:
# [array([ 14.], dtype=float32)]
還記得嗎?Tensorflow 要透過一個會話去執行圖
session
Operation_B
Operation_C
Operation_D
Session_A
要啟動一個會話,just run it.
import tensorflow as tf
sess = tf.Session() # get a session.
hello = tf.constant('Hello, TensorFlow!')
print sess.run(hello)
# output >> Hello, TensorFlow!
a = tf.constant(10)
b = tf.constant(32)
print sess.run(a+b)
# output >> 42
# Close the Session when we're done.
sess.close()
善用with,追求更 pythonic 的寫法
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
with tf.Session() as sess:
result = sess.run(hello)
print result
import tensorflow as tf
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
with tf.Session() as sess:
result = sess.run([product])
print result
即便只 run 了 product,還是能拿到 matrix1,martrix2 的 tensor
product 能走到哪裡,我們求值就能求到哪
注意一點,Session 的求值是並發執行的,可以 GPU 版本追求更高效能
input1's tensor
input2's tensor
Do multiplication
Input1
Input2
output
input1 與 input2 是同時送出 tensor
# Create a Variable, that will be initialized to the scalar value 0.
state = tf.Variable(0, name="counter")
# Create an Op to add one to `state`.
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)
# Variables must be initialized by running an `init` Op after having
# launched the graph. We first have to add the `init` Op to the graph.
init_op = tf.initialize_all_variables()
# Launch the graph and run the ops.
with tf.Session() as sess:
# Run the 'init' op
sess.run(init_op)
# Print the initial value of 'state'
print(sess.run(state))
# Run the op that updates 'state' and print 'state'.
for _ in range(3):
sess.run(update)
print(sess.run(state))
Quiz time : What is the output?
# output:
# 0
# 1
# 1
# 1
The answer is
# output:
# 0
# 1
# 2
# 3
That is the correct answer.
state = tf.Variable(0, name="counter")
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)
init_op = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init_op)
print(sess.run(state))
for _ in range(3):
sess.run(update)
print(sess.run(state))
為什麼即便沒 run new_value,他還是會 ++ ?
因為 update 參照了 new_value,所以 new_value 也會跟著執行
state = tf.Variable(0, name="counter")
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)
init_op = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init_op)
print(sess.run(state))
for _ in range(3):
sess.run(update)
print(sess.run(state))
new_value 不是執行過了嗎?它應該停在一才對呀
new_value 參照的是一個函式,而不是一個變數
換句話說,我們做的是加加,而不是取值
1. TensorFlow 的圖描述了一個計算的過程
2. 這張圖是由若干個操作組成的
3. 每個操作獲得0~N個 tensor 來計算,並輸出0~M個tensor
4. 圖必須由一個會話啟動
5. 會話會將圖中的操作分配給 CPU / GPU 並行的運算