最近看到一份不錯的深度學習資源——Stanford中的CS20SI:《TensorFlow for Deep Learning Research》,正好跟着學習一下TensorFlow的基礎,仍是收穫頗豐,隨手整理成博客隨時翻閱。java
自從12年AlexNet得到ImageNet大賽的冠軍後,深度學習開始流行起來,也由於硬件的快速發展GPU並行計算配合易用的API,讓深度學習以及神經網絡大放光彩。node
深度學習的框架其實有不少,目前來講最火的還要數Pytorch、TensorFlow以及Keras。其中Pytorch比較適合學術研究,本身搞着玩,若是工業實踐就不太適合了。TensorFlow因爲時間比較久,學起來比較困難,不過有完整的開發、部署方案,還有大量的github項目可供參考。Keras則是TensorFlow的一個高級API,同類的還有TensorFlow的TFLearn等等。linux
總結來講,若是你是學生,只是爲了論文或者學習,那麼推薦Pytorch;若是你是公司的開發者,想要在業務中使用深度學習,推薦直接使用TensorFlow,若是使用最新的1.12,那麼官方的示例裏面就已是Keras了;若是你是從github上面下載了源碼想要學習,那就得去學習對應版本的TensorFlow API了。ios
在總結一下Tensoflow的優勢:git
使用TensorFlow的公司包括:Google,OpenAI,DeepMind,SnapChat,Airbus,eBay等github
基於TensorFlow能夠作不少事情,好比圖像CV、天然語言處理NLP、語音識別等等。web
下面就來學習下TensorFlow的基礎知識,TensorFlow不只提供了基礎的語法,還提供了一些簡化的API:json
若是作過大數據或者接觸過java8的流計算,對這種數據流圖應該比較瞭解。就是咱們在程序執行前,先構建好計算的流程框架,而後執行的時候現去讀取數據分配資源執行計算。這樣一方面把構建與計算分離,另外一方面也能夠代碼本身作更深的優化。windows
好比上面的數據流圖中,事先定義好整個網絡的結構,而後計算的時候直接傳入5和3,就能獲得結果23了。api
張量,不是張亮,更不是麻辣燙,它是一種高維數據的統稱。好比:
所以TensorFlow,能夠理解爲Tensor+Flow,即張量的數據流。
import tensorflow as tf # 第一個例子,計算兩個數的加法 a = tf.constant(2) b = tf.constant(3) x = tf.add(a, b) with tf.Session() as sess: print(sess.run(x))
在上面的代碼中,就構建了一個最基本的數據流圖的計算例子。
其中
a = tf.constant(2) b = tf.constant(3) x = tf.add(a, b)
就是在構建圖。而想要拿到x的值,就必須新建一個session(這個時候纔會分配資源),執行run方法(這個時候纔會執行)。
爲了方便查看構建圖,須要學會怎麼使用TensorBoard。在上面的代碼中,只須要增長Tensorboard的聲明便可:
import tensorflow as tf # 第一個例子,計算兩個數的加法 a = tf.constant(2) b = tf.constant(3) x = tf.add(a, b) with tf.Session() as sess: writer = tf.summary.FileWriter('./graphs', sess.graph) print(sess.run(x)) writer.close()
而後在命令行中輸入
tensorboard --logdir=/Users/xingoo/PycharmProjects/xxx/graphs
登陸localhost:6006
就能夠看到下面的內容。
能夠看到左邊有描述每一個節點的意思,點擊add後,能夠到關於add節點的描述。因爲構建的圖很簡單,就是兩個數相加,所以整個圖只有三個圈圈。而且按照默認的操做進行了命名。
增長如下圖的複雜度,而且同時對兩個結果計算:
import tensorflow as tf # tf.constant(value, dtype=None, shape=None, name='Const', verify_shape=False) a = tf.constant([1, 3], name="a") b = tf.constant([[0, 1], [2, 3]], name="b") x = tf.add(a, b, name="add") y = tf.multiply(a, b, name="mul") with tf.Session() as sess: writer = tf.summary.FileWriter('./graphs', sess.graph) x, y = sess.run([x, y]) print(x) print(y) writer.close()
因爲x、y是獨立運算沒有什麼交集,所以在圖中,他們是獨立的兩個操做。
回頭再來看看tensorFlow中的圖究竟是什麼呢?當使用第5部分中的代碼構建graph時,能夠直接輸出graph的定義:
import tensorflow as tf a = tf.constant(2) b = tf.constant(3) x = tf.add(a, b) with tf.Session() as sess: print(sess.graph.as_graph_def())
獲得以下的內容:
node { name: "Const" op: "Const" attr { key: "dtype" value { type: DT_INT32 } } attr { key: "value" value { tensor { dtype: DT_INT32 tensor_shape { } int_val: 2 } } } } node { name: "Const_1" op: "Const" attr { key: "dtype" value { type: DT_INT32 } } attr { key: "value" value { tensor { dtype: DT_INT32 tensor_shape { } int_val: 3 } } } } node { name: "Add" op: "Add" input: "Const" input: "Const_1" attr { key: "T" value { type: DT_INT32 } } } versions { producer: 26 }
每一個node基本都包含下面你的內容:
{ name:咱們本身起的名字,若是沒有則是op+自增的數, op:操做 attr: 類型 attr:值 { 形狀、初始值 } }
經過上面的json,就能完美的組合出web中看到的圖了。
至於graph到底怎麼用,就看後面一節課的內容吧。