TensorFlow基礎

 

TensorFlow基礎

SkySeraph  2017html

Email:skyseraph00#163.comgit

更多精彩請直接訪問SkySeraph我的站點www.skyseraph.com 

Overview

TensorFlow 最初由Google Brain團隊開發, Google 2015年11月9日發佈的開源的AI系統(Apache 2.0),前任(身)爲DistBelief, 2017年2月11日發佈1.0.0版,同時支持Linux、macOS和Windows以及移動平臺,包括Android和iOS。
TensorFlow通常簡寫爲TF。
Awesome AI~github

AI/DL 庫

現在,AI/DL相關庫很是多,Deep Learning Libraries by Language 一文中以語言爲分割匯聚了幾十種DL庫,更多相關資料可參考這個項目的聚合awesome-ai
其中主流的幾種比較以下。
api

相比於其它AI庫,TF突出特色以下:session

  • Runs not only Windows, Linux, and macOS, also on mobile devices(iOS and Android.)
  • provides a Python API TF.Learn(Python API called TF.Learn2) compatible with Scikit-Learn (previously Scikit Flow/skflow)
  • provides a API TF-slim (tensorflow.contrib.slim) for neural networks.
  • Several other high-level APIs such as Keras or Pretty Tensor.
  • includes highly efficient C++ implementations of many ML operations
  • a great visualization tool TensorBoard
  • launched a cloud service
  • A dedicated team, a growing community

Keys

Graph/Tensor

  • Graph,圖。表示你所搭建的結構
  • Session,會話。表示你此次要運行程序
  • Tensor,張量 表示數據
  • Variable,表示變量
  • Feed和Fetch,進行任意的操做賦值或者獲取數據
  • Node,表示節點,代表數據在圖中流動通過的點
  • Operation,表示節點的操做,經過operation來加工處理數據
  • Data Flow Graph(數據流圖) TF用於定義計算結構的有向圖, 其節點表示運算(Operation(Op)),其邊表示數據(data set(tensors))的輸入和輸出(Op 傳入和從 Op 傳出)

TF中的圖描述了計算過程,圖經過Session的運行而執行計算。Session將圖的節點們(即ops)放置到計算設備(如CPUs和GPUs)上,而後經過方法執行它們;這些方法執行完成後,將返回tensors。在Python中的tensor的形式是numpy ndarray對象,而在C/C++中則是tensorflow::Tensor.
TF使用graph來表示計算任務/處理過程. 在被稱之爲Session的上下文 (context) 中執行圖. 使用 tensor 表示數據. 經過Variable 維護狀態. 使用 feed 和 fetch 能夠爲任意的操做(arbitrary operation) 賦值或者從其中獲取數據。數據結構

TF中使用Tensor來表示全部數據的數據結構,零階張量爲 純量或標量 (scalar) 也就是一個數值. 好比 [1] 一階張量爲 向量 (vector), 好比 一維的 [1, 2, 3] 二階張量爲 矩陣 (matrix), 好比 二維的 [[1, 2, 3],[4, 5, 6],[7, 8, 9]]。框架

TPU

TPU( tensor processing unit): 張量處理器, Google爲機器學習定製的專用芯片(ASIC),專爲Google的深度學習框架TensorFlow而設計。
與圖形處理器(GPU)相比,TPU採用低精度(8位)計算,以下降每步操做使用的晶體管數量。下降精度對於深度學習的準確度影響很小,但卻能夠大幅下降功耗、加快運算速度。同時,TPU使用了脈動陣列的設計,用來優化矩陣乘法與卷積運算,減小I/O操做。此外,TPU還採用了更大的片上內存,以此減小對DRAM的訪問,從而更大程度地提高性能。機器學習

TensorBoard

GPU設置

  • GPU device
    「/cpu:0」: 你機器的CPU;
    「/gpu:0」: 你機器的第一個GPU;
    「/gpu:1」: 你機器的第二個GPU;ide

  • 指定GPU。
    with tf.device(‘/cpu:1’):性能

    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
    ......
  • 資源分配。TF默認會使用當前全部GPU資源,控制資源能夠採用動態申請或者限制GPU使用率。

    • 動態申請:
      config = tf.ConfigProto()
      config.gpu_options.allow_growth = True
      session = tf.Session(config=config, …)

    • 限制GPU使用率:
      config = tf.ConfigProto()
      config.gpu_options.per_process_gpu_memory_fraction = 0.4
      session = tf.Session(config=config, …)

References & Recommends



By SkySeraph-2017

SkySeraph cnBlogs

本文首發於skyseraph.com「TensorFlow基礎」

相關文章
相關標籤/搜索