###### tags: `Machine Learning` # ML(Hung-yi Lee)_Lecture02. What to do if my network fails to train ## local minima and saddle point ![](https://i.imgur.com/sqTc8PN.png) 參數對LOSS的微分=0,gradient無法繼續update參數,**卡在critical point** ![](https://i.imgur.com/CkvKkja.png) ### 區分local minima和saddle point #### LOSS funciton形狀 $L(\theta )$around$\theta=\theta'$,沒辦法完整表達 $L(\theta )$,但可以用 $L(\theta)\approx L(\theta')+(\theta-\theta')^Tg+\frac{1}{2}(\theta-\theta')^TH(\theta-\theta')$ ![](https://i.imgur.com/yokYYc3.png) (gradient $g$ is a vector(一次微分),,hessain $H$ is a matrix(二次微分)) * if 走到critical point,表示gradient=0,$(\theta-\theta')^Tg=0$ * 評估Hessian值的不同 : $\frac{1}{2}(\theta-\theta')^TH(\theta-\theta')$ ![](https://i.imgur.com/EU7mY3M.png)\ * $v^THv>0$ ---> around $\theta':L(\theta)>L(\theta')$ 代表現在是local minima * $v^THv<0$ ---> around $\theta':L(\theta)<L(\theta')$ 代表現在是local maxima * sometimes $v^THv<0$, sometimes $v^THv>0$ ---> **saddle point** * decrease $L$: $u$ is an **eigen vector** of $H$,$\lambda$ is the eigen value of $u$,找出$\lambda<0$的$u$,沿著$u$的方向做update可減少LOSS--->計算量大,較少人實作 #### 會不會維度越高可以走的路越多? 在二維中看起來是local minima但在更高維度其實只是saddle point #### 其實local minima沒有那麼常見,通常gradient無法再更新是因為卡在saddle point ## Batch and Momentum ### Batch #### 每一次拿取一大筆資料$B$來計算$L$ ![](https://i.imgur.com/d7Kn9X1.png) * 所有的batch都看過一遍稱為一個**epoch** * **shuffle**:每一個epoch的batch內容都不相同,每次都會重新分配 #### why use batch? * small batch v.s large batch || large batch | small batch | |---------- | -------- | -------- | |batch size| N(full batch) | 1 | |speed of one update| see all samples後才可計算$g$,看完全部s才可update一次參數,slower | 每次update參數只要看一筆資料,faster| |gradient| stable | noisy| |time of one epoch|slower|faster| |optimization|**better**|worse| ![](https://i.imgur.com/UK0AQ65.png) * small batch的noisy gradient反而可以幫助training,performance較佳 * large batch可能會卡在critical point出不去,但small batch有很多batch可以參考 * small batch is better on **testing data**] * small batch有很多LOSS每一次update的參數都不同,動一下就可以離開峽谷small batch不會被困住,但是large batch容易被困在峽谷 ### Momentum * momentum+gradient descent $m^1=\lambda m^0-\eta g^0$ ($\eta \ :$ **learning rate**,決定我們移動的步伐多大) * 會參考先前走過的方向再加上gradient來做為update的方向 * 可以避免gradient=0時就不再繼續更新位置的問題(因為還有參考momentum) ## Adpative Learning Rate * 當LOSS不再下降時不代表gradient一定變小 ### **客製化learning rate** * 在陡峭的地方gradient大--->learning rate調小 * 在平緩的地方gradient小--->learning rate調大 #### $\theta^{t+1}_{i} \leftarrow \theta^{t}_{i}-\frac{\eta}{\sigma^{t}_{i}} g^{t}_{i}$ ($\sigma^{t}_{i} \ :$ parameter dependent because of i, iteration dependent beacuse of t) * 計算$\sigma^{t}_{i}$ : 1. root mean square $\theta^{2}_{i} \leftarrow \theta^{1}_{i}-\frac{\eta}{\sigma^{1}_{i}} g^{1}_{i} \ \ \ \ \sigma^{1}_{i}=\sqrt{\frac{1}{2}[(g^{0}_{i})^2+(g^{1}_{i})^2]}$ ![](https://i.imgur.com/jUBbNvf.png) ($\theta_1$的坡度較小,計算出的gradient值較小,計算出的$\sigma$小$\rightarrow$learning rate較大) 2. RMSProp $\theta^{2}_{i} \leftarrow \theta^{1}_{i}-\frac{\eta}{\sigma^{1}_{i}} g^{1}_{i} \ \ \ \ \sigma^{1}_{i}=\sqrt{\alpha(\sigma^{0}_{i})^2+(1-\alpha)(g^{1}_{i})^2}$ $\ \ 0<\alpha<1$ ($\alpha \approx 0$代表現在新算出來的gradient重要,反之代表之前算出gradient比較重要) (到坡度較大地方時可以把$\alpha$值設小,讓現在的gradient影響步伐$\rightarrow$learning rate變小smaller step) ![](https://i.imgur.com/b8pwd31.png) 3. **Adam:RMSProp+Momentum** ### Learning Rate Scheduling ![](https://i.imgur.com/fECTH4e.png) (y軸的方向累積了很多很小的$\sigma$,累積到一個地步就會爆走噴出去) '$\theta^{t+1}_{i} \leftarrow \theta^{t}_{i}-\frac{\eta}{\sigma^{t}_{i}} g^{t}_{i}$ #### 更新$\eta$ ![](https://i.imgur.com/kL56OHa.png) 1. learning rate decay : 隨著時間越長,learning rate越來越小(踩煞車) 2. **Warm up** : 先變大後變小,論文顯示一開始就很大並不會產生比較好的解 ## Classification ### Regression input:vector output:scalar ![](https://i.imgur.com/PfzaXak.png) #### one-hot vector : 分類問題裡相鄰的兩個並沒有直接關係(ex.class 1& class 2比較接近) $\hat{y}\longleftrightarrow y'=softmax(y)$ $\ y=b'+w'\alpha(b+Wx)$ ($y$ is a vector, $y'$: mark all values between 0 and 1, $\hat{y}$: label值為0or1) * **softmax function**: 本來y裡可以放任何值改為[0,1] $y'_i=\frac{exp(y_i)}{\sum_{j}exp(y_i)}$ (任何數做exp都會變positive) ($1>y'_i>0, \sum_{i}y'_i=1$) ![](https://i.imgur.com/mz9hapo.png) (1. 讓大的值和小的值差距更大 2. 讓值介於0~1之間) * binary classification: 用**sigmoid**和**softmax**等價 #### sigmoid and softmax 差別 * sigmoid ![](https://i.imgur.com/eZYSt5m.png) * 處理多標籤分類問題,可同時擁有多個正確答案,sigmoid可獨立處理個個原始輸出值 * 能夠將任意值轉換到[0,1] * 公式: $sigmoid(x)=\frac{1}{1+e^(-x)}$ * softmax * 處理多標籤分類問題,只能擁有一個正確答案,每個答案皆**互斥** * 將多分類結果以**概率**方式顯示 ### Loss of Classification * **cross-entropy** : $e=-\displaystyle\sum_{i}\hat{y}_ilny_i'$ * (when $\hat{y}_i=y_i'$,cross entropy最小) * maximize likelihood = minimize cross entropy ![](https://i.imgur.com/18NHvTp.png) | | MSE| cross-entropy | | -------- | -------- | -------- | | $y1$大$y2$小($y'_1 \approx 1,y'_2\approx0$) | Loss small | Loss small | |$y1$小$y2$大($y'_1 \approx 0,y'_2\approx1$)|Loss large|Loss large| ||![](https://i.imgur.com/34fvD3c.png)|![](https://i.imgur.com/LybDmez.png)|\ MSE問題:if init初始在gradient小平坦的地方容易卡住 conclusion : **changing the Loss function can change the difficulty of optimization** ## Batch Normalization if 直接剷平error surface? ![](https://i.imgur.com/qiJZAgP.png) ![](https://i.imgur.com/JJcZjwp.png) (假設兩個參數對Loss斜率的差別很大,在w1斜率變化上很小在w2上很大,用固定learning rate結果不好$\rightarrow$adaptive learning rate) 用另一個角度想: 直接把難做的error surface改掉 * case: * $x_1$數值都很小對$L$的改變小,$x_2$數值都很大對$L$改變大 ### feature normalization ![](https://i.imgur.com/uoagete.png) * 取同一個dimension的值去計算mean:$m_i$,standard deviation:$\sigma_i$ $\tilde{x^{r}_{i}} \leftarrow \frac{x^{r}_{i}-m_i}{\sigma_i}$ * 可讓gradient收斂更快,訓練更順利 * 在activation function前做normalization or之後? * if function is sigmoid,建議對z做normalization,但還是要看funciotn的性質(因為sigmoid在0附近的波動較大) ![](https://i.imgur.com/jublt65.png) * 因為做了normalization每個值都會互相影響,相比之前每一個都是獨立計算做 normalization變成一個龐大network$\rightarrow$dataset多**計算量龐大** ### Batch Normalization #### training 不會同時考慮全部的data,會取一個夠大的batch size足以表達整個dataset來做normalization * 設計: $\tilde{z^i}=\frac{z^i-\mu}{\sigma},\ \hat{z^i}=\gamma \bigodot\tilde{z^i}+\beta$ * why 加上$\beta$$\gamma$ ? 調整output的分布 * init value : $\gamma=1,\beta=0$ 剛開始每一個dimension的分布較平均 #### testing 不可能等到收集到batch size的資料量才開始做運算 * 計算**moving avg** * 每一個batch計算一次 * $\overline{\mu}\leftarrow\rho\overline{\mu}+(1-\rho)\mu^t$ * 直接用計算出來的平均$\overline{\mu}$來取代原本的${\mu}$ ![](https://i.imgur.com/gOhcCTS.png) #### why batch normalization work? * 解決gradient爆炸or gradient消失 * batch normalizarion use in CNN