# 【模型品質與改進】 - Part1. 簡介 - Part2. 基本的評估指標 - Part3. Overfitting and Generalization - Part4. Regularization Methods ## Part1. 簡介 - 模型使用 Training data 來學習參數 - Validation data 是我們在訓練期間用來對模型進行公正評估的資料集(每個 epoch 訓練完出現) - Test data 通常是指我們最終測試模型效能的資料集(Ex: Kaggle competetion) - **Validation** 和 **Test** 常常被交換使用 <center> <img src="https://hackmd.io/_uploads/ry_SBhGgke.png" style=" width: 90%; height: auto;"> <div style=" border-bottom: 3px solid #d9d9d9; display: inline-block; color: #999; padding: 3px;"> </div> </center> <center> <img src="https://hackmd.io/_uploads/SyBwr2feJl.png" style=" width: 90%; height: auto;"> <div style=" border-bottom: 3px solid #d9d9d9; display: inline-block; color: #999; padding: 3px;"> </div> </center> <br> ## Part2. 基本的評估指標 #### MNIST Confusion Matrix Example <center> <img src="https://hackmd.io/_uploads/rk4Kr3MxJg.png" style=" width: 70%; height: auto;"> <div style=" border-bottom: 3px solid #d9d9d9; display: inline-block; color: #999; padding: 3px;"> </div> </center> #### 二元分類 <center> <img src="https://hackmd.io/_uploads/S1lDor2flyl.png" style=" width: 70%; height: auto;"> <div style=" border-bottom: 3px solid #d9d9d9; display: inline-block; color: #999; padding: 3px;"> </div> </center> - **Accuracy** \begin{equation} \frac{(TP + TN)}{Total} = \frac{40 + 90}{145} = 0.897 \end{equation} - Misclassification or Error Rate \begin{equation} 1 - Accuracy = 1 - 0.897 = 0.103 \end{equation} - True Positive Rate or Sensitivity or **Recall** \begin{equation} \frac{TP}{TruePositiveLabels} = \frac{90}{10+90} = 0.9 \end{equation} :::info **注意事項**: 患有COVID的病人中,我們預測出了多少 ::: - False Positive Rate = FP / TrueNegativeLabels = 5 / 45 = 0.11 \begin{equation} \frac{FP}{TrueNegativeLabels} = \frac{5}{45} = 0.11 \end{equation} :::info **注意事項**: 沒有患COVID的病人中,我們誤判了多少患者有得 COVID ::: - True Negative Rate or Specificity \begin{equation} \frac{TN}{TrueNegativeLabels} = \frac{40}{45} = 0.89 \end{equation} :::info **注意事項**: 沒有患COVID的病人中,我們正確預測出了多少 ::: - **Precision** \begin{equation} \frac{TP}{PredictedYes} = \frac{90}{95} \end{equation} :::info **注意事項**: 我們預測有得 COVID的病患,有多少是正確的 ::: #### Precision, Recall, F1 - Precision/Recall tradeoff - 通常提升 Precision 或 Recall 會導致另一方下降 - 有時候我們可以接受犧牲一些Precision來提高Recall (看診) - F1 - Precision和Recall的調和平均數(harmonic mean) <center> <img src="https://hackmd.io/_uploads/rygSLnGgJg.png" style=" width: 70%; height: auto;"> <div style=" border-bottom: 3px solid #d9d9d9; display: inline-block; color: #999; padding: 3px;"> </div> </center> :::success **重點筆記**: Precision 和 Recall 的定義 - Precision 是模型預測正確的機率 - Recall 是模型找到所有正確物件的表現 - 當模型 Precision 高,但 Recall 低,代表對正例的預測非常精準,但錯過了大部分正例 ::: #### Classification Report - Support代表各類別出現的次數 <center> <img src="https://hackmd.io/_uploads/H1OUUhGeyg.png" style=" width: 70%; height: auto;"> <div style=" border-bottom: 3px solid #d9d9d9; display: inline-block; color: #999; padding: 3px;"> </div> </center> <br> ## Part3. Overfitting and Generalization #### 如何避免Overfitting - **起因**: 1. 當模型在訓練資料上過度訓練,並在測試資料上表現很差 (很常發生) 2. 當我們的**訓練資料太少**、使用太多Features、或使用太複雜的模型 3. Generalization 代表模型在沒見過的資料上的表現 <center> <img src="https://hackmd.io/_uploads/By9LsuXxyg.png" style=" width: 50%; height: auto;"> <div style=" border-bottom: 3px solid #d9d9d9; display: inline-block; color: #999; padding: 3px;"> </div> </center> - **解決方法**: 1. 最簡單方法:增加訓練資料或降低模型複雜度 2. 使用 Regularization:限制模型的複雜度,並讓模型透過正確的特徵學習 3. Example:我們希望模型能夠學習和狗相關的特徵 (尾巴、鼻子、耳朵),而不是其他一般的特徵 (樹、草) <br> ## Part4. Regularization Methods ### L1 & L2 Regularization - 迫使模型使用較小的參數 (weights and biases) - 透過資料正則化,幫助模型的訓練收斂的更快,限制模型的複雜度 - 避免特定的節點產生過大的影響 - L1會優先將不重要的特徵的權重降為0: 特徵選取 - L2會優先將所有的權重減小: 大的權重會產生更大的Penalty \begin{equation} Loss Function + Penalty_{(L1 \& L2)} \end{equation} - L1 Regularization \begin{equation} Loss Function + \lambda \sum_{j=1}^{p} |\beta_j| \end{equation} 1. $\beta_j$: 參數 2. $\lambda$: 控制 Penalty 的效果 3. 越大的 $\lambda$ 代表 Penalty 越重 - L2 Regularization \begin{equation} Loss Function + \lambda \sum_{j=1}^{p} \beta^2_j \end{equation} 1. $\beta_j$: 參數 2. $\lambda$: 控制 Penalty 的效果 3. 越大的 $\lambda$ 代表 Penalty 越重 :::success **重點筆記**: - L1和L2都會使模型使用較小的權重 - 除非使用大的權重可以顯著降低原有的Loss - 在較小的權重和較低的原有Loss間做選擇 (透過 $\lambda$ 控制) ::: ### Dropout - Dropout影響的不是訓練損失,而是**模型本身** - 在訓練過程中,隨機關閉某一層的一些節點 :::success **重點筆記**: - 隨機關閉一些節點 (ex: 一半) - 將一個 Mini-Batch 前向傳遞,並用反向傳遞計算梯度 - 更新開放的節點的權重 - 恢復所有節點、再次隨機關閉一些節點、並傳遞下一組 Mini-Batch - 使用Dropout Rate來控制關閉節點的數量 - 迫使模型學習更可靠的特徵 (每次訓練不一樣的模型 ) - Dropout 也會增加模型 Converge 所需要的時間 - 在測試模型時,我們會使用所有的節點來做預測 <center> <img src="https://hackmd.io/_uploads/r1kSRuQlkx.png" style=" width: 50%; height: auto;"> <div style=" border-bottom: 3px solid #d9d9d9; display: inline-block; color: #999; padding: 3px;"> </div> </center> ::: ### Data Augmentation - 其中一個導致 Overfitting 的原因:沒有足夠的訓練資料 - 使用 Data Augmentation 可以解決這個問題 - 圖像資料特別適合使用 Data Augmentation - Data Augmentation in Keras and Pytorch 1. Keras 和 Pytorch 都提供許多基本的 Data Augmentation 方法: 翻轉 (水平/垂直)、亮度和對比、旋轉、縮放、裁切、扭曲 - 一般只會對訓練資料進行資料擴增 - Data Augmentation 通常發生在 Data Loader 搬運資料時 (相當於每次讓模型看不同版本的圖片) ### Early Stopping - 我們可以設定一個 threshold,告訴模型如果在接下來的 P 個Epochs都沒有更好的結果,就停止訓練 - 在每個Epoch,我們都會儲存模型的權重 <center> <img src="https://hackmd.io/_uploads/Hk0zyFml1l.png" style=" width: 50%; height: auto;"> <div style=" border-bottom: 3px solid #d9d9d9; display: inline-block; color: #999; padding: 3px;"> </div> </center> - Early Stopping in Keras and Pytorch 1. In Keras: 使用Callback 2. In Pytorch: 需要手動實作 ### Batch Normalization - 個別對每組 Mini-Batch 計算 mean 和 STD 以及進行標準化 - 針對 CNN ,每一層會有 d 個 means 和 STDs (取決於有幾個 filters ) - 通常會設在 Conv Layer 和 Activation Function Layer 之間 - 假如有使用 Dropout,層的順序為:Conv -> BacthNorm -> ReLU -> Dropout <center> <img src="https://hackmd.io/_uploads/HkxWxFXlJg.png" style=" width: 70%; height: auto;"> <div style=" border-bottom: 3px solid #d9d9d9; display: inline-block; color: #999; padding: 3px;"> </div> </center> ### Regularization 小建議 1. 不要一開始就使用 Regularization (建立Baseline Model) 2. 有時 Regularization 會對模型性能產生不利影響(例如,如果我們使用一些錯誤的參數設定) 3. Dropout 和 Batch Norm 會增加訓練時間 4. Dropout: 不要在 Softmax Layer 的前一層使用。(這裡指的 softmax layer 應該改成 softmax function 更為準確) 5. 很簡單的模型不太需要使用 Regularization 6. 更多 Epochs 7. 如果 L2 Penalty 太高,模型可能會 Underfitting