# **Deep Learning 筆記** ###### tags: `神經網路與深度學習` `deep learning` `CNN` Github: https://github.com/YunghuiHsu/deep-learning-from-scratch ## CH2 感知器 Perception / 人工神經元 $y = \begin{cases}0 \quad (w_1x_1+w_2x_2 \le \theta) \\1 \quad (w_1x_1+w_2x_2 \ > \theta)\end{cases}$ * $\theta$ : * 臨界值。 * 或b [偏權值] => 控制神經元的發火難度 * *w* : * 權重。相當於電流中的電阻。 * 權重越重,對應於該權重的訊號越重要。 * 控制各訊號的重要性 <br/> ### ch2.2 邏輯電路 ![NAND](https://i.imgur.com/ewKzCDe.png =300x) ![NAND](https://i.imgur.com/Wxq0gyY.png =200x) #### 實作AND_gate ``` # coding: utf-8 import numpy as np def AND(x1, x2): x = np.array([x1, x2]) w = np.array([0.5, 0.5]) b = -0.7 tmp = np.sum(w*x) + b if tmp <= 0: return 0 else: return 1 if __name__ == '__main__': for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]: y = AND(xs[0], xs[1]) print(str(xs) + " -> " + str(y)) ``` ### ch2.5 多層感知器 * XORT Gate 無法線性分隔(NAND、NOT、OR均可線性分隔) ps: X表輸出為0,O表輸出為1。 ![NAND](https://i.imgur.com/hVY9Kku.png =200x) ![XORT](https://i.imgur.com/AD647fQ.png =300x) * 但可以用曲線分隔! * 或著用多條線分隔(層疊) ![NAND](https://i.imgur.com/nIBka9Z.png =200x) <font color="#f00">! **單層感知器無法表現互斥閘門(無法分離非線性區域),但組合(層疊)感知器可以**</font> ### XOR gate \begin{array}{c c||c c||c} x_1 & x_2 & \alpha_1 & \alpha_2 & y\\ \hline 0&0&1&0&0\\ 0&1&1&1&1\\ 1&0&1&1&1\\ 1&1&0&1&0\\ \end{array} <br/> ![](https://i.imgur.com/giOpecn.png =500x) [Lecture 6: Neural Networks 6: solving XOR with a hidden layer](https://www.google.com/url?sa=i&url=https%3A%2F%2Ffreevideolectures.com%2Fcourse%2F3736%2Fneural-networks-and-backpropagation%2F6&psig=AOvVaw1AtO7V7aM9z6RicGEjc1nP&ust=1612254209323000&source=images&cd=vfe&ved=2ahUKEwiNoN-MocjuAhXbed4KHaOiARQQjhx6BAgAEBI) <br/> ![](https://i.imgur.com/WG5ob3U.png =500x) [Artificial Neural Network (draft) ](https://www.slideshare.net/Jboulie/ann-preso-draft) #### 實作XOR_gate ``` # coding: utf-8 from and_gate import AND from or_gate import OR from nand_gate import NAND def XOR(x1, x2): s1 = NAND(x1, x2) s2 = OR(x1, x2) y = AND(s1, s2) return y if __name__ == '__main__': for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]: y = XOR(xs[0], xs[1]) print(str(xs) + " -> " + str(y)) ``` # CH3 神經網路 ## 活化函數 activation function * 具有決定如何活化輸入訊號總和的功能 <br/> ![](https://i.imgur.com/kNvNA89.png =400x) [a) Step function and (b) sigmoid function. | Download Scientific Diagram](https://www.researchgate.net/figure/a-Step-function-and-b-sigmoid-function_fig3_257219838) * sigmoid 函數傳遞連續的實數訊號 * step函數傳遞0或1訊號 * 兩者皆為<font color="#f00">非線性</font>函數 !! 線性函數作為活化函數,無論加深多少層,均與原本的輸入維持線性關係 #### 實作step與sigmoid function ``` import numpy as np import matplotlib.pylab as plt def sigmoid(x): return 1 / (1 + np.exp(-x)) def step_function(x): return np.array(x > 0, dtype=np.int) # np.array(x>0) 會回傳布林陣列, 指定為int會回傳0 or 1值,也可以用 .astype(np.int) x = np.arange(-5.0, 5.0, 0.1) y1 = sigmoid(x) y2 = step_function(x) plt.plot(x, y1) plt.plot(x, y2, 'k--') plt.ylim(-0.1, 1.1) #図で描画するy軸の範囲を指定 plt.show() ``` ## ReLU函數 Rectified Linear Unit * $\ge 0 \Rightarrow$ 直接輸出 * $<0 \Rightarrow$ 0 <br/> ![](https://i.imgur.com/dfYcYE8.png =400x) [What is the ReLU layer in CNN?](https://www.quora.com/What-is-the-ReLU-layer-in-CNN) #### 實作ReLU function ``` # coding: utf-8 import numpy as np import matplotlib.pylab as plt def relu(x): return np.maximum(0, x) # 從輸入值當中選取較大的值來輸出 x = np.arange(-5.0, 5.0, 0.1) y = relu(x) plt.plot(x, y) plt.ylim(-1.0, 5.5) plt.show() ``` * 輸出層使用的活化函數須配合欲解決的問題性質而定 * 恆等函數 : 迴歸 * sigmoid : 2類別分類問題 * softmax : 多類別分類問題 ## Softmax函數 $y_k\ =\ \frac{exp(a_k)}{\sum_{i=1}^{n} \ exp(a_i)}$