Wednesday, June 23, 2021

TensorFlow

TensorFlow

2019/04/16

前言:

-----



Fig. Flow(圖片來源)。

-----

Summary:

-----

Outline

一、Tensor
二、Frameworks
三、TensorFlow in a Nutshell
四、TensorFlow 2.0

-----


Fig.1.1a. Tensor [1]。

-----


Fig.1.1b. Tensor [2]。

-----

一、Tensor

But we don’t have to restrict ourselves to linear algebra. As it turns out, we can move up the mathematical food chain a bit. It has long been known that there are bigger fish in the mathematical abstraction sea than just matrices. One such candidate is called a tensor. Tensors figure prominently in the mathematical underpinnings of general relativity and are fundamental to other branches of physics as well. But just as the mathematical concepts of matrices and vectors can be simplified down to the arrays we use in computers, tensors can also be simplified and represented as multidimensional arrays and some related operations. Unfortunately, things aren’t quite as simple as they were with matrices and vectors, largely because there isn’t an obvious and simple set of operations to be performed on tensors like there were with matrices and vectors.
長期以來,人們都知道在數學抽象的海洋中存在著比矩陣還要大的魚,這其中一個候選就是張量(tensor)。張量是廣義相對論重要的數學基礎,此外它對於物理學的其它分支來說也具有基礎性的地位。那麼如同數學的矩陣和向量概念可被簡化成我們在計算機中使用的數組一樣,我們是否可以將張量也簡化和表徵成多維數組和一些相關的運算呢?很不幸,事情沒有那麼簡單,這其中的主要原因是不存在一個顯而易見且簡單的(如在矩陣和向量上類似的)可在張量上進行的一系列運算。
// Deep Learning 101  Demystifying Tensors

A tensor is often thought of as a generalized matrix. That is, it could be a 1-D matrix (a vector is actually such a tensor), a 3-D matrix (something like a cube of numbers), even a 0-D matrix (a single number), or a higher dimensional structure that is harder to visualize. The dimension of the tensor is called its rank.
張量通常被認為是一個廣義矩陣。 也就是說,它可以是 1-D 矩陣(向量實際上是這樣的張量),3-D 矩陣(類似於數字的立方體),甚至是 0-D 矩陣(單個數字),或者更難視覺化的更高維的結構。 張量的維度稱為其 rank。
// What’s the difference between a matrix and a tensor

-----


Fig. 1.2a. Tensor [3]。

-----


Fig. 1.2b. Code [4]。

// TensorFlow 101:基礎數值運算 – DataInPoint – Medium

「張量是現代機器學習的基礎。它的核心是一個數據容器,多數情況下,它包含數字,有時候它也包含字符串,但這種情況比較少。因此把它想像成一個數字的水桶。」
// 浅谈什么是张量tensor - So What - CSDN博客

-----


Fig. Army(圖片來源)。

-----

數字的水桶可能還是很抽象,不如以軍隊的編制說明比較具體,男生可能很容易瞭解,對女生或許也會有一些幫助。

這一兩天沈醉在張量裡面,從網路上蒐集了很多文章。數學與物理的部分,其實很難讀懂,深度學習的部分,配合圖示,倒是一下子就很清楚了。今天早上,我回憶起在成功嶺的單位,一師四旅九營二連。連,即是最基本的張量!

單兵:純量。班:向量。排:矩陣。連:張量。營:連的向量。旅:連的矩陣。師:連的向量。轉置矩陣,可以想成把班長抽出獨立成一班,副班長也抽出成一班,等等。

-----


Fig. 2. Deep Learning Frameworks

// Battle of the Deep Learning frameworks — Part I  2017, even more frameworks and interfaces

-----

二、Frameworks

「Facebook 開源 PyTorch,成為 TensorFlow 強敵 Theano 完成歷史使命,功成身退 ONNX 開放平台橫空出世,力圖促進開放的開發生態 CNTK,Keras,MXNet 均迎來重大更新 微軟攜手亞馬遜推出 Gluon 深度學習庫,英特爾推出增強學習框架 Coach 針對移動終端,Caffe2 和 TensorFlow Lite 先後開源」
// 迎來 PyTorch,告別 Theano,2017 深度學習框架發展大盤點 _ 香港矽谷

「1. 張量(Tensor) 2. 基於張量的各種操作 3. 計算圖(Computation Graph) 4. 自動微分(Automatic Differentiation)工具 5. BLAS、cuBLAS、cuDNN等拓展包」
// The Anatomy of Deep Learning Frameworks – Medium

「對於 Theano 的退出,有開發者這樣評價,「它開啟了基於符號運算的機器學習框架的先河,然後隨着 TensorFlow 和 PyTorch 的崛起,完成自己的歷史任務。」」目前「Theano 雖然已經停止更新了,但基本上所有的框架都參考過 Theano,它並不算徹底消失。」
 // 迎來 PyTorch,告別 Theano,2017 深度學習框架發展大盤點 _ 香港矽谷

-----



Fig. 3.1. TensorFlow

// TensorFlow — A hands-on approach – Towards Data Science

-----

三、TensorFlow in a Nutshell

「TensorFlow 的名稱,凸顯了這個框架的兩個重要性質,靜態的資料結構設計成 Tensor 張量,能夠一次存放多屬性的資料,這讓我們可以用張量來描述我們要讓電腦進行機器學習的物件,像是一張包含 RGB 的彩色圖片。另外一方面 Flow,則是表達運算的動態路徑流,TensorFlow 利用 Graph 來表示 Tensor 的運算加工流程,以方便產生自動微分 AD,這是執行反向傳播來修正類神經網路權重的核心過程。所以如果有人問你,TensorFlow 的原理是什麼?她最重要的特點是什麼?我認為答案已經在「Tensor」「Flow」這兩個詞彙上面了。」
// Tensor張量的定義 – Epic

-----


Fig. 3.2
// TensorFlow in a Nutshell - DZone AI

-----

So, What Are the Benefits of Using Graphs? Parallelism: By using explicit edges to represent dependencies between operations, it is easy for the system to identify operations that can execute in parallel. Distributed execution: By using explicit edges to represent the values that flow between operations, it is possible for TensorFlow to partition your program across multiple devices (CPUs, GPUs, and TPUs) attached to different machines. TensorFlow inserts the necessary communication and coordination between devices. Compilation: TensorFlow’s XLA compiler can use the information in your dataflow graph to generate faster code, for example, by fusing together adjacent operations. Portability: The dataflow graph is a language-independent representation of the code in your model. You can build a dataflow graph in Python, store it in a SavedModel, and restore it in a C++ program for low-latency inference.

那麼,使用圖表的好處是什麼?

並行性:通過使用顯式邊來表示操作之間的依賴關係,系統很容易識別可以並行執行的操作。

分佈式執行:通過使用顯式邊來表示在操作之間流動的值,TensorFlow可以將程序劃分到連接到不同計算機的多個設備(CPU,GPU和TPU)上。 TensorFlow在設備之間插入必要的通信和協調。

編譯:TensorFlow的XLA編譯器可以使用數據流圖中的信息生成更快的代碼,例如,通過將相鄰操作融合在一起。

可移植性:數據流圖是模型中代碼的獨立於語言的表示形式。 您可以在Python中構建數據流圖,將其存儲在SavedModel中,並在C ++程序中將其還原以進行低延遲推理。


-----


Fig. 3.3. TensorFlow toolkit hierarchy
// TensorFlow — A hands-on approach – Towards Data Science


-----

TensorFlow provides a variety of different tool kits that allow you to write code at your preferred level of abstraction. For instance, you can write code in the Core TensorFlow (C++) and call that method from Python code. You can also define the architecture on which your code should run (CPU, GPU etc.). In the above hierarchy, the lowest level in which you can write your code is C++ or Python. These two levels allow you to write numerical programs to solve mathematical operations and equations. Although this is not highly recommended for building Machine Learning models, it offers a wide range of math libraries that ease your tasks. The next level in which you can write your code is using the TF specific abstract methods which are highly optimised for model components. For example, using the tf.layers method abstract you can play with the layers of a neural net. You can build a model and evaluate the model performance using the tf.metrics method. The most widely used level is the tf.estimator API, which allows you to build (train and predict) production ready models with easy. The estimator API is insanely easy to use and well optimised. Although it offers less flexibility, it has all that is needed to train and test your model. Let’s see an application of the estimator API to build a classifier using just three lines of code.

TensorFlow提供了各種不同的工具包,允許您以您喜歡的抽象級別編寫代碼。例如,您可以在Core TensorFlow(C ++)中編寫代碼並從Python代碼中調用該方法。您還可以定義應運行代碼的體系結構(CPU,GPU等)。在上面的層次結構中,您可以編寫代碼的最低級別是C ++或Python。這兩個級別允許您編寫數值程序來解決數學運算和方程。雖然這不是強烈建議用於構建機器學習模型,但它提供了各種數學庫,可以簡化您的任務。您可以編寫代碼的下一個級別是使用特定於TF的抽象方法,這些方法針對模型組件進行了高度優化。例如,使用tf.layers方法摘要,您可以使用神經網絡的圖層。您可以使用tf.metrics方法構建模型並評估模型性能。最廣泛使用的級別是tf.estimator API,它允許您輕鬆地構建(訓練和預測)生產就緒模型。估算器API非常易於使用且經過優化。雖然它提供的靈活性較低,但它具備培訓和測試模型所需的一切。讓我們看一下估算器API的應用程序,僅使用三行代碼構建分類器。



-----


-----


Fig. 4. TensorFlow 2.0

// What’s coming in TensorFlow 2.0 – TensorFlow – Medium

-----

四、TensorFlow 2.0

「使用 Keras 和 eager execution,輕鬆建立簡單的模型並執行。 在任何平台達成生產環境的模型部署。 為研究提供強大的實驗工具。 清除不建議使用的 API 和減少重複來簡化 API。」
// TensorFlow 2.0 Alpha 版来了!吴恩达配套课程同步上线 _ 雷锋网

「此外,TensorFlow 2.0 Alpha 版還有些新功能,允許研究人員和進階用戶使用豐富的延伸去實驗,如 Ragged Tensors、TensorFlow Probability、Tensor2Tensor 等。」
// TensorFlow 2.0 Alpha 版来了!吴恩达配套课程同步上线 _ 雷锋网

◎ Ragged Tensor

Gluon 支持循環和不規則張量(ragged tensors)(能批處理變長度序列),對於 RNN 和 LSTM 模型具有空前的效率。
// 迎來 PyTorch,告別 Theano,2017 深度學習框架發展大盤點 _ 香港矽谷

「在處理很多機器學習現實任務的時候,我們常常會面臨形狀分佈不固定的Tensor,比如,在處理文本分類任務中,一句話中包含的單詞的數目是可變的,在語音識別中,輸入的音頻的長度是可變的,在語音合成中,輸入的文本的長度是可變的。一般這個時候,我們需要設定一個固定的長度,對於過長的輸入,就進行截斷;而對於過短的輸入,就用特殊數值補齊。 在TF 2.0中,我們迎來了一個新的解決方法,那就是」Ragged Tensor。
// TensorFlow 2.0新特性之Ragged Tensor_tf
 
◎ Tensor2Tensor

「Tensor2Tensor(T2T)是 Google Brain Team 在 Github 上開源出來的一套基於 TensorFlow 的深度學習系統。該系統最初是希望完全使用 Attention 方法來建模序列到序列(Sequence-to-Sequence,Seq2Seq)的問題,對應於《Attention Is All You Need》這篇論文。該項工作有一個有意思的名字叫“Transformer”。隨著系統的不斷擴展,T2T 支持的功能變得越來越多,目前可以建模的問題包括:圖像分類,語言模型、情感分析、語音識別、文本摘要,機器翻譯。 T2T 在很多任務上的表現很好,並且模型收斂比較快,在 TF 平台上的工程化代碼實現的也非常好,是一個十分值得使用和學習的系統。」
// “变形金刚”Google Tensor2Tensor系统 - 知乎

◎ TensorFlow Probability

「TensorFlow Probability(TFP)是一個基於 TensorFlow 的 Python 庫,能夠更容易地結合概率模型和深度學習。數據科學家、統計學以及機器學習研究者或者從業者,都可以用它編碼領域知識 (Domain Knowledge),從而理解數據並寫出自己的應用。」
// TensorFlow Probability 概率編程入門級實操教程 _ 香港矽谷

-----

Conclusion


師,連的張量?

-----

References

一、Tensor

# Tensor 圖示 
Learning AI if You Suck at Math — P4 — Tensors Illustrated (with Cats!)
https://hackernoon.com/learning-ai-if-you-suck-at-math-p4-tensors-illustrated-with-cats-27f0002c9b32

# Tensor 圖示
From Scalar to Tensor  Fundamental Mathematics for Machine Learning with Intuitive Examples Part…
https://medium.com/datadriveninvestor/from-scalar-to-tensor-fundamental-mathematics-for-machine-learning-with-intuitive-examples-part-163727dfea8d

# Tensor 圖示 
WTF is a Tensor !
https://www.kdnuggets.com/2018/05/wtf-tensor.html

# Tensor 代碼
TensorFlow 101:基礎數值運算 – DataInPoint – Medium
https://medium.com/datainpoint/tf101-tf-as-s-calculator-7d51c66882bb

-----

What’s the difference between a matrix and a tensor
https://medium.com/@quantumsteinke/whats-the-difference-between-a-matrix-and-a-tensor-4505fbdc576c

Deep Learning 101  Demystifying Tensors
https://www.kdnuggets.com/2017/06/deep-learning-demystifying-tensors.html 
深度学习初学者必读:张量究竟是什么? _ 机器之心
https://www.jiqizhixin.com/articles/2017-06-20-2

# TensorFlow
浅谈什么是张量tensor - So What - CSDN博客
https://blog.csdn.net/qq_31821675/article/details/79188449

# TensorFlow
Tensor張量的定義 – Epic
https://1fly2sky.wordpress.com/2018/10/25/tensor%E5%BC%B5%E9%87%8F%E7%9A%84%E5%AE%9A%E7%BE%A9/

-----

// 數學、物理

# 數學
通俗理解张量tensor - 简书
https://www.jianshu.com/p/2a0f7f7735ad 

# 數學
[線性代數]張量 – 尼斯的靈魂
https://frankliou.wordpress.com/2012/09/03/%E7%B7%9A%E6%80%A7%E4%BB%A3%E6%95%B8%E5%BC%B5%E9%87%8F/

# 數學、物理
Tensor - Wikipedia
https://en.wikipedia.org/wiki/Tensor
 
# 數學、物理 
張量 - 維基百科,自由的百科全書
https://zh.wikipedia.org/wiki/%E5%BC%B5%E9%87%8F

# 數學、物理
什么是张量 (tensor)? - 知乎
https://www.zhihu.com/question/20695804

-----

二、Frameworks

# 框架
Battle of the Deep Learning frameworks — Part I  2017, even more frameworks and interfaces
https://towardsdatascience.com/battle-of-the-deep-learning-frameworks-part-i-cff0e3841750 
2017年深度学习框架之争——看谁主沉浮? - 简书
https://www.jianshu.com/p/9c2685862fd4

# 框架、核心
The Anatomy of Deep Learning Frameworks – Medium
https://medium.com/@gokul_uf/the-anatomy-of-deep-learning-frameworks-46e2a7af5e47
深度学习框架太抽象?其实不外乎这五大核心组件 _ 雷锋网
https://www.leiphone.com/news/201701/DZeAwe2qgx8JhbU8.html

# 框架、Ragged Tensor
迎來 PyTorch,告別 Theano,2017 深度學習框架發展大盤點 _ 香港矽谷
https://www.hksilicon.com/articles/1509257 

-----

三、TensorFlow in a Nutshell

# 786 claps
Tensorflow for absolute beginners – Towards Data Science
https://towardsdatascience.com/tensorflow-for-absolute-beginners-28c1544fb0d6

TensorFlow Tutorial For Beginners (article) - DataCamp
https://www.datacamp.com/community/tutorials/tensorflow-tutorial

9 Things You Should Know About TensorFlow – Hacker Noon
https://hackernoon.com/9-things-you-should-know-about-tensorflow-9cf0a05e4995

TensorFlow in a Nutshell - DZone AI
https://dzone.com/articles/tensorflow-in-a-nutshell

TensorFlow Tutorial For Beginners (article) - DataCamp
https://www.datacamp.com/community/tutorials/tensorflow-tutorial
 
Understand TensorFlow by mimicking its API from scratch
https://medium.com/@d3lm/understand-tensorflow-by-mimicking-its-api-from-scratch-faa55787170d

TensorFlow — A hands-on approach – Towards Data Science
https://towardsdatascience.com/tensorflow-a-hands-on-approach-8614372f021f

从框架优缺点说起,这是一份TensorFlow入门极简教程 _ 机器之心
https://www.jiqizhixin.com/articles/052401

-----

四、TensorFlow 2.0

TensorFlow is dead, long live TensorFlow! – Hacker Noon
https://hackernoon.com/tensorflow-is-dead-long-live-tensorflow-49d3e975cf04

What’s coming in TensorFlow 2.0 – TensorFlow – Medium
https://medium.com/tensorflow/whats-coming-in-tensorflow-2-0-d3663832e9b8

TensorFlow 2.0 Alpha 版来了!吴恩达配套课程同步上线 _ 雷锋网
https://www.leiphone.com/news/201903/hFAVIz7ZiCsGBx7C.html
TensorFlow 2.0 Alpha 版來了!吳恩達配套課程同步上線 _ TechNews 科技新報
http://technews.tw/2019/03/08/tensorflow-2-0-alpha/

-----

◎ Ragged Tensor

Introducing Ragged Tensors – TensorFlow – Medium
https://medium.com/tensorflow/introducing-ragged-tensors-ac301c31fd38

TensorFlow 2.0新特性之Ragged Tensor_tf
http://www.sohu.com/a/306300588_723464

-----

◎ Tensor2Tensor

Training Cutting-Edge Neural Networks with Tensor2Tensor and 10 lines of code
https://medium.com/data-from-the-trenches/training-cutting-edge-neural-networks-with-tensor2tensor-and-10-lines-of-code-10973c030b8

Tensor2Tensor and One Model to Learn them all – Towards Data Science
https://towardsdatascience.com/tensor2tensor-and-one-model-to-learn-them-all-7ef3f9b61ba4

“变形金刚”Google Tensor2Tensor系统 - 知乎
https://zhuanlan.zhihu.com/p/39348901

-----

◎ TensorFlow Probability

An introduction to probabilistic programming, now available in TensorFlow Probability
https://medium.com/tensorflow/an-introduction-to-probabilistic-programming-now-available-in-tensorflow-probability-6dcc003ca29e

TensorFlow Probability 概率編程入門級實操教程 _ 香港矽谷
https://www.hksilicon.com/articles/1730267

No comments: