---
tags: cs231
---
# Lecture 4: Introduction to Neural Networks
## Backpropagation QA in class
### [1.Why we use these kind of method (chain rule) to calculate the gradient?](https://youtu.be/d14TUNcbn1k?list=PL3FW7Lu3i5JvHM8ljYj-zLfQRF3EO8sYv&t=727)
Once we use chain rule to break down into these computations, we can simply get the value of the gradient <font color=Red>without having to ever derive the entire expression.</font>
我們可以集中精神在局部性的運算,即使整體運算非常複雜,各個步驟做的事情就是將目標節點的『局部性計算』,藉此我們可以有效際算微分值。
### [2.Whether this only works because the current value of the function?](https://youtu.be/d14TUNcbn1k?list=PL3FW7Lu3i5JvHM8ljYj-zLfQRF3EO8sYv&t=968)
It works given the current value if the function that we plug in, we can write an expression for this still in terms of the variables

### 3.[Is the reason why w~0~ and x~0~ are not connected with w~2~? Base on all of these additions just connected together.](https://youtu.be/d14TUNcbn1k?list=PL3FW7Lu3i5JvHM8ljYj-zLfQRF3EO8sYv&t=1702)
Because the teacher,Fei-Fei ,wanted to write this out into as simple as possible where each node only had up to two inputs

### 4.[What is max gate?](https://youtu.be/d14TUNcbn1k?list=PL3FW7Lu3i5JvHM8ljYj-zLfQRF3EO8sYv&t=1961)
One of these is going to get the full value of the gradient just passed back and the other one will have a gradient of zero. We can call it as a gradient router.

### 5.[What is mul gate?](https://youtu.be/d14TUNcbn1k?list=PL3FW7Lu3i5JvHM8ljYj-zLfQRF3EO8sYv&t=2036)
The answer is that the local gradient is the times of the others inputs.
We call it as a gradient switcher

### 6.[Why we hadn't add anything to actually update the weights?](https://youtu.be/d14TUNcbn1k?list=PL3FW7Lu3i5JvHM8ljYj-zLfQRF3EO8sYv&t=2130)
- We're talking so far is to compute gradients with respect to any variables in our function.
- Once we know these we can apply everything we learned in optimization lecture.
- Take this entire framework that you learned about last lecture for optimization, and we can learn how to compute the gradients we need for arbitarily complex functions.
### 7.[Write down all the variables so it could help us understand more about it. ](https://youtu.be/d14TUNcbn1k?list=PL3FW7Lu3i5JvHM8ljYj-zLfQRF3EO8sYv&t=2194)
:::info
$\dfrac{\partial f}{\partial X}$ = $\sum_{i} (\dfrac{\partial f}{\partial q~i~})(\dfrac{\partial q~i~}{\partial X})$
:::
- If x is connected to these multiple elements, in these case, partical q~i~, then the chain rule is taking the effect of all the intermediate variables on our final output f.
- And then we compound each one with local effect of our variable x on the intermediate values.
- Sum all these up together.
### [8.What is Jacobian matrix?](https://youtu.be/d14TUNcbn1k?list=PL3FW7Lu3i5JvHM8ljYj-zLfQRF3EO8sYv&t=2278)
- [Jacobian prerequisite knowledge](https://www.youtube.com/watch?v=VmfTXVG9S0U)
$$
\left[
\begin{matrix}
2 & -3 \\
1 & 1 \\
\end{matrix}
\right]
\left[
\begin{matrix}
x \\
y \\
\end{matrix}
\right]=
\left[
\begin{matrix}
2x-3y \\
x+y \\
\end{matrix}
\right]
$$
- 那你清楚幾何意義是什麼嗎?我們來看看以下的例子 (Linear Transformation)
 -> 
原本基底是 (1,0) 和 (0,1),透過linear transformation,基底變成(2,1)和(-3,1)
- [The Jacobian matrix](https://www.youtube.com/watch?v=bohL918kXQk)
- 藉著我們假設有一個比較複雜的轉換,轉換的函數如下:
$$
f(\left[
\begin{matrix}
x \\
y \\
\end{matrix}
\right])= \left[
\begin{matrix}
x+sin(y) \\
y+sin(x) \\
\end{matrix}
\right] \tag{1}
$$
 -> 
- 先將(1)式改寫成如下
$$
(\left[
\begin{matrix}
f~1~(x,y) \\
f~2~(x,y) \\
\end{matrix}
\right])= \left[
\begin{matrix}
x+sin(y) \\
y+sin(x) \\
\end{matrix}
\right] \tag{1}
$$
- 我們要表示在新的座標方式如下,J為Jacobian matrix
$$
J= \left[
\begin{matrix}
\dfrac{\partial f~1~}{\partial x} & \dfrac{\partial f~1~}{\partial y}\\
\dfrac{\partial f~2~}{\partial x} & \dfrac{\partial f~2~}{\partial y} \\
\end{matrix}
\right]
$$
- Jacobian matrix 定義
假設f: R^n^→R^m^為一個從歐式n維空間轉換到歐式m維空間的函數,並且由m個實函數組成: y~1~(x~1~,…,x~n~), …, y~m~(x~1~,…,x~n~)。
若將該函數的偏導數(若存在)組成一個m行n列的矩陣, 那麼這個矩陣就是所謂的雅可比矩陣

- 補充:[Computing a Jacobian matrix](https://www.youtube.com/watch?v=CGbBbH1e7Yw&list=PLSQl0a2vh4HC5feHa6Rc5c0wbRTx56nF7&index=72)
- 假設想要知道(-2,1)在這個新的座標系的座標點,將值代入
$$
J(-2,1)= \left[
\begin{matrix}
\dfrac{\partial f~1~}{\partial x} & \dfrac{\partial f~1~}{\partial y}\\
\dfrac{\partial f~2~}{\partial x} & \dfrac{\partial f~2~}{\partial y} \\
\end{matrix}
\right]=
\left[
\begin{matrix}
1 & cos(y)\\
cos(x) & 1 \\
\end{matrix}
\right]=
\left[
\begin{matrix}
1 & 0.54\\
-0.42 & 1 \\
\end{matrix}
\right]
$$

![Uploading file..._i356n5x3w]()
![Uploading file..._opvxednj6]()
- 補充:[The Jacobian Determinant](https://www.youtube.com/watch?v=p46QWyHQE6M)
### [9.Jacobian calculation example](https://youtu.be/d14TUNcbn1k?list=PL3FW7Lu3i5JvHM8ljYj-zLfQRF3EO8sYv&t=2814)
這邊應該寫錯了?!
軒彤:沒錯RRRRRRRR
信賢:OK惹 眼殘~
$\nabla ~w~ f$ = 2q $\cdot$ x^T^
$$
q=\left[
\begin{matrix}
0.22 \\
0.26 \\
\end{matrix}
\right] \tag{1}
$$
$$
x=\left[
\begin{matrix}
0.2 \\
0.4 \\
\end{matrix}
\right] \tag{2}
$$
$\nabla ~w~ f$ =
$$
\left[
\begin{matrix}
0.088 & 0.176 \\
0.104 & 0.208 \\
\end{matrix}
\right] \tag{3}
$$

![Uploading file..._3kfuy1mqr]()
## Neural Networks


* 很多neuron連接起來就是neural network
* 有很多方法來連接neuron
* 連接的方式其實是自己去設計的
* 課堂上介紹的是fully connect
* 可不可以不要fully connect network,下禮拜的CNN
---

* 每個neuron都有自己的一組weight和一組bias,這個weight和bias是根據training data找出來的
* 怎麼給weight:隨機給初始
* gradient descent計算每個參數對total loss的偏微分
* 把參數減掉learning rate*偏微分的值=新參數

---

* 通常寫成矩陣的方式
---

* 我們會把neuron排成一排稱它為layer
---
如果套用到圖片

* Input 是256維,output是10維
* output是對應到每個數字的機率
參考資料:
https://www.youtube.com/watch?v=Dr-WRlEFefw&list=PLJV_el3uVTsPy9oCRY30oBPNLCo89yu49&index=11
---
## 問題
| 組別 | <center> 問題 </center> |
|:------:| ----------------------- |
| 第一組 | (圖1)<br><br>(圖2)<br>1. loss不是從f開始backforward計算嗎(如圖1),那為什麼圖2的最後會是z?<br>2.綠色參數實際上是forword得到的?再利用backword兩者得出loss?Jocbian matrix size 為何是那樣?<br>3. backpropgation 如何幫模型訓練完成?<br>4. 不同的activation function有什麼不同的效果嗎?
| 第二組 | 1. 2017 lecture p.84,為何 2-layer NN 要取max ?<br>2. 不是很了解 p.60-p.74 的矩正微分推導,可以進一步解釋嗎?<br>3. Jacobian matrix 通常會在哪些情況被使用<br>4. 課程中提到的analytic gradient解法指的是用computation graph找gradient這類的**流程**方法,還是指像sigmoid function這種function,只是如果無法被sigmoid取代的才拆小?<br>5. synapses與dendrites差異?|
| 第三組 | <center>報告組</center> |
| 第四組 | 1. 樹突跟現在的類神經網路差在哪裡?<br>2. 請問學生在問老師左邊的馬跟右邊的馬,他們4在什麼神秘的對話?|
## 回答
### 第一組
#### Q1. loss不是從f開始backforward計算嗎(如圖1),那為什麼圖2的最後會是z?
我們認為圖1&圖2是不同的示意圖
或是有人認為是一樣ㄉ嗎?(求解20分)
#### Q2.綠色參數實際上是forword得到的?再利用backword兩者得出loss?Jocbian matrix size 為何是那樣?
- (1)綠色參數不是得到的參數,它代表input
綠色是input value,紅色是利用backward得到的gradient,更新w,最佳化loss value
- (2)再利用backpropagatoion得到loss

- (3)請看圖XD

m=n=4096
mxn=4096x4096

#### Q3.backpropgation 如何幫模型訓練完成?
- 在神經網路的學習中,我們尋找最佳參數(w和b)時,要找出盡量縮小損失函數值的參數
- 為了找出最小損失函數,必須計算參數的微分(梯度),將微分值當作線索,逐漸更新參數值
- 這時我們就需要快速計算梯度方法,而引入backpropagation。我們只需要計算局部的微分值,再利用chain rule,就可以快速計算微分值
- Backpropagation 就是 gradient descent,它只是一個比較有效率的演算法來計算gradient
#### Q4.不同的activation function有什麼不同的效果嗎?
- 為什麼要用激活函數?
- 如果我們不運用激活函數的話,則輸出信號將僅僅是一個簡單的線性函數,線性方程是很容易解決的,但是它們的複雜性有限
- 如果不用激勵函數,每一層輸出都是上層輸入的線性函數,無論神經網絡有多少層,輸出都是輸入的線性組合
- 簡單線性會比較無法學習和模擬其他複雜類型的數據,例如圖像、視頻、音頻、語音等
- 所以需要非線性函數來學習複雜類型的數據,像是激活函數

- 激活函數的種類

- 激活函數的比較(舉常用的兩種)
- sigmoid
- 優點:Sigmoid 函數的取值範圍在 (0,1) 之間,單調連續,求導容易,一般用於二分類神經網絡的輸出層。
- 缺點:上圖中紅色橢圓標註的飽和區曲線平緩,梯度的值很小,近似為零。而且 Sigmoid 函數的飽和區範圍很廣,例如除了 [-5,5],其餘區域都近似飽和區。這種情況很容易造成梯度消失,梯度消失會增大神經網絡訓練難度,影響神經網絡模型的性能。

- RELU
- 優點:
- 沒有飽和區,不存在梯度消失問題。
- 沒有複雜的指數運算,計算簡單、效率提高。
- 實際收斂速度較快,大約是 Sigmoid/tanh 的 6 倍。
- 缺點:
- 當 x<0 時,ReLU 輸出總為零。該神經元輸出為零,則反向傳播時,權重、參數的梯度橫為零,造成權重、參數永遠不會更新,即造成神經元失效,形成了「死神經元」。所以,針對這一問題,有時候會將 ReLU 神經元初始化為正偏值,例如 0.01

原文網址:https://kknews.cc/code/ygkq3aa.html
---
### 第二組
#### Q1.2017 lecture p.84,為何 2-layer NN 要取max ?
before 是線性函數
now 是神經網路
max(0,w)就是relu函數,激活函數可以增加網路的複雜度
#### Q2.不是很了解 p.60-p.74 的矩正微分推導,可以進一步解釋嗎?
詳見 Backpropagation QA in class Q9
#### Q3.Jacobian matrix 通常會在哪些情況被使用
- 做變數轉換的情況會使用
- 積分也會用到
#### Q4.課程中提到的analytic gradient解法指的是用computation graph找gradient這類的流程方法,還是指像sigmoid function這種function,只是如果無法被sigmoid取代的才拆小?
梯度計算
計算梯度有兩種方法一種是數值方法numerical gradient,它計算比較慢,精度低但是比較容易;另一種是解析方法analytic gradient,它快速準確但是容易犯錯。
- 數值法

- 解析法
影片教的~
we calculate all activations and last we backpropagate and calculate the gradient of loss w.r.t. our weights using the chain rule. The Gradient calculated by this method is called the analytic gradient.
#### Q5.synapses與dendrites差異?
- 軸突傳遞訊息
- 樹突接受訊息
- 突觸是輸入與輸出的神經元傳遞的機制

---
### 第四組
#### Q1.樹突跟現在的類神經網路差在哪裡?

#### Q2.請問學生在問老師左邊的馬跟右邊的馬,他們4在什麼神秘的對話?

學生:
W1裡如果包含左臉的馬和右臉的馬的template
一張類似左臉的馬圖片進來,那左臉的馬的分數會比較高,右臉的馬的分數會比較低,然後取比較大的值嗎?
老師:
在左臉的馬的分數會比較高,右臉的馬的分數會比較低,是加權,不是取最大值。
如果你有各種馬的template,那最後將會獲得很高的分數

什麼是template??????


---