Tensorflow學習筆記1:Get Started


關於Tensorflow的基本介紹

Tensorflow是一個基於圖的計算系統,其主要應用於機器學習。

從Tensorflow名字的字面意思可以拆分成兩部分來理解:Tensor+flow。

  • Tensor:中文名可以稱為“張量”,其本質就是任意維度的數組。一個向量就是一個1維的Tensor,一個矩陣就是2維的Tensor。
  • Flow:指的就是圖計算中的數據流。

當我們想要使用Tensorflow做什么事情的時候,一般需要三個操作步驟:

  1. 創建Tensor;
  2. 添加Operations(Operations輸入Tensor,然后輸出另一個Tensor);
  3. 執行計算(也就是運行一個可計算的圖)。

Tensorflow有個圖的概念,Operations會添加到圖中,作為圖的節點。在添加某Operation的時候,不會立即執行該Operation。Tensorflow會等待所有Operation添加完畢,然后Tensorflow會優化該計算圖,以便決定如何執行計算。

 

快速使用

如果想開始實驗一下Tensorflow,可以通過Docker啟動官方的鏡像tensorflow/tensorflow

如下所示,

lienhuadeMacBook-Pro:tensorflow lienhua34$ docker run -d -p 8888:8888 --name tensorflow tensorflow/tensorflow
0fc7849b3ef5ac56e8ad372cc201874338c586ed5f47a4205997712efcd35646
lienhuadeMacBook-Pro:tensorflow lienhua34$ docker ps -a
CONTAINER ID        IMAGE                                    COMMAND                  CREATED             STATUS                   PORTS                              NAMES
0fc7849b3ef5        tensorflow/tensorflow                    "/run_jupyter.sh"        49 minutes ago      Up 2 seconds             6006/tcp, 0.0.0.0:8888->8888/tcp   tensorflow

該鏡像啟動一個jupyter,然后我們在瀏覽器中輸入http://localhost:8888/來訪問,如下圖所示,

然后點擊右上角的New -> Python 2,新建的一個Python交互頁面,便可以開始實驗Tensorflow的功能,

 

簡單實例:向量相加

下面我們通過兩個向量相加的簡單例子來看一下Tensorflow的基本用法。

[1. 1. 1. 1.] + [2. 2. 2. 2.] = [3. 3. 3. 3.]
import tensorflow as tf
with tf.Session():
  input1 = tf.constant([1.0 1.0 1.0 1.0])
  input2 = tf.constant([2.0 2.0 2.0 2.0])
  output = tf.add(input1, input2)
  result = output.eval()
  print result

Tensorflow的計算必須要在一個Session的上下文中。Session會包含一個計算圖,而這個圖你添加的Tensors和Operations。當然,你在添加Tensor和Operation的時候,它們都不會立即進行計算,而是等到最后需要計算Session的結果的時候。當Tensorflow之后了計算圖中的所有Tensor和Operation之后,其會知道如何去優化和執行圖的計算。

兩個tf.constant() 語句向計算圖中創建了兩個Tensor。調用tf.constant()的動作大致可以說為,創建兩個指定維度的Tensor,以及兩個constant操作符用於初始化相對應的Tensor(不會立即執行)。

tf.add()語句向計算圖中添加了一個add操作,當不會立即執行,這時候add操作的結果還無法獲取。此時,計算圖大致如下所示, addvec.jpg

result = output.eval() print result

當我們最后調用output.eval()時,會觸發Tensorflow執行計算圖,從而獲取output計算結點的結果。

 

Variable的使用

我們上面的例子使用的Tensor是常量(constant),而在我們實際的機器學習任務中,我們往往需要變量(variable)來記錄一下可變的狀態(例如神經網絡節點的權重參數等)。下面我們來看一個簡單的variable例子。

import tensorflow as tf
import numpy as np

with tf.Session() as sess:
  # Set up two variables, total and weights, that we'll change repeatedly.
  total = tf.Variable(tf.zeros([1, 2]))
  weights = tf.Variable(tf.random_uniform([1,2]))

  # Initialize the variables we defined above.
  tf.initialize_all_variables().run()

  # This only adds the operators to the graph right now. The assignment
  # and addition operations are not performed yet.
  update_weights = tf.assign(weights, tf.random_uniform([1, 2], -1.0, 1.0))
  update_total = tf.assign(total, tf.add(total, weights))

  for _ in range(5):
    # Actually run the operation graph, so randomly generate weights and then
    # add them into the total. Order does matter here. We need to update
    # the weights before updating the total.
    sess.run(update_weights)
    sess.run(update_total)

    print weights.eval(), total.eval()

概括了說,上面的代碼就是創建了兩個變量total和weights(都是1維的tensor),total所有元素初始化為0,而weights的元素則用-1到1之間的隨機數進行初始化。然后在某個迭代中,使用-1到1之間的隨機數來更新變量weights的元素,然后添加到變量total中。

在調用tf.Variable()的時候,只是定了變量以及變量的初始化操作(實際上並未執行)。所有變量都需要在開始執行圖計算之前進行初始化。調用tf.initialize_all_variables().run()來對所有變量進行初始化。

在for循環中,

sess.run(update_weights)

觸發執行更新weights變量的計算。

sess.run(update_total)

則處理了將變量total和變量weights進行相加,並將結果賦值到變量total。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM