Academic
  • Introduction
  • Artificial Intelligence
    • Introduction
    • AI Concepts, Terminology, and Application Areas
    • AI: Issues, Concerns and Ethical Considerations
  • Biology
    • Scientific Method
    • Chemistry of Life
    • Water, Acids, Bases
    • Properties of carbon
    • Macromolecules
    • Energy and Enzymes
    • Structure of a cell
    • Membranes and transport
    • Cellular respiration
    • Cell Signaling
    • Cell Division
    • Classical and molecular genetics
    • DNA as the genetic material
    • Central dogma
    • Gene regulation
  • Bioinformatics
    • Bioinformatics Overview
  • Deep Learning
    • Neural Networks and Deep Learning
      • Introduction
      • Logistic Regression as a Neural Network
      • Python and Vectorization
      • Shallow Neural Network
      • Deep Neural Network
    • Improving Deep Neural Networks
      • Setting up your Machine Learning Application
      • Regularizing your Neural Network
      • Setting up your Optimization Problem
      • Optimization algorithms
      • Hyperparameter, Batch Normalization, Softmax
    • Structuring Machine Learning Projects
    • Convolutional Neural Networks
      • Introduction
    • Sequence Models
      • Recurrent Neural Networks
      • Natural Language Processing & Word Embeddings
      • Sequence models & Attention mechanism
  • Linear Algebra
    • Vectors and Spaces
      • Vectors
      • Linear combinations and spans
      • Linear dependence and independence
      • Subspaces and the basis for a subspace
      • Vector dot and cross products
      • Matrices for solving systems by elimination
      • Null space and column space
    • Matrix transformations
      • Functions and linear transformations
      • Linear transformation examples
      • Transformations and matrix multiplication
      • Inverse functions and transformations
      • Finding inverses and determinants
      • More Determinant Depth
  • Machine Learning
    • Introduction
    • Linear Regression
      • Model and Cost Function
      • Parameter Learning
      • Multivariate Linear Regression
      • Computing Parameters Analytically
      • Octave
    • Logistic Regression
      • Classification and Representation
      • Logistic Regression Model
    • Regularization
      • Solving the Problem of Overfitting
    • Neural Networks
      • Introduction of Neural Networks
      • Neural Networks - Learning
    • Improve Learning Algorithm
      • Advice for Applying Machine Learning
      • Machine Learning System Design
    • Support Vector Machine
      • Large Margin Classification
      • Kernels
      • SVM in Practice
  • NCKU - Artificial Intelligence
    • Introduction
    • Intelligent Agents
    • Solving Problems by Searching
    • Beyond Classical Search
    • Learning from Examples
  • NCKU - Computer Architecture
    • First Week
  • NCKU - Data Mining
    • Introduction
    • Association Analysis
    • FP-growth
    • Other Association Rules
    • Sequence Pattern
    • Classification
    • Evaluation
    • Clustering
    • Link Analysis
  • NCKU - Machine Learning
    • Probability
    • Inference
    • Bayesian Inference
    • Introduction
  • NCKU - Robotic Navigation and Exploration
    • Kinetic Model & Vehicle Control
    • Motion Planning
    • SLAM Back-end (I)
    • SLAM Back-end (II)
    • Computer Vision / Multi-view Geometry
    • Lie group & Lie algebra
    • SLAM Front-end
  • Python
    • Numpy
    • Pandas
    • Scikit-learn
      • Introduction
      • Statistic Learning
  • Statstics
    • Quantitative Data
    • Modeling Data Distribution
    • Bivariate Numerical Data
    • Probability
    • Random Variables
    • Sampling Distribution
    • Confidence Intervals
    • Significance tests
Powered by GitBook
On this page
  • Hyperparameter Tuning
  • Tunning Process
  • Appropriate Scale for Picking Hyperparameters
  • Practices
  • Batch Normalization (BN)
  • Adding BN to a NN
  • Why BN works ?
  • BN at test time
  • Softmax Regression
  • Softmax Cost Function
  • Softmax Gradient Descent
  • Deep Learning Frameworks

Was this helpful?

  1. Deep Learning
  2. Improving Deep Neural Networks

Hyperparameter, Batch Normalization, Softmax

Hyperparameter Tuning

  • 下面來 ranking 一下目前所出現過的 hyperparameters 重要度

  • 最重要

    • Learning rate α\alphaα

  • 第二重要

    • Momentum parameter β≈0.9\beta \approx 0.9β≈0.9

    • Number of hidden units nln^{l}nl

    • Mini-Batch size

  • 第三重要

    • Number of layers LLL

    • Learning rate decay Decay-rate\text{Decay-rate}Decay-rate

  • 不怎麼需要調整

    • Adam parameters β1,β2,ϵ\beta_1, \beta_2, \epsilonβ1​,β2​,ϵ

Tunning Process

  • 我們可以一次測試多種 hyperparameters 的組合

  • 但不要使用 grid 當作測試方法

    • 假設你的 (x, y) 選用最重要跟最不重要的 (α,ϵ)(\alpha, \epsilon)(α,ϵ)

    • 那麼你用 grid 方式等於只是在測試五種 α\alphaα 而已

  • 隨機選擇測試組合

    • 這是相對不錯的測試方法

  • Coarse to fine

    • 基於隨機選擇,在測試時順便聚焦到測試結果不錯的區域,繼續測試該區域

Appropriate Scale for Picking Hyperparameters

  • 在隨機選取 hyperparameters 時要注意數值的區間 scale

  • 例如隨機選取 layers 或 units,線性區間是非常合理的事

    • n = [50, 100], L = [2, 4]

  • 但在選取 α,β\alpha, \betaα,β 時,不同區間代表的意義不同

    • 例如 β\betaβ 從 0.9 增加至 0.9005 幾乎沒影響

      • 依然是在取大約前 10 個值的平均

    • 但是 β\betaβ 從 0.999 增加至 0.9995

      • 代表原本取前 1000 個值的平均

      • 變成取前 2000 個值的平均

  • 所以對這些 hyperparameters 必須先依區間拆分,然後再從每個區間隨機取值

  • α\alphaα 拆成 0.0001, 0.001, 0.01, 0.1

  • β\betaβ 取 1−β1-\beta1−β 然後像 α\alphaα 一樣拆成 0.1, 0.01, 0.001

Practices

  • 考慮數據、伺服器等環境變化,最好每隔幾個月就要更新你的 hyperparameters,來獲得當前最好的模型

  • Deep learning 中,hyperparameters 可能可以跨領域設定,所以時常關注不同領域的應用可以獲得靈感

  • 在訓練模型時,有兩種方法

    • Panda (babysitting one model)

      • 硬體有限、模型困難,所以無法一次測試大量模型時採用

      • 快速建置、開始測試、不斷調整參數

      • 就像熊貓一樣專心的照顧自己小孩

    • Caviar (training many model parallelly)

      • 有足夠硬體、自己足以應付多個模型

      • 一次平行測試多種 hyperparameters 的結果,並挑選最好的

      • 就像魚一次產下上億個魚卵,看看能不能有很棒的小孩出現

Batch Normalization (BN)

  • Batch Normalization (BN) 有許多好處

    • 訓練速度提升

    • 快速尋找 hyperparameters,所以尋找範圍更加旁大

    • 能夠使 NN 不會受 hyperparameters 影響,更加穩定

  • 那我們能不能把這個方法,也運用在每個 hidden layers 的 activation units (A[l]A^{[l]}A[l]) 呢 ?

    • 答案是可以的,而且能夠加速 W[l+1],b[l+1]W^{[l+1]}, b^{[l+1]}W[l+1],b[l+1] 的訓練

    • 實作時通常會 normalize Z[l]Z^{[l]}Z[l],實作如下

      μ=1m∑iz(i)σ2=1m∑i(zi−μ)2znorm(i)=z(i)−μσ2+ϵ\begin{aligned} \mu &= \frac{1}{m}\sum_i z^{(i)}\\ \sigma^2 &= \frac{1}{m}\sum_i (z_i - \mu)^2\\ z_\text{norm}^{(i)} &= \frac{z^{(i)}-\mu}{\sqrt{\sigma^2+\epsilon}} \end{aligned}μσ2znorm(i)​​=m1​i∑​z(i)=m1​i∑​(zi​−μ)2=σ2+ϵ​z(i)−μ​​
    • 此時所有新的 z(i)z^{(i)}z(i) 的 mean = 0, variance = 1

    • 但我們不想讓每個 z(i)z^{(i)}z(i) 都一樣是 mean = 0, variance = 1

      • 以 sigmoid 為假設,這樣 normalize 後,每一個 z(i)z^{(i)}z(i) 都會在 sigmoid 的 linear 區間

      • 將會不利於訓練 non-linear neural network,進而得到不好的模型

    • 所以我們再次計算

      z~(i)=γ×znorm(i)+β\tilde{z}^{(i)} = \gamma \times z_\text{norm}^{(i)} + \betaz~(i)=γ×znorm(i)​+β
      • 這個 γ,β\gamma, \betaγ,β 跟 w,bw, bw,b 一樣都是 parameters,可以隨著 gradient descent 更新

      • 最終這個 z~(i)\tilde{z}^{(i)}z~(i) 就可以取代 z(i)z^{(i)}z(i)

Adding BN to a NN

  • 所以現在利用 w,bw, bw,b 計算完 zzz 之後

  • 再用 γ,β\gamma, \betaγ,β 計算 z~\tilde{z}z~,以此類推下去

  • Backpropogation 一樣就會計算出 dγ,dβd\gamma, d\betadγ,dβ 來更新 γ,β\gamma, \betaγ,β

  • 另外由於 BN 包含減去 mean μ\muμ 的動作,使得 bbb 參數變得沒有作用

    • 會變成由 β\betaβ 來取代 bbb 的工作

    • 所以在 BN 中,可以把 bbb 省略或設為 0

Why BN works ?

  1. 因為對每個 neuron 的 input 都做了 normalization,所以提高整體訓練速度

  2. 因為 normalization 的關係,所有前面層的 weights 對後面層影響減少,更加精確

    • 每一層之間的 weights 耦合度減少,所以更加獨立,並提升自我學習的強度

    • 減緩了 "Covariate Shift" 的影響

  3. BN 也起到了些微的 regularization 效果

    • 因為是在每個 mini-batch 重新計算 mean, variance

    • 這樣計算的結果會產生一些 noise,這些 noise 產生了類似 dropout 的效果

      • 若 mini-batch size 越大,這個效果會越小

    • 不要把 regularization 當作使用 BN 的主要原因

BN at test time

  • BN 能在 mini-batch 時運作良好

  • 但在運行 test set 時,資料是一筆一筆出現,無法得到 μ,σ2\mu, \sigma^2μ,σ2

  • 解決方法 : 當訓練每一個 mini-batch 時,會得到每一個 μ{i}[l],σ2{i}[l]\mu^{\{i\}[l]}, \sigma^{2\{i\}[l]}μ{i}[l],σ2{i}[l]

    • 將這些參數當成 θi\theta_iθi​ 進行 exponentailly weighted averages

    • 預測出最終給 test 時使用的 μ,σ2\mu, \sigma^2μ,σ2 即可

Softmax Regression

  • 要得到多個種類的 classification,我們可以使用 Softmax 作為最後一層的 activation function

  • 我們使用 CCC 作為 classification 的種類數目

  • 最後一層計算出 Z : Z[L]=W[L]a[L−1]+b[L]Z^{[L]} = W^{[L]}a^{[L-1]} + b^{[L]}Z[L]=W[L]a[L−1]+b[L]

  • 對 ZZZ 向量的每一個 ziz_izi​ 使用 softmax activation function

    ai[L]=eZi[L]∑i=1CeZi[L]a_i^{[L]} = \frac{e^{Z_i^{[L]}}}{\sum_{i=1}^C e^{Z_i^{[L]}}}ai[L]​=∑i=1C​eZi[L]​eZi[L]​​
    • 所有的 ai[L]a_i^{[L]}ai[L]​ 加起來要等於 1

      ∑i=1Cai[L]=1\sum_{i=1}^C a_i^{[L]} = 1∑i=1C​ai[L]​=1

  • 用實際例子舉例

Softmax Cost Function

  • Loss function : L(y^,y)=−∑j=1Cyjlog⁡yj^\mathcal{L}(\hat{y}, y) = -\sum_{j=1}^C y_j\log \hat{y_j}L(y^​,y)=−∑j=1C​yj​logyj​^​

  • 在 yyy vector 中因為只有第 i 個 entry 為 1

    • 所以其他都等於 0 yj=0 if j≠iy_j = 0 \text{ if } j \neq iyj​=0 if j=i

  • 所以 Loss function 可以簡化成 L(y^,y)=−log⁡yj^\mathcal{L}(\hat{y}, y) = -\log\hat{y_j}L(y^​,y)=−logyj​^​

    • 也就是要讓 yj^\hat{y_j}yj​^​ 越大,cost 才會越接近 0

  • 於是 Cost Function 等於

    J=1m∑i=1mL(y^,y)J = \frac{1}{m}\sum_{i=1}^m \mathcal{L}(\hat{y}, y)J=m1​i=1∑m​L(y^​,y)

Softmax Gradient Descent

  • Softmax regression 在回推 backpropogation 時的算法跟 logistic 時一模一樣

  • 只是以向量模式取代實數

  • dZ[L]=y^−y∈R(C,1)dZ^{[L]} = \hat{y} - y \in \mathbb{R}^{(C, 1)}dZ[L]=y^​−y∈R(C,1)

Deep Learning Frameworks

  • 現在有非常多種框架能夠簡單應用 deep learning

    • Caffe / Caffe 2

    • CNTK

    • DL4J

    • Keras

    • Lasagne

    • mxnet

    • PaddlePaddle

    • TensorFlow

    • Theano

    • Torch

  • 但選擇時最好基於以下幾個重點

    1. 方便編寫程式

    2. 運行速度要快 (特別是大型數據)

    3. 是否真正開放 (不但是開源、而且需要良好管理、能夠持續更新)

PreviousOptimization algorithmsNextStructuring Machine Learning Projects

Last updated 5 years ago

Was this helpful?

在先前我們學過

Normalize input