Reflection: Deep Learning with R-Ch2.1–2.3
Ch 2.1–2.3
## Part1-Ch2 Before we begin: the mathematical building blocks of neural networks
### 2.1. A FIRST LOOK AT A NEURAL NETWORK
* 分析 MNIST 資料集就像是 deep learning 領域的 “hello world”
- 灰階手寫數字(0~9)圖片(28 * 28 pixels)
- 資料集有 60000 張訓練資料,10000 張測試資料
- The images are encoded as 3D arrays, and the labels are a 1D array of digits, ranging from 0 to 9.
* class: a category in a classification problem
* samples: data points
* label: the class associated with a specific sample
* workflow:
1. 餵模型資料 training data(train_image & train_labels)
2. 模型訓練
3. 用測試資料產生預測值
4. 與測試 label 比對
* 神經網絡的核心構建模塊是”層”,它是一個數據處理模塊,可以將其視為數據過濾器。
* 深度學習模型就像數據處理的篩子一樣,由一系列不斷完善的數據過濾器組成 — 過濾器就是 layer
* compile 重要部分:
1. loss function: 網絡將如何測量其在訓練數據上的表現,以及如何將自身引導至正確的方向。
2. optimizer: 網絡根據其看到的數據及其損失函數更新自身權重的機制。
3. metrics: 指標 (ex: accuracy)
* R-code:
1. compile() 函數在適當的地方修改了網絡(而不是像常見的那樣返回新的網絡物件)。
2. 資料前處理:
- reshape: 使用 array_reshape() 而非 <- dim()
- scaling to [0, 1] interval
3. output 有兩個數值 training data loss, training data accuracy
### 2.2. DATA REPRESENTATIONS FOR NEURAL NETWORKS
* 存儲在多維數組(也稱為張量)中的數據。張量是向量和矩陣到任意維的廣義化。
#### 2.2.1. Scalars (0D tensors)
* 僅包含一個數字的張量稱為純量(或純量張量或零維張量或0D張量)。
#### 2.2.2. Vectors (1D tensors)
* 一維數字數組稱為向量或一維張量。一維張量恰好具有一個軸。
* 5D向量僅具有一個軸,沿著此軸具有五個維度
* 5D張量具有五個軸(並且沿每個軸可以具有任意數量的維度)。
* 維度可以表示 1.沿特定軸的項數(例如5D向量) 2.也可以表示張量中的軸數(例如5D張量)。
#### 2.2.3. Matrices (2D tensors)
* 數字的二維數組是矩陣或2D張量。
#### 2.2.4. 3D tensors and higher-dimensional tensors
* 如果將這樣的矩陣打包到新的數組中,則會獲得3D張量。通過將3D張量打包成一個數組,可以創建4D張量,依此類推。
* 在深度學習中,通常會處理0D到4D的張量,如果處理視頻數據則可能會達到5D。
#### 2.2.5. Key attributes
* tensor 具有以下三個關鍵屬性
- 軸數(rank):3D張量具有三個軸,矩陣具有兩個軸。length(dim(train_images))
- shape:是一個整數向量,描述張量沿每個軸的維度(dim)。length(dim(train_images))
- data type:張量中包含的數據的類型,如 integer or double。length(dim(train_images))
#### 2.2.7. The notion of data batches
* 在深度學習中會遇到的所有數據張量中的第一個軸將是樣本軸(有時稱為樣本維)。
* 深度學習模通常不會一次處理整個數據集,而是將數據分成小批。
* 考慮批次張量時,第一個軸稱為批次軸或批次尺寸。(batch axis or batch dimension)
* batch <- train_images[129:256, ,]
#### 2.2.8. Real-world examples of data tensors
* Vector data: 2D tensors of shape (samples, features)
* Time-series data or sequence data: 3D tensors of shape (samples, timesteps, features)
* Images: 4D tensors of shape (samples, height, width, channels) or (samples, channels, height, width)
* Video: 5D tensors of shape (samples, frames, height, width,
channels) or (samples, frames, channels, height, width)
#### 2.2.9. Vector data
* 2D tensors of shape (samples, features)
* 每個數據點都可以被編碼為向量,因此一批數據將被編碼為2D張量(即向量數組)。
#### 2.2.10. Time-series data or sequence data
* 3D tensors of shape (samples, time-steps, features)
* 每個樣本可以被編碼為向量序列(2D張量),因此一批數據將被編碼為3D張量。
* The time axis is always the second axis
#### 2.2.11. Image data
* 圖片資料通常有三個維度: height, width, and color depth.
* 一批128張彩色圖像可以以張量形狀(128, 256, 256, 3)存儲
* 來自Google的TensorFlow機器學習框架將顏色深度軸放置在末尾: (samples, height, width, color_depth)。
* Theano將顏色深度軸放置在批處理軸之後:(samples, color_depth, height, width)。
#### 2.2.12. Video data
* 一批不同的視頻可以存儲在5D張量中形狀 (samples, frames, height, width, color_depth)。
### 2.3. THE GEARS OF NEURAL NETWORKS: TENSOR OPERATIONS
#### 2.3.1. Element-wise operations
* Elementwise addition: z <- x + y
* Elementwise relu: z <- pmax(z, 0)
#### 2.3.2. Operations involving tensors of different dimensions
* sweep(x, 2, y, `+`): 能夠在高維張量和低維張量之間執行操作。
* 兩個不同維度的的張量,可對兩個張量共同的某幾個維度之間操作。
#### 2.3.3. Tensor dot
* 向量乘法 %*%
#### 2.3.4. Tensor reshaping
* 使用 array_reshape() 函數而不是 dim <- () 函數來重塑數組。數據將使用行重新解釋(與R的默認列相反),這又與 Keras(NumPy,TensorFlow等)解釋數組的方式兼容。
#### 2.3.5. Geometric interpretation of tensor operations
* 仿射變換,旋轉,縮放等基本幾何運算可以表示為張量運算。
#### 2.3.6. A geometric interpretation of deep learning
* 假設有兩張色紙弄皺揉成一團紙球,弄皺的紙球是輸入數據,每張紙都是分類問題中的一類數據。神經網絡(或任何其他機器學習模型)的作用是弄清楚紙球的變形,使之平坦,從而使這兩類再次可分離。通過深度學習,這將實現為3D空間的一系列簡單轉換,例如可以一次用一個手指移動到紙球上的那些轉換。
* 弄皺的紙球就是機器學習的意義:找到復雜的,高度折疊的數據流形的簡潔表示。
* 在數學中,流形是局部類似於每個點附近的歐幾里得空間的拓撲空間。