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
  • Motivations
  • Example - Computer Vision
  • Neural Networks
  • Model Representation
  • Neuron
  • Notations
  • Vectorized implementation
  • Applications and Intuitions
  • AND
  • OR
  • NOR
  • XNOR
  • Multiclass Classification

Was this helpful?

  1. Machine Learning
  2. Neural Networks

Introduction of Neural Networks

PreviousNeural NetworksNextNeural Networks - Learning

Last updated 5 years ago

Was this helpful?

Motivations

想像我們為了解決上面這個 Non-linear classification

必須使用 sigmoid function 搭配很多的 parameters 和新 features

像上面用了兩個 features 就產生了可能 "Overfitting" 的 hypothesis

當我們有更多的 features 時,新 features 的數量將以指數成長

Example - Computer Vision

當我們要用機器學習來辨識圖片時 (舉例: 車子)

我們將整張圖片的每一個 pixel 截取出來,並給予 intensity (0-255 灰階)

我們將不同圖片的 pixel 資訊 plot 在一個二維空間中,用以分辨該 pixel 為多少時是否為車子

這種時候就可以用到 non-linear hypothesis

假設我們只要辨識 50 * 50 pixel 的小圖片

那我們將會用到 2500 的 features (7500 if RGB)

而產生的新 features 將會達到約 3 billion features

Neural Networks

一開始 neural networks 是為了試圖模擬人的大腦運作

大腦可以讓我們做幾乎任何事情,聽、說、讀、寫、看、觸 ...

而其實大腦只花了一種 learning algorithm 就做到這些事情

科學家用一種 Neural Rewiring 的實驗發現了這件事情

他們將動物接收聽覺的 Auditory Cortex REWIRE 接收視覺

他們發現 Auditory Cortex 變得能夠接收到視覺

Sensor Representation in the brain

還有許多將 Sensor 與大腦結合的發明

  • 用舌頭看東西

  • 人體聲納

  • Haptic belt

這些東西告訴我們

其實大腦只用了一種 algorithm 來教導身體做事

而我們的最終目標就是找到這個 learning algorithm 的 hypothesis

並且把他運用在電腦上

Model Representation

Neuron

Neuron 作為 neural networks 的最小單位,模擬人類的神經元

  • 透過 input wires (dendrites) 接收多個 input

  • 並從 output wires (axons) 將處理過的訊息傳送出去

當有多層 layer 時,我們稱第一層為 input layer 最後一層為 output layer

而中間所有的層統稱為 hidden layers

Notations

我們給予上圖 hidden layer 的 nodes 一個名字: Activation units

而這些 activation units 是經由 input 和 Matrix of Weights 作用 output 過來的

我們可以推出 matrix of weight 的 dimension :

例如 layer 1 有 2 個 nodes 而 layer 2 有 4 個 nodes

Vectorized implementation

也就是說在第 j 個 layer 的第 k 個 node,他的 z 就會是

總結來說,z 和 a 的公式如下

我們可以看到其實將最後一個 hidden layer 計算出 output layer 的過程

等同於將最後一個 hidden layer 的每個 activation units 作為 features 執行 logistic regression

而這些 activation units 其實又都是上一層的 nodes 執行 logistic regression 而來

所以這個方法稱作 Forward Propogation

意思是每一層都將計算出更有深度意義 (complex) 的 Hypothesis 至下一層

產出最好的 Hypothesis function

Applications and Intuitions

我們將使用 XNOR (NOT XOR) 的例子來解釋為何 neural networks 為何可以成功

x1

x2

XNOR

0

0

1

0

1

0

1

0

0

1

1

1

首先我們先來看 AND, OR 和 NOR 在 neural networks 的實作 :

AND

我們將 activation function 設成以下狀態

我們知道 sigmoid function 在大於 4.6 時差不多為 1

在小於 4.6 時差不多為 0

所以我們帶進去得到

x1

x2

0

0

0

1

1

0

1

1

OR

x1

x2

0

0

0

1

1

0

1

1

NOR

首先 Negation 的 hypothesis 產生如下

x1

0

1

而 NOR 其實就是 (NOT x1) AND (NOT x2)

x1

x2

0

0

0

1

1

0

1

1

XNOR

而 OR 則是在最後運算

Multiclass Classification

現在若 Neural networks 要一次處理多種問題

例如圖片辨識需要一次辨識出 Pedestrian, Car, Motorcycle, Truck

我們會將 y 和 hypothesis 以 vector 方式輸出

g(θ0+θ1x1+θ2x2+θ3x1x2+θ4x12x2+θ5x13x2+θ6x1x22+⋯ )g(\theta_0 + \theta_1x_1 + \theta_2x_2 + \theta_3x_1x_2 + \theta_4x_1^2x_2 + \theta_5x_1^3x_2 + \theta_6x_1x_2^2 + \cdots)g(θ0​+θ1​x1​+θ2​x2​+θ3​x1​x2​+θ4​x12​x2​+θ5​x13​x2​+θ6​x1​x22​+⋯)

其中 input 的是 features x1,x2,⋯xnx_1, x_2, \cdots x_nx1​,x2​,⋯xn​

而 output 就是 hypothesis function hθ(x)h_\theta(x)hθ​(x)

我們會在 input 再加上一個 x0x_0x0​ 作為 bias unit 永遠為 1

而處理這些 input 的為 sigmoid (logistic) activation function 11+e−θTx\frac{1}{1+e^{-\theta^Tx}}1+e−θTx1​

而在 neural networks 中 θ\thetaθ 這個 parameters 又稱作 "weight"

ai(j)=activation of unit i in layer jΘ(j)=matrix of weights controlling function mapping from layer j to layer j+1\begin{aligned} a_i^{(j)} &= \text{activation of unit } i \text{ in layer }j\\ \Theta^{(j)} &= \text{matrix of weights controlling function mapping from layer } j \text{ to layer } j + 1 \end{aligned}ai(j)​Θ(j)​=activation of unit i in layer j=matrix of weights controlling function mapping from layer j to layer j+1​

舉個例子來說 a1(2),a2(2),a3(2)a_1^{(2)}, a_2^{(2)}, a_3^{(2)}a1(2)​,a2(2)​,a3(2)​

就是由 3x4 的 Θ(1)\Theta^{(1)}Θ(1) 乘上 4x1 的 input x⃗\vec{x}x 向量而來

而 hypothesis hΘ(x)h_\Theta(x)hΘ​(x) 就是

hΘ(x)=a1(3)=g(Θ(2)a⃗(2))=g(Θ10(2)a0(2)+Θ11(2)a1(2)+Θ12(2)a2(2)+Θ13(2)a3(2))h_\Theta(x) = a_1^{(3)} = g(\Theta^{(2)}\vec{a}^{(2)}) = g(\Theta^{(2)}_{10}a_0^{(2)}+ \Theta^{(2)}_{11}a_1^{(2)} + \Theta^{(2)}_{12}a_2^{(2)} + \Theta^{(2)}_{13}a_3^{(2)})hΘ​(x)=a1(3)​=g(Θ(2)a(2))=g(Θ10(2)​a0(2)​+Θ11(2)​a1(2)​+Θ12(2)​a2(2)​+Θ13(2)​a3(2)​)

若 layer jjj 共有 sjs_jsj​ 個 units

且 layer j+1j+1j+1 共有 sj+1s_{j+1}sj+1​ 個 units

那麼 Θ(j)\Theta^{(j)}Θ(j) 的 dimension 為 sj+1×(sj+1)s_{j+1} \times (s_j + 1)sj+1​×(sj​+1)

那麼 Θ(1)\Theta^{(1)}Θ(1) 的 dimension 為 4 x 3

現在我們將每個 g function 內的 Θ\ThetaΘ 和 xxx 改為矩陣運算產生的 zzz

a1(2)=g(Θ10(1)x0+Θ11(1)x1+Θ12(1)x2+Θ13(1)x3)=g(z1(2))a2(2)=g(Θ20(1)x0+Θ21(1)x1+Θ22(1)x2+Θ23(1)x3)=g(z2(2))a3(2)=g(Θ30(1)x0+Θ31(1)x1+Θ32(1)x2+Θ33(1)x3)=g(z3(2))\begin{aligned} a_1^{(2)} = g(\Theta_{10}^{(1)}x_0 + \Theta_{11}^{(1)}x_1 + \Theta_{12}^{(1)}x_2 + \Theta_{13}^{(1)}x_3) = g(z_1^{(2)})\\ a_2^{(2)} = g(\Theta_{20}^{(1)}x_0 + \Theta_{21}^{(1)}x_1 + \Theta_{22}^{(1)}x_2 + \Theta_{23}^{(1)}x_3) = g(z_2^{(2)})\\ a_3^{(2)} = g(\Theta_{30}^{(1)}x_0 + \Theta_{31}^{(1)}x_1 + \Theta_{32}^{(1)}x_2 + \Theta_{33}^{(1)}x_3) = g(z_3^{(2)}) \end{aligned}a1(2)​=g(Θ10(1)​x0​+Θ11(1)​x1​+Θ12(1)​x2​+Θ13(1)​x3​)=g(z1(2)​)a2(2)​=g(Θ20(1)​x0​+Θ21(1)​x1​+Θ22(1)​x2​+Θ23(1)​x3​)=g(z2(2)​)a3(2)​=g(Θ30(1)​x0​+Θ31(1)​x1​+Θ32(1)​x2​+Θ33(1)​x3​)=g(z3(2)​)​
zk(j)=Θk,0(j−1)x0+Θk,1(j−1)x1+⋯+Θk,n(j−1)xnz_k^{(j)} = \Theta_{k,0}^{(j-1)}x_0 + \Theta_{k,1}^{(j-1)}x_1 + \cdots + \Theta_{k,n}^{(j-1)}x_nzk(j)​=Θk,0(j−1)​x0​+Θk,1(j−1)​x1​+⋯+Θk,n(j−1)​xn​

現在我們進一步將 input xxx 向量作為 a(1)a^{(1)}a(1),所以

z(2)=Θ(1)a(1)a(2)=g(z(2))\begin{aligned} z^{(2)} &= \Theta^{(1)}a^{(1)} \\ a^{(2)} &= g(z^{(2)}) \end{aligned}z(2)a(2)​=Θ(1)a(1)=g(z(2))​

然後 a(2)a^{(2)}a(2) 必須要補個 a0(2)=1a_0^{(2)} = 1a0(2)​=1 才能繼續

z(3)=Θ(2)a(2)a(3)=g(z(3))=hΘ(x)\begin{aligned} z^{(3)} &= \Theta^{(2)}a^{(2)} \\ a^{(3)} &= g(z^{(3)}) = h_\Theta(x) \end{aligned}z(3)a(3)​=Θ(2)a(2)=g(z(3))=hΘ​(x)​

其中的 n + 1 為 a0(1)=x0,a0(2),a0(3),⋯a_0^{(1)} = x_0, a_0^{(2)}, a_0^{(3)}, \cdotsa0(1)​=x0​,a0(2)​,a0(3)​,⋯

z(j)=Θ(j−1)a(j−1) where dim⁡(Θ(j−1))=sj×(n+1)a(j)=g(z(j))\begin{aligned} z^{(j)} &= \Theta^{(j-1)}a^{(j-1)} \text{ where } \dim({\Theta^{(j-1)}}) = s_j \times (n + 1)\\ a^{(j)} &= g(z^{(j)}) \end{aligned}z(j)a(j)​=Θ(j−1)a(j−1) where dim(Θ(j−1))=sj​×(n+1)=g(z(j))​

所以這個 hΘ(x)h_\Theta(x)hΘ​(x) 可以說是 AND 的 best hypothesis

這個 Neural network 產生的 hΘ(x)h_\Theta(x)hΘ​(x) 也可以說是 OR 的 best hypothesis

現在我們有了三種 Θ\ThetaΘ for AND, OR, NOR

AND Θ(1)=[−302020]NOR Θ(1)=[10−20−20]OR Θ(1)=[−102020]\begin{aligned} \text{AND } \Theta^{(1)} &= \begin{bmatrix} -30 & 20 & 20\end{bmatrix} \\ \text{NOR } \Theta^{(1)} &= \begin{bmatrix} 10 & -20 & -20\end{bmatrix} \\ \text{OR } \Theta^{(1)} &= \begin{bmatrix} -10 & 20 & 20\end{bmatrix} \\ \end{aligned}AND Θ(1)NOR Θ(1)OR Θ(1)​=[−30​20​20​]=[10​−20​−20​]=[−10​20​20​]​

我們將 AND 和 NOR 的 Θ\ThetaΘ 分別擺到第一層運算

Θ(1)=[−30202010−20−20]Θ(2)=[−102020]a(2)=g(Θ(1)⋅x)a(3)=g(Θ(2)⋅a(2))=hΘ(x)\begin{aligned} \Theta^{(1)} &= \begin{bmatrix} -30 & 20 & 20 \\ 10 & -20 & -20 \end{bmatrix} \\ \Theta^{(2)} &= \begin{bmatrix} -10 & 20 & 20 \end{bmatrix}\\\\ a^{(2)} &= g(\Theta^{(1)} \cdot x) \\ a^{(3)} &= g(\Theta^{(2)} \cdot a^{(2)}) = h_\Theta(x) \end{aligned}Θ(1)Θ(2)a(2)a(3)​=[−3010​20−20​20−20​]=[−10​20​20​]=g(Θ(1)⋅x)=g(Θ(2)⋅a(2))=hΘ​(x)​

當 training sets 為 (x(1),y(1)),(x(2),y(2)),⋯ ,(x(m),y(m))(x^{(1)}, y^{(1)}), (x^{(2)}, y^{(2)}), \cdots, (x^{(m)}, y^{(m)})(x(1),y(1)),(x(2),y(2)),⋯,(x(m),y(m)) 時

y(i)=[1000],[0100],[0010],[0001]y^{(i)} = \begin{bmatrix}1\\0\\0\\0\end{bmatrix}, \begin{bmatrix}0\\1\\0\\0\end{bmatrix}, \begin{bmatrix}0\\0\\1\\0\end{bmatrix}, \begin{bmatrix}0\\0\\0\\1\end{bmatrix}y(i)=​1000​​,​0100​​,​0010​​,​0001​​

若是路人,則 hΘ(x)≈[1000]h_\Theta(x) \approx \begin{bmatrix} 1\\0\\0\\0\end{bmatrix}hΘ​(x)≈​1000​​

若是車子,則 hΘ(x)≈[0100]h_\Theta(x) \approx \begin{bmatrix} 0\\1\\0\\0\end{bmatrix}hΘ​(x)≈​0100​​

以此類推 ... 試圖讓 hΘ(x(i))≈y(i)∣Both are R4h_\Theta(x^{(i)}) \approx y^{(i)} \mid \text{Both are } \mathbb{R}^4hΘ​(x(i))≈y(i)∣Both are R4

hΘ(x)h_\Theta(x)hΘ​(x)
g(−30)≈0g(-30) \approx 0g(−30)≈0
g(−10)≈0g(-10) \approx 0g(−10)≈0
g(−10)≈0g(-10) \approx 0g(−10)≈0
g(10)≈1g(10) \approx 1g(10)≈1
hΘ(x)h_\Theta(x)hΘ​(x)
g(−10)≈0g(-10) \approx 0g(−10)≈0
g(10)≈1g(10) \approx 1g(10)≈1
g(10)≈1g(10) \approx 1g(10)≈1
g(30)≈1g(30) \approx 1g(30)≈1
hΘ(x)h_\Theta(x)hΘ​(x)
g(10)≈1g(10) \approx 1g(10)≈1
g(−10)≈0g(-10) \approx 0g(−10)≈0
hΘ(x)h_\Theta(x)hΘ​(x)
g(10)≈1g(10) \approx 1g(10)≈1
g(−10)≈0g(-10) \approx 0g(−10)≈0
g(−10)≈0g(-10) \approx 0g(−10)≈0
g(−30)≈0g(-30) \approx 0g(−30)≈0