TensorFlow Quick Start

Justin Yang

Quick Installation

# 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

Introduction

先談談基本觀念

TensorFlow 的運算是基於圖

一張圖會有什麼?

演算法常說,一張圖包含了

TensorFlow 的觀點

一個「點」,對應一個操作

Operation_A

Operation_B

Operation_C

Operation_D

operation

^1/_4
1/4^1/_4

TensorFlow 的觀點

一條「邊」,對應某些張量流

Operation_A

Operation_B

Operation_C

Operation_D

Tensor flow

^2/_4
2/4^2/_4

TensorFlow 的觀點

操作間透過輸入與輸出張量流互相溝通

Operation_A

Operation_B

Operation_C

Operation_D

operation

tensor flow

^3/_4
3/4^3/_4

TensorFlow 的觀點

最後,我們會用一個會話來執行這張圖

Operation_A

Operation_B

Operation_C

Operation_D

session

Session_A

^4/_4
4/4^4/_4

Show me the code

怎麼創建一張圖

從點出發

operation

點能分成兩種類型:

值來自自己

值來自他人的 tensor

Create an operation

創建常量點

^1/_3
1/3^1/_3
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.] ]

Create an operation

創建變數點

^2/_3
2/3^2/_3
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

Create an operation

若想創建變數又還沒有要賦值,可以使用占位符

^3/_3
3/3^3/_3
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

Oops ! too fast

怎麼多了一堆沒看過的東西?

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)]
^1/_3
1/3^1/_3

先來處理第四行

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)]
^2/_3
2/3^2/_3

先來處理第四行

^3/_3
3/3^3/_3
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)]
^1/_6
1/6^1/_6

如何執行一張圖

還記得嗎?Tensorflow 要透過一個會話去執行圖

^2/_6
2/6^2/_6

session

Operation_B

Operation_C

Operation_D

Session_A

如何執行一張圖

要啟動一個會話,just run it.

^3/_6
3/6^3/_6
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 的寫法

^4/_6
4/6^4/_6
import tensorflow as tf

hello = tf.constant('Hello, TensorFlow!')

with tf.Session() as sess:
  result = sess.run(hello)
  print result

如何執行一張圖

^5/_6
5/6^5/_6
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 版本追求更高效能

^6/_6
6/6^6/_6

input1's tensor

input2's tensor

Do multiplication

Input1

Input2

output

input1 與 input2 是同時送出 tensor

TensorFlow counter

# 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))
^1/_5
1/5^1/_5

Quiz time : What is the output?

TensorFlow counter

# output:
# 0
# 1
# 1
# 1
^2/_5
2/5^2/_5

The answer is

TensorFlow counter

# output:
# 0
# 1
# 2
# 3
^3/_5
3/5^3/_5

That is the correct answer.

TensorFlow counter

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))
^4/_5
4/5^4/_5

為什麼即便沒 run new_value,他還是會 ++ ?

 因為 update 參照了 new_value,所以 new_value 也會跟著執行

TensorFlow counter

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))
^5/_5
5/5^5/_5

new_value 不是執行過了嗎?它應該停在一才對呀

new_value 參照的是一個函式,而不是一個變數

換句話說,我們做的是加加,而不是取值

總歸而言

1. TensorFlow 的圖描述了一個計算的過程

2. 這張圖是由若干個操作組成的

3. 每個操作獲得0~N個 tensor 來計算,並輸出0~M個tensor

4. 圖必須由一個會話啟動

5. 會話會將圖中的操作分配給 CPU / GPU 並行的運算

Example - MINST