Deep Learning - chanandrew96/MyLearning GitHub Wiki

Deep Learning is a part of AI, which is insupervised learning
** Starting point for beginner: **

Type Reference
Theory 手刻 Deep Learning — 第零章 — 線性回歸
手刻 Deep Learning — 第零章 — 微分觀念入門

Framework

  • PyTorch
  • Tensorflow
  • Theano
  • Keras
  • Dynet

PyTorch

Tensorflow

TensorFlow中可用的類型(DType)

當我們新建一個Tensor物件時若沒有定義其所用的dtype,TensorFlow會自動選取一個最合適的dtype使用
除了常用的floatsints以外還有多種可用的類型:

  • Complex Numbers
  • Strings

TensorFlow DType和Python的轉換/對應類別

TF dtype Python Type
tf.int32 Integers
tf.float32 Floating point numbers

TensorFlow中Tensor的形狀 (Shapes)

基本概念可參考前文
若我們需要進行可視化或建立相關的圖表時,我們可以使用tf.ranktf.shape來取得Tensor物件(Object)

# For return with Tensor Object
print("Number of axes:", tf.rank([YOUR_TENSOR]))
print("Shape of tensor:", tf.shape([YOUR_TENSOR]))

另外有一些非四方體型的Tensor:

Broadcast

當我們處理兩個不同大小的Tensor(如相乘)的時候,較小的Tensor會進行被擴張,而這個動作被稱為Broadcasting
Boardcasting並不會實際擴大Tensor,所以Broadcasting實際上有比較好的效能

Most of the time, broadcasting is both time and space efficient, as the broadcast operation never materializes the expanded tensors in memory.

如以下例子我們將一個3x1的Tensor和1x4的Tensor相乘,它們便會被Boardcasting

# These are the same computations
x = tf.reshape(x,[3,1])
y = tf.range(1, 5)
print(tf.multiply(x, y))

# Boardcasting simulation
x_stretch = tf.constant([[1, 1, 1, 1],
                         [2, 2, 2, 2],
                         [3, 3, 3, 3]])
y_stretch = tf.constant([[1, 2, 3, 4],
                         [1, 2, 3, 4],
                         [1, 2, 3, 4]])
print(x_stretch * y_stretch)  # Again, operator overloading

Result of Boardcasting

轉換成Tensor

大多數時候TF的功能如tf.matmultf.reshape都取tf.Tensor類別為輸入,但大多數Python物件只需要有Tensor的格式也是可以被接受的(自動轉換)
但當我們使用了未能自動轉換的物件時就需要使用tf.convert_to_tensor將該物件轉換為tf.Tensor類別

Ragged Tensors

當Tensor裡的軸線上有未知長度的時候我們需要使用Ragged Tensors來儲存該Tensor數據
而我們取得Ragged Tensors的Shape時當中會包含了Unknown長度
Ragged Tensors Sample

Sparse Tensors

當我們需要儲存一些稀疏矩陣的資料時便要用到Sparse Tensor來確保數據當中的空白
Sparse Tensors Sample

String Tensors

用於儲存tf.string的數據

其他功能在TensorFlow中的實現

  • Tensor的運算和比較(Basic Math & Ops)
  • 索引(Indexing)
  • 重塑Tensor形狀(Manipulating/Reshaping Tensor)
  • Tensor間的類別轉換(Casting between dtype)
  • Boardcasting
  • Ragged Tensors

詳細的運算和結果可以參考CoLab/ipynb的結果


References

PyTorch - Python Deep Learning Neural Network API
TensorFlow - Tensors