Taylor, Jacobian, Hessian, Newton and all the else about gradient

本文的主要目的是對基於gradient的一些approximation知識點以及優化方法做一個簡單的review。詳細內容參考引用鏈接,這裏只列出key points,主要是在遺忘的時候能夠快速catch up…

Jacobian矩陣和Hessian矩陣

引用:

  1. Jacobian矩陣和Hessian矩陣
  2. 雅克比與海森
  • 雅可比矩陣(描述f:R^n\rightarrow R^m的一階導數矩陣). J_{ij}=\partial f_i/\partial x_j. 把它理解爲一階gradient就好了。例如在Automatic Differenciation中,利用Chain Rule就可以將求導過程作爲一系列Jacobian矩陣的乘積。
  • 海森矩陣(描述f:R^n\rightarrow R的二階導數矩陣). H_{ij}=\frac{\partial^2 f}{\partial x_i\partial x_j}. 另y = f(x_1,x_2,...), 則此函數的雅克比矩陣爲[\partial f/\partial x_1, \partial f/\partial x_2,...].求此雅克比矩陣(轉置後作爲向量函數)的雅克比矩陣,即得到海森矩陣。

Automatic Differenciation

引用:

  1. Auto Differenciation(Wikipedia)
  2. 關於AutoDiff的一些信息和Tools 例如基於python的ad
  3. 關於Dual Numbers
  • Symbolic differentiation can lead to inefficient code and faces the difficulty of converting a computer program into a single expression. 符號求導是先推導出\partial y/\partial x的符號公式,再代入x求解,比較複雜。
  • Numerical differentiation can introduce round-off errors in the discretization process and cancellation. 數值積分是在目標點鄰域兩端取點,並通過\frac{\Delta y}{\Delta x}的方式對gradient進行數值近似。容易遇到數值問題。
  • Auto Differenciation exploits the fact that every computer program, no matter how complicated, executes a sequence of elementary arithmetic operations (addition, subtraction, multiplication, division, etc.) and elementary functions (exp, log, sin, cos, etc.). By applying the chain rule repeatedly to these operations, derivatives of arbitrary order can be computed automatically, accurately to working precision, and using at most a small constant factor more arithmetic operations than the original program. 自動求導主要是對Chain Rule的應用。
  • 考慮f:R^n\rightarrow R^m我們最終想要的是一個m*n的雅克比矩陣。
  • Forward accumulation: \frac{\partial y}{\partial x_i} =\frac{\partial y}{\partial w_0}\frac{\partial w_0}{\partial x_i}=\frac{\partial y}{\partial w_1}(\frac{\partial w_1}{\partial w_0}\frac{\partial w_0}{\partial x_i})=...也就是沿着f箭頭方向,從x向y展開chain. 注意括號的位置,對括號都對應了在computational graph中的一個節點。每次計算都是針對某個xi進行的,所以要得到最終的雅克比矩陣需要進行n次計算。當n<<m時,使用Forward方式理論上計算次數更少
  • Reverse accumulation:與forward相反\frac{\partial y}{\partial x_i} =\frac{\partial y}{\partial w_5}\frac{\partial w_5}{\partial x_i}=(\frac{\partial y}{\partial w_5}\frac{\partial w_5}{\partial w_4})\frac{\partial w_4}{\partial x_i}...逆着f箭頭方向,從y向x展開chain。注意括號的位置,對括號都對應了在computational graph中的一個節點。每次計算都是針對某個yi進行的,所以要得到最終的雅克比矩陣需要進行m次計算。當n>>m時,使用Reverse方式理論上計算次數更少.但是要注意,由於是top-down遞歸求解,中間過程需要存儲,導致memory使用會比較大
  • Forward/Reverse 只是兩中極端方式,如何使用最少的步驟求得雅克比矩陣,是一個np難問題。
  • Dual Numbers: 這是一種計算Ad的方式。把標量全部用類似complex number的形式表現出來:x\rightarrow(a+b\epsilon )其中的\epsilon理解爲無限趨近於零的無窮小(infinidestimal),並且\epsilon ^2=0. 現在f(x)=f(x+\epsilon )=f(x)+f'(x)\epsilon也就是說,將x用dual number表示後,只要按往常一樣計算y,那麼最終y的dual number表示中的第二個component就是gradient了。這裏有點意思,舉個栗子吧:f(x)=x^2=f(x+\epsilon )^2=x^2+2x\epsilon +\epsilon ^2=x^2+(2x)\epsilon所以2x就是f'(x)了!這裏我覺得其實是完全符合gradient的定義的,即當x改變一丟丟時,y的變化量。可以把此處的\epsilon理解爲dx

Back-propagation

神經網絡中的back-propagation(BP)是對Ad的典型應用,對應了reverse accumulation的一種情況。

Newton and Quasi-Newton

引用:

  1. Taylor Series 全網沒有找到一個比這個更好的解釋。值得番羽牆去看,利劍
  2. Wiki 這裏面介紹了一些坑

Taylor Series

可以理解成對連續可導函數f(x)在某一點的多項式近似g(x)。這個推導思路其實是,如果想保證這個近似在x_0是比較準確的,那麼應該滿足:f(x_0)=g(x_0),不過如果兩個函數在這一點的變化趨勢(gradient)也一樣就更好了:f'(x_0)=g'(x_0), 如果變化趨勢的變化趨勢也一樣的就更更好了:f''(x_0)=g''(x_0)...

在假定g(x)=a+bx+cx^2...的前提下,根據上面的等式我們可以求出abc,也就得到了taylor近似。這個結果有着強大的用處

牛頓法尋根

在一個起始點,對f(x)進行泰勒一階展開g(x)可以輕易求出x0使g(x0)=0. 也就是一個近似根。這個根可能離f(x)的根比較遠但是比起始點要近一些,那麼只要繼續不斷迭代就好了。

牛頓法求最優值

同樣的配方,只要有了Talor多項式近似就有辦法!對f(x)進行泰勒二階展開,兩邊求導從而找到f'(x)的近似,使其爲0即可得到迭代公式:x_{n+1}=x_n-\frac{f'(x_n)}{f''(x_n)}

這裏需要重點列一些可能出現的問題:

  1. Starting point。可能質量過差導致迭代方向偏離。可以首先使用bisection算法找到一個質量好的starting point再應用牛頓法
  2. Iteration point is stationary。迭代中遇到導數爲0或接近0的點,導致無法求得下一個迭代點或者下一個點偏離過遠
  3. Cycle。兩個iteration point進入循環。
  4. Derivative not exist at root. Considerf(x)=x^{1/3}at 0
  5. Derivative not continuous near the root.
  6. 另外一些情況下導致non-quadratic convergence: root處導數爲0;root的二階導數不存在等

擬牛頓法

當涉及多變量時,牛頓法裏的分母f'(x)需要改爲乘以J^{-1}(x)也就是對應的Jacobian矩陣。而這一矩陣計算量可能過大,此時可以通過一些方法對其進行快速的近似,這也就是Quasi-Newton methods.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章