TensorFlow學習筆記1:入門

TensorFlow 簡介

TensorFlow是Google在2015年11月份開源的人工智能系統(Github項目地址),是以前所開發的深度學習基礎架構DistBelief的改進版本,該系統能夠被用於語音識別、圖片識別等多個領域。html

官網上對TensorFlow的介紹是,一個使用數據流圖(data flow graphs)技術來進行數值計算的開源軟件庫。數據流圖中的節點,表明數值運算;節點節點之間的邊,表明多維數據(tensors)之間的某種聯繫。你能夠在多種設備(含有CPU或GPU)上經過簡單的API調用來使用該系統的功能。TensorFlow是由Google Brain團隊的研發人員負責的項目。node

什麼是數據流圖(Data Flow Graph)

數據流圖是描述有向圖中的數值計算過程。有向圖中的節點一般表明數學運算,但也能夠表示數據的輸入、輸出和讀寫等操做;有向圖中的邊表示節點之間的某種聯繫,它負責傳輸多維數據(Tensors)。圖中這些tensorsflow也就是TensorFlow的命名來源。python

節點能夠被分配到多個計算設備上,能夠異步和並行地執行操做。由於是有向圖,因此只有等到以前的入度節點們的計算狀態完成後,當前節點才能執行操做。git

TensorFlow的特性

1 靈活性github

TensorFlow不是一個嚴格的神經網絡工具包,只要你可使用數據流圖來描述你的計算過程,你可使用TensorFlow作任何事情。你還能夠方便地根據須要來構建數據流圖,用簡單的Python語言來實現高層次的功能。編程

2 可移植性api

TensorFlow能夠在任意具有CPU或者GPU的設備上運行,你能夠專一於實現你的想法,而不用去考慮硬件環境問題,你甚至能夠利用Docker技術來實現相關的雲服務。數組

3 提升開發效率網絡

TensorFlow能夠提高你所研究的東西產品化的效率,而且能夠方便與同行們共享代碼。session

4 支持語言選項

目前TensorFlow支持Python和C++語言。(可是你能夠本身編寫喜好語言的SWIG接口)

5 充分利用硬件資源,最大化計算性能

基本使用

你須要理解在TensorFlow中,是如何:

  • 將計算流程表示成圖;
  • 經過Sessions來執行圖計算;
  • 將數據表示爲tensors
  • 使用Variables來保持狀態信息;
  • 分別使用feedsfetches來填充數據和抓取任意的操做結果;

概覽

TensorFlow是一種將計算表示爲圖的編程系統。圖中的節點稱爲ops(operation的簡稱)。一個ops使用0個或以上的Tensors,經過執行某些運算,產生0個或以上的Tensors一個Tensor是一個多維數組,例如,你能夠將一批圖像表示爲一個四維的數組[batch, height, width, channels],數組中的值均爲浮點數。

TensorFlow中的圖描述了計算過程,圖經過Session的運行而執行計算。Session將圖的節點們(即ops)放置到計算設備(如CPUs和GPUs)上,而後經過方法執行它們;這些方法執行完成後,將返回tensors。在Python中的tensor的形式是numpy ndarray對象,而在C/C++中則是tensorflow::Tensor.

圖計算

TensorFlow程序中圖的建立相似於一個 [施工階段],而在 [執行階段] 則利用一個session來執行圖中的節點。很常見的狀況是,在 [施工階段] 建立一個圖來表示和訓練神經網絡,而在 [執行階段] 在圖中重複執行一系列的訓練操做。

建立圖

在TensorFlow中,Constant是一種沒有輸入的ops,可是你能夠將它做爲其餘ops的輸入。Python庫中的ops構造器將返回構造器的輸出。TensorFlow的Python庫中有一個默認的圖,將ops構造器做爲節點,更多可瞭解Graph Class文檔

見下面的示例代碼:

import tensorflow as tf

# Create a Constant op that produces a 1x2 matrix.  The op is
# added as a node to the default graph.
#
# The value returned by the constructor represents the output
# of the Constant op.
matrix1 = tf.constant([[3., 3.]])

# Create another Constant that produces a 2x1 matrix.
matrix2 = tf.constant([[2.],[2.]])

# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.
# The returned value, 'product', represents the result of the matrix
# multiplication.
product = tf.matmul(matrix1, matrix2)

默認的圖(Default Graph)如今有了三個節點:兩個 Constant()ops和一個matmul()op。爲了獲得這兩個矩陣的乘積結果,還須要在一個session中啓動圖計算。

在Session中執行圖計算

見下面的示例代碼,更多可瞭解Session Class

# Launch the default graph.
sess = tf.Session()

# To run the matmul op we call the session 'run()' method, passing 'product'
# which represents the output of the matmul op.  This indicates to the call
# that we want to get the output of the matmul op back.
#
# All inputs needed by the op are run automatically by the session.  They
# typically are run in parallel.
#
# The call 'run(product)' thus causes the execution of threes ops in the
# graph: the two constants and matmul.
#
# The output of the op is returned in 'result' as a numpy `ndarray` object.
result = sess.run(product)
print(result)
# ==> [[ 12.]]

# Close the Session when we're done.
sess.close()

Sessions最後須要關閉,以釋放相關的資源;你也可使用with模塊,session在with模塊中自動會關閉:

with tf.Session() as sess:
  result = sess.run([product])
  print(result)

TensorFlow的這些節點最終將在計算設備(CPUs,GPus)上執行運算。若是是使用GPU,默認會在第一塊GPU上執行,若是你想在第二塊多餘的GPU上執行:

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)
    ...

device中的各個字符串含義以下:

  • "/cpu:0": 你機器的CPU;
  • "/gpu:0": 你機器的第一個GPU;
  • "/gpu:1": 你機器的第二個GPU;

關於TensorFlow中GPU的使用見這裏

交互環境下的使用

以上的python示例中,使用了SessionSession.run()來執行圖計算。然而,在一些Python的交互環境下(如IPython中),你可使用InteractiveSession類,以及Tensor.eval()Operation.run()等方法。例如,在交互的Python環境下執行如下代碼:

# Enter an interactive TensorFlow Session.
import tensorflow as tf
sess = tf.InteractiveSession()

x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0])

# Initialize 'x' using the run() method of its initializer op.
x.initializer.run()

# Add an op to subtract 'a' from 'x'.  Run it and print the result
sub = tf.sub(x, a)
print(sub.eval())
# ==> [-2. -1.]

# Close the Session when we're done.
sess.close()

Tensors

TensorFlow中使用tensor數據結構(實際上就是一個多維數據)表示全部的數據,並在圖計算中的節點之間傳遞數據。一個tensor具備固定的類型、級別和大小,更加深刻理解這些概念可參考Rank, Shape, and Type

變量(Variables)

變量在圖執行的過程當中,保持着本身的狀態信息。下面代碼中的變量充當了一個簡單的計數器角色:

# 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))

# output:

# 0
# 1
# 2
# 3

賦值函數assign()add()函數相似,直到session的run()以後纔會執行操做。與之相似的,通常咱們會將神經網絡模型中的參數表示爲一系列的變量,在模型的訓練過程當中對變量進行更新操做。

抓取(Fetches)

爲了抓取ops的輸出,須要先執行sessionrun函數。而後,經過print函數打印狀態信息。

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() as sess:
  result = sess.run([mul, intermed])
  print(result)

# output:
# [array([ 21.], dtype=float32), array([ 7.], dtype=float32)]

全部tensors的輸出都是一次性 [連貫] 執行的。

填充(Feeds)

TensorFlow也提供這樣的機制:先建立特定數據類型的佔位符(placeholder),以後再進行數據的填充。例以下面的程序:

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()的變量進行數據填充,將會引起錯誤,更多的例子可參考MNIST fully-connected feed tutorial (source code)

示例:曲線擬合

下面是一段使用Python寫的,曲線擬合計算。官網將此做爲剛開始介紹的示例程序。

# 簡化調用庫名
import tensorflow as tf
import numpy as np

# 模擬生成100對數據對, 對應的函數爲y = x * 0.1 + 0.3
x_data = np.random.rand(100).astype("float32")
y_data = x_data * 0.1 + 0.3

# 指定w和b變量的取值範圍(注意咱們要利用TensorFlow來獲得w和b的值)
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b

# 最小化均方偏差
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

# 初始化TensorFlow參數
init = tf.initialize_all_variables()

# 運行數據流圖(注意在這一步纔開始執行計算過程)
sess = tf.Session()
sess.run(init)

# 觀察屢次迭代計算時,w和b的擬合值
for step in xrange(201):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(W), sess.run(b))

# 最好的狀況是w和b分別接近甚至等於0.1和0.3
相關文章
相關標籤/搜索