不喜欢学习数学英语而产生的眼高手低的典范

# Coursera 部分

# definition

  • Field of study that gives computers the ability to learn without being explicitly programmed Arthur Samuel(1959)
  • A computer program is said to learn from experience E with respect to some task T and some performance measure P, if its performance on T, as measured by P, improves with experience E. Tom Mitchell

# Supervised learning(teach the machine to do sth)

  • the “right answers” data sets are given
  • regression (回归) problem(predict continuous value)
  • classification problem(discrete valued output(0 or 1 or 2,…))
  • can predict according to infinite features

# Notations:

  • m=Number of training examples

  • x’s=“input” variable/features

  • y’s=“output” variable/features

  • (x,y)=one training example

  • (x(i),y(i))=theithtrainingexample(x^{(i)},y^{(i)})=the\quad i^{th}\quad training\quad example

  • h=hypothesis, mapping from x’s to y’s

    eg.hθ(x)=θ0+θ1x(univariablelinearregression)eg.h_\theta(x)=\theta_0+\theta_1x\quad(univariable\quad linear\quad regression)

    1.png

# Univariable linear regression

  • Goal:tominimizeθ0,θ1J(θ0,θ1),whereJ(θ0,θ1)=12mi=1m(hθ(x(i))y(i))2(costfunction)Goal:to\quad minimize_{\theta_0,\theta_1}J(\theta_0,\theta_1),\\ where\quad J(\theta_0,\theta_1)=\frac{1}{2m}\sum_{i=1}^m(h_\theta(x^{(i)})-y^{(i)})^2(cost\quad function)\\

  • J is called cost function or squared error cost function

  • squared error cost function is the most used function for linear regression problem and works pretty well

# Gradient descent

  • to minimize function J

  • Outline:

    • Startwithsomeθ0,θ1Start\quad with\quad some\quad \theta_0,\theta_1

    • keepchangingθ0,θ1toreduceJ(θ0,θ1)keep\quad changing\quad \theta_0,\theta_1\quad to\quad reduce\quad J(\theta_0,\theta_1)

    • until we hopefully end up at a minimum

  • repeatuntilconvergence{θ0=θ0αθ0J(θ0,θ1)θ1=θ1αθ1J(θ0,θ1)}α is  called  learning  raterepeat\quad until\quad convergence\{\\ \theta_0'=\theta_0-\alpha\frac{\partial}{\partial\theta_0}J(\theta_0,\theta_1)\\ \theta_1'=\theta_1-\alpha\frac{\partial}{\partial\theta_1}J(\theta_0,\theta_1)\\ \}\\ \alpha \ is\; called\;learning\; rate

  • Noticing that theta0 and theta1 are simultaneously updated

  • θ0=1mi=1m(hθ(x(i))y(i))θ1=1mi=1m(hθ(x(i))y(i))x(i)\frac{\partial}{\partial\theta_0}=\frac{1}{m}\sum_{i=1}^m(h_\theta(x^{(i)})-y^{(i)})\\ \frac{\partial}{\partial\theta_1}=\frac{1}{m}\sum_{i=1}^{m}(h_\theta(x^{(i)})-y^{(i)})\cdot x^{(i)}

  • univariable linear regression is a convex function(a bowl shape function ), so it has no local optima other than the global optimum

  • "Batch" Gradient Descent : Each step of gradient descent uses all the training examples.(from 1 to m)

# Multivariate regression problem

  • n=number of features(variables)

  • xj(i)=theithtrainingexamplethejthfeaturex^{(i)}_j=the\quad i^{th}\quad training\quad example\quad the\quad j^{th}\quad feature

  • hθ(x)=i=0nθixi(definingx0=1)X(i)=[x0(i)x1(i)...xn(i)]Rn+1θ=[θ0θ1...θn]Rn+1hθ(x)=θTX(i)h_\theta(x)=\sum_{i=0}^n\theta_ix_i\quad (defining\quad x_0=1)\\ X^{(i)}=\begin{bmatrix} x_0^{(i)}\\ x_1^{(i)}\\ ...\\ x_n^{(i)}\\ \end{bmatrix} \in\mathbb{R}^{n+1} \quad \theta=\begin{bmatrix} \theta_0\\ \theta_1\\ ...\\ \theta_n \end{bmatrix} \in\mathbb{R}^{n+1}\\ h_\theta(x)=\theta^TX^{(i)}\\

  • J(θ)=J(θ0,θ1,...,θn)=12mi=1m(hθ(x(i))y(i))2J(\theta)=J(\theta_0,\theta_1,...,\theta_n)=\frac{1}{2m}\sum_{i=1}^m(h_\theta(x^{(i)})-y^{(i)})^2

  • Gradient descent:

    θj=θjα1mi=1m(hθ(xj(i))y(i))xj(i)θ=θαmXT(XθY)(wirte  in  matrix)\theta_j'=\theta_j-\alpha\frac{1}{m}\sum_{i=1}^m(h_\theta(x_j^{(i)})-y^{(i)})\cdot x^{(i)}_j\\ \theta'=\theta-\frac{\alpha}{m}*X^T(X\theta-Y)\quad(wirte\;in\;matrix)

# Practical tricks for making gradient descent work well

  • Feature Scaling(faster)

    • Idea: Make sure features are on a similar scale
    • *Get every feature into **approximately(not necessarily exact)*a [-1, 1] range

    E.g.

x1=sizes(02000feet2),x2=numberofbedrooms(15)x1=size2000,x2=#bedrooms5x1=size10002000,x2=#bedrooms25so  that  x1  and  x2  are  both  in  [0.5,0.5]x_1=sizes(0-2000feet^2),x2=number\,of\,bedrooms(1-5)\\ x_1'=\frac{size}{2000},x_2'=\frac{\#bedrooms}{5}\\ x_1''=\frac{size-1000}{2000},x_2''=\frac{\#bedrooms-2}{5}\\ so\;that\;x_1\;and\;x_2\;are\;both\;in\;[-0.5, 0.5]\\

let  xi=xixiˉmax{xi}min{xi}where  xiˉ=1mj=1mxi(j)let\;x_i'=\frac{x_i-\bar{x_i}}{max\{x_i\}-min\{x_i\}}\quad where\;\bar{x_i}=\frac{1}{m}\sum_{j=1}^mx^{(j)}_i

  • Choose learning rate properly

    • Declare convergence if your cost function decreases by less than 1e-3(?) in one iteration
    • If your cost function doesn’t decrease on every iteration, use smaller learning rate
  • Try to choose …, 0.001, 0.01, 0.1, 1,…

  • Choose appropriate features

  • Think about what features really determine the result in particular problem

  • Polynomial regression

    • tryhθ(x)=θ0+θ1(size)+θ2sizewhere  size=lengthwidthbetterthanθ0+θ1length+θ2widthtry\quad h_\theta(x)=\theta_0+\theta_1(size)+\theta_2\sqrt{size}\quad where\;size=length*width\\ better\quad than\quad \theta_0+\theta_1*length+\theta_2*width\\

    • pay attention to features’ scale

      hθ(x)=θ0+θ1size1000+θ2size32ifsizerangesin[1,1000],100032h_\theta(x)=\theta_0+\theta_1\frac{size}{1000}+\theta_2\frac{\sqrt{size}}{32}\\ if\quad size\quad ranges\quad in\quad [1,1000],\sqrt{1000}≈32

# Normal equation

  • X=[x0(1)=1x1(1)...xn(1)x0(2)=1x1(2)...xn(2)............x0(m)=1x1(m)...xn(m)]y=[y(1)y(2)...y(m)]J(θ)=12mi=1m(j=0nxj(i)θjy(i))2=12m(Xθy)T(Xθy)=12m(θTXTyT)(Xθy)=12m(θTXTXθθTXTyyTXθ+yTy)dJ(θ)dθ=12m(dθTdθXTXθ+(θTXTX)TdθdθdθTdθXTy(yTX)Tdθdθ)=12m(IXTXθ+XTXθTIXTyXTy)=12m((XTX+XTX)θ(XTy+XTy))so  if  dJ(θ)dθ=0XTXθXTy=0soθ=(XTX)1XTyin  fact  AX  is  also  a  vectord[y1y2...ym]d[x1x2...xn]=d[y1y2...ym]d[x1x2...xn]=[y1x1y2x1...ymx1y1x2y2x2...ymx2............y1xny2xn...ymxn]so  we  have  dXTdX=dXdX=I,dAXdX=ATdABdX=dAdXB+AdBdXwhile  X  is  a  vector  and  A  is  1×n  and  B  is  n×1X=\begin{bmatrix} x_0^{(1)}=1&x_1^{(1)}&...&x_n^{(1)}\\ x_0^{(2)}=1&x_1^{(2)}&...&x_n^{(2)}\\ ...&...&...&...\\ x_0^{(m)}=1&x_1^{(m)}&...&x_n^{(m)} \end{bmatrix}y=\begin{bmatrix} y^{(1)}\\ y^{(2)}\\ ...\\ y^{(m)} \end{bmatrix}\\ J(\theta)=\frac{1}{2m}\sum_{i=1}^m(\sum_{j=0}^nx_j^{(i)}\theta_j-y^{(i)})^2\\ =\frac{1}{2m}(X\theta-y)^T(X\theta-y)\\ =\frac{1}{2m}(\theta^TX^T-y^T)(X\theta-y)\\ =\frac{1}{2m}(\theta^TX^TX\theta-\theta^TX^Ty-y^TX\theta+y^Ty)\\ \frac{dJ(\theta)}{d\theta}=\frac{1}{2m}(\frac{d\theta^T}{d\theta}X^TX\theta+(\theta^TX^TX)^T\frac{d\theta}{d\theta}-\frac{d\theta^T}{d\theta}X^Ty-(y^TX)^T\frac{d\theta}{d\theta})\\ =\frac{1}{2m}(IX^TX\theta+X^TX\theta^TI-X^Ty-X^Ty)\\ =\frac{1}{2m}((X^TX+X^TX)\theta-(X^Ty+X^Ty))\\ so\;if\;\frac{dJ(\theta)}{d\theta}=0\\ X^TX\theta-X^Ty=0\\ so\quad \theta=(X^TX)^{-1}X^Ty\\**************************\\ in\;fact\;AX\;is\;also\;a\;vector\\ \frac{d\begin{bmatrix}y_1&y_2&...&y_m\end{bmatrix}}{d\begin{bmatrix}x_1\\x_2\\...\\x_n\end{bmatrix}}=\frac{d\begin{bmatrix}y_1\\y_2\\...\\y_m\end{bmatrix}}{d\begin{bmatrix}x_1\\x_2\\...\\x_n\end{bmatrix}}= \begin{bmatrix} \frac{\partial y_1}{\partial x_1}&\frac{\partial y_2}{\partial x_1}&...&\frac{\partial y_m}{\partial x_1}\\ \frac{\partial y_1}{\partial x_2}&\frac{\partial y_2}{\partial x_2}&...&\frac{\partial y_m}{\partial x_2}\\ ...&...&...&...\\ \frac{\partial y_1}{\partial x_n}&\frac{\partial y_2}{\partial x_n}&...&\frac{\partial y_m}{\partial x_n} \end{bmatrix}\\ so\;we\;have\;\frac{dX^T}{dX}=\frac{dX}{dX}=I,\frac{dAX}{dX}=A^T\\\frac{dAB}{dX}=\frac{dA}{dX}B+A\frac{dB}{dX}\quad while\;X\;is\;a\;vector\;and\;A\;is\;1\times n\;and\;B\;is\;n\times 1\\

  • When  (XTX)  is  noninvertible?Redundant  features(linearly  dependent)E.g.  x1=size  in  feet2,x2=size  in  m2,so  x13.282x2Too  many  features(E.g.  mn)When\;(X^TX)\;is\;non-invertible?\\ *Redundant\;features(linearly\;dependent)\\ E.g.\;x_1=size\;in\;feet^2,x_2=size\;in\;m^2,so\;x_1\equiv3.28^2x_2\\ *Too\;many\;features(E.g.\;m\leq n)

# Logistic Regression Classification

# binary class classification

  • y{0,1}y\in\{0,1\}

    0 refers to the absence of something while 1 refers to the presence of something

# logistic regression model
  • want  0hθ(x)1so  let  hθ(x(i))=g(θTX(i)),where  g(z)=11+ezhθ(x)=estimated  probability  that  y=1  on  input  xhθ(x)=P(y=1x;θ)P(y=1x;θ)>0.50hθ(x(i))want \; 0\leq h_\theta(x)\leq 1\\ so \; let\; h_\theta(x^{(i)})=g(\theta^TX^{(i)}),where\;g(z)=\frac{1}{1+e^{-z}}\\ h_\theta(x)=estimated\;probability\; that\; y=1\; on\; input\; x\\ h_\theta(x)=P(y=1|x;\theta)\\ P(y=1|x;\theta)>0.5\Leftrightarrow 0\leq h_\theta(x^{(i)})

  • Non-linear decision boundaries

    hθ(x)=g(θ0+θ1x1+θ2x2+θ3x12+θ4x22)h_\theta(x)=g(\theta_0+\theta_1x_1+\theta_2x_2+\theta_3x_1^2+\theta_4x_2^2)

    • the parameter theta determines the decision boundary
  • Cost function

    J(θ)=1mi=1mCost(hθ(x(i)),y(i))If  we  choose  Cost(hθ(x(i)),y(i))=12(hθ(x(i))y(i))2  like  linear  regression  problemthen  J(θ)  would  be  a  nonconvex  functionJ(\theta)=\frac{1}{m}\sum_{i=1}^mCost(h_\theta(x^{(i)}),y^{(i)})\\ If\;we\;choose\;Cost(h_\theta(x^{(i)}),y^{(i)})=\frac{1}{2}(h_\theta(x^{(i)})-y^{(i)})^2\;like\;linear\;regression\;problem\\ then\;J(\theta)\;would\;be\;a\;non-convex\;function

    Cost(hθ(x(i),y(i)))={log(hθ(x(i)))if  y=1log(1hθ(x(i)))if  y=0=y(i)log(hθ(x(i)))(1y(i))log(1hθ(x(i)))Cost(h_\theta(x^{(i)},y^{(i)}))=\begin{cases}-log(h_\theta(x^{(i)}))\quad if\;y=1\\-log(1-h_\theta(x^{(i)}))\quad if\;y=0\end{cases}\\ =-y^{(i)}log(h_\theta(x^{(i)}))-(1-y^{(i)})log(1-h_\theta(x^{(i)}))

    • The cost function derived from Maximum Likelihood Estimation and has a nice property that it’s convex

    J(θ)=1mi=1m[y(i)log(hθ(x(i)))+(1y(i))log(1hθ(x(i)))]minθJ(θ)Repeat{θj=θjαθjJ(θ)=θjαmi=1m(hθ(x(i))y(i))xj(i)}J(\theta)=-\frac{1}{m}\sum_{i=1}^m[y^{(i)}log(h_\theta(x^{(i)}))+(1-y^{(i)})log(1-h_\theta(x^{(i)}))]\\ min_\theta J(\theta)\\ Repeat\{\\ \theta_j'=\theta_j-\alpha\frac{\partial}{\partial\theta_j}J(\theta)\\ =\theta_j-\frac{\alpha}m\sum_{i=1}^m(h_\theta(x^{(i)})-y^{(i)})x_j^{(i)}\\ \}\\

  • A  simple  proof(provided  by  SuBonan):sigmond(x)=sigmond(x)[1sigmond(x)]hθ(x(i))=sigmond(j=0nxj(i)θj)sohθ(x(i))θj=sigmond(j=0nxj(i)θj)[1sigmond(j=0nxj(i)θj)]xj(i)=hθ(x(i))[1hθ(x(i))]xj(i)whileCost(hθ(x(i)),y(i))=yln(hθ(x(i)))(1y)ln(1hθ(x(i)))soCost(hθ(x(i)),y)θj=yhθ(x(i))hθ(x(i))θj+1y1hθ(x(i))hθ(x(i))θj=[hθ(x(i))y]xj(i)J(θ)=1mi=1mCost(hθ(x(i)),y(i))soJ(θ)θj=1mi=1mθjCost(hθ(x(i)),y(i))=1mi=1m[hθ(x(i))y(i)]xj(i)A\;simple\;proof(provided\;by\;SuBonan):\\ sigmond'(x)=sigmond(x)[1-sigmond(x)]\\ h_\theta(x^{(i)})=sigmond(\sum_{j=0}^nx^{(i)}_j\theta_j)\\ so\quad \frac{\partial h_\theta(x^{(i)})}{\partial\theta_j}=sigmond(\sum_{j=0}^nx_j^{(i)}\theta_j)[1-sigmond(\sum_{j=0}^nx_j^{(i)}\theta_j)]x_j^{(i)}\\ =h_\theta(x^{(i)})[1-h_\theta(x^{(i)})]x_j^{(i)}\\ while\quad Cost(h_\theta(x^{(i)}),y^{(i)})=-yln(h_\theta(x^{(i)}))-(1-y)ln(1-h_\theta(x^{(i)}))\\ so\quad \frac{\partial Cost(h_\theta(x^{(i)}),y)}{\partial \theta_j}=\frac{-y}{h_\theta(x^{(i)})}\cdot \frac{\partial h_\theta(x^{(i)})}{\partial\theta_j}+\frac{1-y}{1-h_\theta(x^{(i)})}\cdot\frac{\partial h_\theta(x^{(i)})}{\partial\theta_j}\\ =[h_\theta(x^{(i)})-y]x_j^{(i)}\\ J(\theta)=\frac{1}{m}\sum_{i=1}^mCost(h_\theta(x^{(i)}),y^{(i)})\\ so\quad \frac{\partial J(\theta)}{\partial \theta_j}=\frac{1}{m}\sum_{i=1}^m\frac{\partial}{\partial\theta_j}Cost(h_\theta(x^{(i)}),y^{(i)})\\ =\frac{1}{m}\sum_{i=1}^m[h_\theta(x^{(i)})-y^{(i)}]x_j^{(i)}

  • So the vectorized implementation is:

    θ=θαmXT(sigmond(Xθ)Y)\theta'=\theta-\frac{\alpha}{m}X^T(sigmond(X\theta)-Y)

  • Advanced Optimization

    • Conjugate gradient
    • BFGS
    • L-BFGS
    • All algorithms above has following advantages:
      • No need to manually pick learning rate
      • Often faster than gradient descent

# Multiclass classfication

y{0,1,2,3,...}y\in\{0,1,2,3,...\}

# One-vs-all(one-vs-rest)
  • hθ(i)(X)=P(y=iX;θ)so  given  X  find  maxihθ(i)(X)h_\theta^{(i)}(X)=P(y=i|X;\theta)\\ so\;given\;X\;find\;max_ih_\theta^{(i)}(X)

# The problem of overfitting

2

  • Overfitting: If we have too many features, the learned hypothesis may fit the training set very well, but fail to generalize to new examples(predict prices on new examples).

  • Adressing overfitting:

    • Reduce features:

      • Manually select which features to keep.
      • Model selection algorithm
    • Regularization:

      • Keep all the features, but reduce magnitude/values of parameters theta.
      • Works well when we have a lot of features, each of which contributes a bit to predicting y.

# Cost Function

  • hθ(x)=θ0+θ1x+θ2x2+θ3x3Cost  function=1mi=1m[hθ(x)y]2+1000θ32h_\theta(x)=\theta_0+\theta_1x+\theta_2x^2+\theta_3x^3\\ Cost\;function=\frac{1}{m}\sum_{i=1}^m[h_\theta(x)-y]^2+1000\theta_3^2

    So that we can avoid theta3 being too large by minimizing cost function

  • But we don’t know which parameter theta to shrink in advance, so we choose to shrink all of them:

    J(θ)=12m[i=1m[hθ(x(i))y(i)]2+λj=1nθj2]λ=regularization  parameterJ(\theta)=\frac{1}{2m}[\sum_{i=1}^m[h_\theta(x^{(i)})-y^{(i)}]^2+\lambda\sum_{j=1}^n\theta_j^2]\\ \lambda=regularization\;parameter

    In practice, whether or not you choose to shrink theta0 makes very little difference to the results

# Regularized Linear Regression

  • Gradient descent:

θ=θαm[XT(XθY)+λθ]\theta'=\theta-\frac{\alpha}{m}[X^T(X\theta-Y)+\lambda\theta]

  • Normal equation:

    θ=(XTX+λIn+1)1XTyand  XTX+λIn+1  will  be  always  invertible\theta=(X^TX+\lambda I_{n+1})^{-1}X^Ty\\ and\;X^TX+\lambda I_{n+1}\;will\;be\;always\;invertible

# Regularized Logistic Regression

  • Gradient descent:

    θ=θαmXT(sigmond(Xθ)Y)+λαmθ\theta'=\theta-\frac{\alpha}{m}X^T(sigmond(X\theta)-Y)+\frac{\lambda\alpha}{m}\theta

# Neural Network

# Notations

ai(j)="activation"  of  unit  i  in  layer  jΘ(j)=matrix  of  weights  controlling  function  mapping  from  layer  jto  layer  j+1the  dimension  of  Θ(j)  is  sj+1×(sj+1)sj  is  the  number  of  units  in  layer  ja_i^{(j)}="activation"\;of\;unit\;i\;in\;layer\;j\\ \varTheta^{(j)}=matrix\;of\;weights\;controlling\;function\;mapping\;from\;layer\;j\\to\;layer\;j+1\\ the\;dimension\;of\;\varTheta^{(j)}\;is\;s_{j+1}\times(s_j+1)\\ s_j\;is\;the\;number\;of\;units\;in\;layer\;j

# Vectorized implementation

a(2)=sigmoid(Θ(1)X)Add  a0(2)=1a(3)=sigmoid(Θ(2)a(2))...output  layer:[P{output=1}P{output=2}...P{output=numOfLabels}]=sigmoid(Θ(n)a(n))but  P  is  just  prediction  not  the  answer,  soi=1numOfLabelsP{output=i}  does  not  necessarily  equal  to  1a^{(2)}=sigmoid(\varTheta^{(1)}X)\\ Add\;a_0^{(2)}=1\\ a^{(3)}=sigmoid(\varTheta^{(2)}a^{(2)})\\ ...\\ output\;layer:\\ \begin{bmatrix} P\{output=1\}\\ P\{output=2\}\\ ...\\ P\{output=numOfLabels\} \end{bmatrix}=sigmoid(\varTheta^{(n)}a^{(n)})\\ but\;P\;is\;just\;prediction\;not\;the\;answer,\;so\\ \sum_{i=1}^{numOfLabels}P\{output=i\}\;does\;not\;necessarily\;equal\;to\;1

# Cost Function

K=numOfLabelsL=numOfLayerslethΘ(x)RKrepresent  the  outputand  (hΘ(x))k  represent  the  ith  output  in  output  layerJ(Θ)=1m[i=1mk=1Kyk(i)log(hΘ(x(i))k)+(1yk(i))log(1hΘ(x(i))k)]+λ2ml=1L1i=2sl+1j=1sl+1(Θji(l))2K=numOfLabels\quad L=numOfLayers\\ let\quad h_\varTheta(x)\in\mathbb{R}^{K}\quad represent\;the\;output\\ and\;(h_\varTheta(x))_k\;represent\;the\;i^{th}\;output\;in\;output\;layer\\ J(\varTheta)=-\frac{1}{m}[\sum_{i=1}^m\sum_{k=1}^Ky_k^{(i)}log(h_\varTheta(x^{(i)})_k)+(1-y_k^{(i)})log(1-h_\varTheta(x^{(i)})_k)]\\+\frac{\lambda}{2m}\sum_{l=1}^{L-1}\sum_{i=2}^{s_{l+1}}\sum_{j=1}^{s_l+1}(\varTheta_{ji}^{(l)})^2

# Backpropagation Algorithm

  • Gradient computation

    Supposing we have only one training example (m=1)

so  x,y  substitute  for  x(i),y(i)Intuition:δj(l)="error"  of  node  j  in  layer  lso\;x,y\;substitute\;for\;x^{(i)},y^{(i)}\\ Intuition:\delta_j^{(l)}="error"\;of\;node\;j\;in\;layer\;l\\

If the network has only four layers:

δ(4)=a(4)yδ(3)=(Θ(3))Tδ(4).sigmoid(Θ(2)a(2))=(Θ(3))Tδ(4).(sigmoid(Θ(2)a(2)).(1sigmoid(Θ(2)a(2)))=(Θ(3))Tδ(4).(a(3).(1a(3)))δ(2)=(Θ(2))Tδ(3).sigmoid(Θ(1)a(1))=(Θ(2))Tδ(3).(a(2).(1a(2)))and  input  layer  dont  have  δ(1)\delta^{(4)}=a^{(4)}-y\\ *\delta^{(3)}=(\varTheta^{(3)})^T\delta^{(4)}.*sigmoid'(\varTheta^{(2)}a^{(2)})\\ =(\varTheta^{(3)})^T\delta^{(4)}.*(sigmoid(\varTheta^{(2)}a^{(2)}).*(1-sigmoid(\varTheta^{(2)}a^{(2)}))\\ =(\varTheta^{(3)})^T\delta^{(4)}.*(a^{(3)}.*(1-a^{(3)}))\\ *\delta^{(2)}=(\varTheta^{(2)})^T\delta^{(3)}.*sigmoid'(\varTheta^{(1)}a^{(1)})\\ =(\varTheta^{(2)})^T\delta^{(3)}.*(a^{(2)}.*(1-a^{(2)}))\\ and\;input\;layer\;don't\;have\;\delta^{(1)}

Set  Δij(l)=0  (for  all  i,j,l)For  i=1  to  mSet  a(1)=x(i)forward  propagation  to  compute  a(l),l=2,3,...,LUsing  y(i),  compute  δ(L)=a(L)y(i)Compute  δ(L1),δ(L2),...,δ(2)Δ(l):=Δ(l)+δ(l+1)(a(l))TDij(l)=1mΔij(l)+λmΘij(l)  if  j0Dij(l)=1mΔij(l)  if  j=0Θij(l)J(Θ)=Dij(l)Θ(l)J(Θ)=D(l)Set\;\Delta_{ij}^{(l)}=0\;(for\;all\;i,j,l)\\ For\;i=1\;to\;m\\ Set\;a^{(1)}=x^{(i)}\\ forward\;propagation\;to\;compute\;a^{(l)},l=2,3,...,L\\ Using\;y^{(i)},\;compute\;\delta^{(L)}=a^{(L)}-y^{(i)}\\ Compute\;\delta^{(L-1)},\delta^{(L-2)},...,\delta^{(2)}\\ \Delta^{(l)}:=\Delta^{(l)}+\delta^{(l+1)}(a^{(l)})^T\\ D_{ij}^{(l)}=\frac{1}{m}\Delta_{ij}^{(l)}+\frac{\lambda}{m}\varTheta_{ij}^{(l)}\;if\;j\neq0\\ D_{ij}^{(l)}=\frac{1}{m}\Delta_{ij}^{(l)}\;if\;j=0\\ \frac{\partial}{\partial\varTheta_{ij}^{(l)}}J(\varTheta)=D_{ij}^{(l)}\\ \frac{\partial}{\partial\varTheta^{(l)}}J(\varTheta)=D^{(l)}

  • Gradient Checking

let  θ="unrolled"  Θ(l)θ=[Θ(1)(:);Θ(2)(:);...;Θ(L)(:)]Rnso  J(Θ(1),...,Θ(L))=J(θ)=J([θ1θ2...θn])and  θiJ(θ)=J([θ1...θi+ϵ...θn])J([θ1...θiϵ...θn])2ϵlet\;\theta="unrolled"\;\Theta^{(l)}\\ \theta=[\Theta^{(1)}(:);\Theta^{(2)}(:);...;\Theta^{(L)}(:)]\in\mathbb{R}^{n}\\ so\;J(\Theta^{(1)},...,\Theta^{(L)})=J(\theta)=J([\theta_1\quad\theta_2\quad...\quad\theta_n])\\ and\;\frac{\partial}{\partial\theta_i}J(\theta)=\frac{J([\theta_1\quad...\quad\theta_i+\epsilon\quad...\quad\theta_n])-J([\theta_1\quad...\quad\theta_i-\epsilon\quad...\quad\theta_n])}{2\epsilon}

1
2
3
4
5
6
7
for i = 1 : n
thetaPlus = theta;
thetaPlus(i) = thetaPlus(i) + EPSILON;
thetaMinus = theta;
thetaMinus(i) = thetaMinus(i) - EPSILON;
gradApprox(i) = (J(thetaPlus) - J(thetaMinus)) / (2 * EPSILON);
end;

Check that gradAppprox ≈ DVec, while epsilon = 1e-4

  • Implementation Note

    • Implement backprop to compute DVec
    • Implement numerical gradient check to compute gradApprox
    • Make sure they give similar values
    • Turn off gradient checking. Using backprop code for learning
      • numerical gradient checking is computenionally expensive compared with backprop
  • Random Initialization

    E.g.

    Theta1 = rand(10, 11) * (2 * INIT_EPSILON) - INIT_EPSILON;

    In order to break symmetry

# Bias and Variance

Take regularized linear regression problem as an example

Split the training examples into three parts randomly:

  • training set (to train and modify parameters) (around 60%)

    J(θ)=12m[i=1m[hθ(x(i))y(i)]2+λj=1nθj2]We  train  the  model  through  minimizing  J(θ)Jtrain(θ)=12m[i=1m[hθ(x(i))y(i)]2We  use  Jtrain(θ)  to  evaluate  how  well  our  model  fits  the  training  set(we  now  only  care  about  how  well  our  model  fits  the  training  set)J(\theta)=\frac{1}{2m}[\sum_{i=1}^m[h_\theta(x^{(i)})-y^{(i)}]^2+\lambda\sum_{j=1}^n\theta_j^2]\\ We\;train\;the\;model\;through\;minimizing\;J(\theta)\\ J_{train}(\theta)=\frac{1}{2m}[\sum_{i=1}^m[h_\theta(x^{(i)})-y^{(i)}]^2\\ We\;use\;J_{train}(\theta)\;to\;evaluate\;how\;well\;our\;model\;fits\;the\;training\;set\\ (we\;now\;only\;care\;about\;how\;well\;our\;model\;fits\;the\;training\;set)\\

  • validation set *(to prevent overfitting and help choose model(like degree of polynomial)) * (around 20%)

    Jcv(θ)=12m[i=1m[hθ(x(i))y(i)]2We  use  Jcv(θ)  to  prevent  overfitting.After  a  iteration,if  J(θ)  and  Jtrain(θ)  decreases  but  Jcv(θ)  increases,that  means  overfitting  problem.J_{cv}(\theta)=\frac{1}{2m}[\sum_{i=1}^m[h_\theta(x^{(i)})-y^{(i)}]^2\\ We\;use\;J_{cv}(\theta)\;to\;prevent\;overfitting.\\ After\;a\;iteration,if\;J(\theta)\;and\;J_{train}(\theta)\;decreases\;but\;J_{cv}(\theta)\;increases,\\ that\;means\;overfitting\;problem.

  • test set (only after finishing training to test the performance of the model) (around 20%)

    Jtest(θ)=12m[i=1m[hθ(x(i))y(i)]2After  all  trainings,we  use  Jtest(θ)  to  evaluate  the  performance  of  the  modelJ_{test}(\theta)=\frac{1}{2m}[\sum_{i=1}^m[h_\theta(x^{(i)})-y^{(i)}]^2\\ After\;all\;trainings,we\;use\;J_{test}(\theta)\;to\;evaluate\;the\;performance\;of\;the\;model

3

Problems:

  • High Bias(underfit)

    Jtrain(Θ)  and  Jcv(Θ)  are  both  highJ_{train}(\Theta)\;and\;J_{cv}(\Theta)\;are\;both\;high

4

  • High Variance(overfit)

    Jtrain(Θ)  will  be  lowJcv(Θ)>>Jtrain(Θ)J_{train}(\Theta)\;will\;be\;low\\ J_{cv}(\Theta)>>J_{train}(\Theta)

5

  • Solutions

    • Get more training examples to fix high variance
    • Try smaller sets of features to fix high variance
    • Try getting additional features to fix high bias
    • Try adding polynomial features to fix high bias
    • Try decreasing lambda to fix high bias
    • Try increasing lambda to fix high variance

# Error metrics for skewed classes

If class y=1 only counts for 0.5%, and y=0 counts for 99.5%, then y=1 is called a skewed class.

and predicting y=0 all the time may low the error rate, but it turns out not to be a good idea

predicted class↓/actual class→ 1 0
1 true positive false positive
0 false negative true negative

Precision=num  of  True  positivenum  of  Ture  positive+num  of  False  positiveRecall=num  of  True  positivenum  of  True  positive+num  of  False  negativeon  validation  setPrecision=\frac{num\;of\;True\;positive}{num\;of\;Ture\;positive+num\;of\;False\;positive}\\ Recall=\frac{num\;of\;True\;positive}{num\;of\;True\;positive+num\;of\;False\;negative}\\ *on\;validation\;set

  • Suppose we want to predict y=1 only if very confident

    0.9hθ(x)predict  y=1hθ(x)<0.9predict  y=00.9\leq h_\theta(x)\quad predict\;y=1\\ h_\theta(x)<0.9\quad predict\;y=0

    Then the precision will be higher and recall will be lower.

  • Suppose we want to avoid missing too many case of y=0

    0.3hθ(x)predict  y=1hθ(x)<0.3predict  y=00.3\leq h_\theta(x)\quad predict\;y=1\\ h_\theta(x)<0.3\quad predict\;y=0

    Then the precision will be lower and recall will be higher.

  • F1  Score:2×PrecisionRecallPrecision+RecallF_1\;Score:2\times\frac{Precision*Recall}{Precision+Recall}

    We use F1 Score to evaluate the performance of the algorithm on skewed classes.

# Support Vector machine(SVM)

6

minθ1m[i=1my(i)(log(hθ(x(i))))+(1y(i))(log(1hθ(x(i))))]+λ2mj=1nθj2Replace  log(z),log(1z)  with  cost1(z),cost0(z):minθ1m[i=1my(i)(cost1(hθ(x(i))))+(1y(i))(cost0(hθ(x(i)))]+λ2mj=1nθj2=minθi=1m[y(i)(cost1(hθ(x(i))))+(1y(i))(cost0(hθ(x(i)))]+λ2j=1nθj2=minθCi=1m[y(i)(cost1(hθ(x(i))))+(1y(i))(cost0(hθ(x(i)))]+12j=1nθj2min_\theta\frac{1}{m}[\sum_{i=1}^my^{(i)}(-log(h_\theta(x^{(i)})))+(1-y^{(i)})(-log(1-h_\theta(x^{(i)})))]+\frac{\lambda}{2m}\sum_{j=1}^n\theta_j^2\\ Replace\;-log(z),-log(1-z)\;with\;cost_1(z),cost_0(z):\\ min_\theta\frac{1}{m}[\sum_{i=1}^my^{(i)}(cost_1(h_\theta(x^{(i)})))+(1-y^{(i)})(cost_0(h_\theta(x^{(i)}))]+\frac{\lambda}{2m}\sum_{j=1}^n\theta_j^2\\ =min_\theta\sum_{i=1}^m[y^{(i)}(cost_1(h_\theta(x^{(i)})))+(1-y^{(i)})(cost_0(h_\theta(x^{(i)}))]+\frac{\lambda}{2}\sum_{j=1}^n\theta_j^2\\ =min_\theta C\sum_{i=1}^m[y^{(i)}(cost_1(h_\theta(x^{(i)})))+(1-y^{(i)})(cost_0(h_\theta(x^{(i)}))]+\frac{1}{2}\sum_{j=1}^n\theta_j^2

Large C: Lower bias, high variance

Small C: Higher bias, low variance

# Kernels

Given x, compute some new features depending on proximity to landmarks.

f1=similarity(x,l(1))=exl(1)22σ2=exp(j=1n(xjlj(1))22σ2)f2=similarity(x,l(2))=exl(2)22σ2=exp(j=1n(xjlj(2))22σ2)......hθ(x)=θ0+θ1f1+θ2f2+...predict  y=1  if  hθ(x)0  and  0  otherwisef_1=similarity(x,l^{(1)})=e^{-\frac{|x-l^{(1)}|^2}{2\sigma^2}}=exp(-\frac{\sum_{j=1}^n(x_j-l_j^{(1)})^2}{2\sigma^2})\\ f_2=similarity(x,l^{(2)})=e^{-\frac{|x-l^{(2)}|^2}{2\sigma^2}}=exp(-\frac{\sum_{j=1}^n(x_j-l_j^{(2)})^2}{2\sigma^2})\\ ......\\ h_\theta(x)=\theta_0+\theta_1f_1+\theta_2f_2+...\\ predict\;y=1\;if\;h_\theta(x)\geq 0\;and\;0\;otherwise

  • f is called Gaussian kernel. when |x - l|≈0, f≈1, and |x - l|=∞, f ≈0

  • x which is near l tends to be predicted positive.

We  choose  l(i)=x(i),i=1,2,3,...,mf1(i)=sim(x(i),x(1))f2(i)=sim(x(i),x(2))...fi(i)=sim(x(i),x(i))=1...fm(i)=sim(x(i),x(m))f(i)=[f0(i)f1(i)...fm(m)]Rm+1We\;choose\;l^{(i)}=x^{(i)},i=1,2,3,...,m\\ f_1^{(i)}=sim(x^{(i)},x^{(1)})\\ f_2^{(i)}=sim(x^{(i)},x^{(2)})\\ ...\\ f_i^{(i)}=sim(x^{(i)},x^{(i)})=1\\ ...\\ f_m^{(i)}=sim(x^{(i)},x^{(m)})\\ f^{(i)}=\begin{bmatrix}f_0^{(i)}\\f_1^{(i)}\\...\\f_m^{(m)}\end{bmatrix}\in\mathbb{R}^{m+1}

minθCi=1m[y(i)(cost1(θTf(i)))+(1y(i))(cost0(θTf(i)))]+12j=1mθj2nowθRm+1Large  σ2:Features  fi  vary  more  smoothly.High  bias,lower  varianceSmall  σ2:Features  fi  vary  less  smoothly.Lower  bias,Higher  variancemin_\theta C\sum_{i=1}^m[y^{(i)}(cost_1(\theta^Tf^{(i)}))+(1-y^{(i)})(cost_0(\theta^Tf^{(i)}))]+\frac{1}{2}\sum_{j=1}^m\theta_j^2\\ now\quad\theta\in\mathbb{R}^{m+1}\\ Large\;\sigma^2:Features\;f_i\;vary\;more\;smoothly.High\;bias,lower\;variance\\ Small\;\sigma^2:Features\;f_i\;vary\;less\;smoothly.Lower\;bias,Higher\;variance

  • If n is large (relative to m), Use logistic regression, or SVM without a kernel.

  • If n is small, m is intermediate, Use SVM with Gaussian kernel.

  • If n is small, m is large (n=1~1000, m=50000+), Create/add more features, then use logistic regression or SVM without a kernel.

  • Neural network likely to work well for most of these settings, but may be slower to train.

# Unsupervised learning

  • no given values or classification

  • Cluster data(genes) or Non-cluster data(Cocktail party problem)

  • find some “structure” of the dataset

# Clustering algorithm: K-means algorithm

Repeat:

1. Randomly choose two cluster centroids. The red one and the blue one.

2. Cluster assignment: color each of the data points red or blue depending on which cluster centroid it is more closer to.

3. Move blue cluster centroid to the average point of all blue points.

Move red cluster centroid to the average point of all red points.

More formally:

  • Input:

    • K (number of clusters)
    • Training set{x1, x2, …, xm}
  • Process:

    Randomly  initialize  K  cluster  centroids  μ1,μ2,...,μKRnRepeat:{for  i=1  to  mc(i)=index(from  1  to  K)  of  cluster  centroid  closest  to  x(i)(minc(i)=kx(i)μk2)for  k=1  to  Kμk=average  (mean)  of  points  assigned  to  cluser  k(μk=1mi=1m,c(i)=kx(i)Rn)}Randomly\;initialize\;K\;cluster\;centroids\;\mu_1,\mu_2,...,\mu_K\in\mathbb{R}^n\\ Repeat:\{\\ for\;i=1\;to\;m\\ c^{(i)}=index(from\;1\;to\;K)\;of\;cluster\;centroid\;closest\;to\;x^{(i)}\\ (min_{c^{(i)}=k}|x^{(i)}-\mu_k|^2)\\ for\;k=1\;to\;K\\ \mu_k=average\;(mean)\;of\;points\;assigned\;to\;cluser\;k\\ (\mu_k=\frac{1}{m}\sum_{i=1}^{m,c^{(i)=k}}x^{(i)}\in\mathbb{R}^n)\\ \}\\

Optimization objective(cost function):

J(c(1),...,c(m),μ1,...,μk)=1mi=1mx(i)μc(i)2J(c^{(1)},...,c^{(m)},\mu_1,...,\mu_k)=\frac{1}{m}\sum_{i=1}^m|x^{(i)}-\mu_{c^{(i)}}|^2\\

Randomly initialize and run K-means several times and find the lowest J

Elbow methord

7

*how to choose the number of cluster

# Dimensionality reduction:Principal Component Analysis Algorithm

  • Data preprocessing:

    replace  each  xj(i)  with  xj(i)1mi=1mxj(i)maxi=1:m{xj(i)}mini=1:m{xj(i)}Σ=1mi=1m(x(i))(x(i))TRn×ncompute  eigenvectors  of  matrix  Sigma  by[U,S,V]=svd(Σ)(Singular  value  decomposition)replace\;each\;x_j^{(i)}\;with\;\frac{x_j^{(i)}-\frac{1}{m}\sum_{i=1}^mx_j^{(i)}}{max_{i=1:m}\{x_j^{(i)}\}-min_{i=1:m}\{x_j^{(i)}\}}\\ \Sigma=\frac{1}{m}\sum_{i=1}^m(x^{(i)})(x^{(i)})^T\in\mathbb{R}^{n\times n}\\ compute\;eigenvectors\;of\;matrix\;Sigma\;by\\ [U,S,V]=svd(\Sigma)(Singular\;value\;decomposition)\\

  • PCA

    [U,S,V]=svd(Σ)U=[...u(1)u(2)u(3)...u(n)...]Ureduce=U(:,1:k)=[...u(1)u(2)u(3)...u(k)...]Rn×kz(i)=UreduceTx(i)Rk×1xapprox(i)=Ureducez(i)Rn×1(noting  that  UTU1...)[U,S,V]=svd(\Sigma)\\ U=\begin{bmatrix} |&|&|&...&|\\ u^{(1)}&u^{(2)}&u^{(3)}&...&u^{(n)}\\ |&|&|&...&|\\ \end{bmatrix}\\ U_{reduce}=U(:,1:k)=\begin{bmatrix} |&|&|&...&|\\ u^{(1)}&u^{(2)}&u^{(3)}&...&u^{(k)}\\ |&|&|&...&|\\ \end{bmatrix}\in\mathbb{R}^{n\times k}\\ z^{(i)}=U_{reduce}^Tx^{(i)}\in\mathbb{R}^{k\times1}\\ x_{approx}^{(i)}=U_{reduce}z^{(i)}\in\mathbb{R}^{n\times1}\\ (noting\;that\;U^T\neq U^{-1}...)

    8

  • Choosing k

    We  choose  k  to  minimize:1mi=1mx(i)xapprox(i)21mi=1mx(i)20.01In  other  words,"99%  of  variance  is  retained"1mi=1mx(i)xapprox(i)21mi=1mx(i)2=1i=1kSiii=1nSiitry  k=1,2,3,...until  1mi=1mx(i)xapprox(i)21mi=1mx(i)20.01We\;choose\;k\;to\;minimize:\\ \frac{\frac{1}{m}\sum_{i=1}^m|x^{(i)}-x_{approx}^{(i)}|^2}{\frac{1}{m}\sum_{i=1}^m|x^{(i)}|^2}\leq 0.01\\ In\;other\;words,"99\%\;of\;variance\;is\;retained"\\ \frac{\frac{1}{m}\sum_{i=1}^m|x^{(i)}-x_{approx}^{(i)}|^2}{\frac{1}{m}\sum_{i=1}^m|x^{(i)}|^2}=1-\frac{\sum_{i=1}^kS_{ii}}{\sum_{i=1}^nS_{ii}}\\ try\;k=1,2,3,...until\;\frac{\frac{1}{m}\sum_{i=1}^m|x^{(i)}-x_{approx}^{(i)}|^2}{\frac{1}{m}\sum_{i=1}^m|x^{(i)}|^2}\leq 0.01

# Anomaly detection

  • Original Model

μj=1mi=1mxj(i),σj2=1mi=1m(xj(i)μj)2p(x(i))=j=1n12πσjexp((xj(i)μj)22σj2)Gaussian  distributionflag  x(i)  is  anomaly  if  p(x(i))<ϵ\mu_j=\frac{1}{m}\sum_{i=1}^mx_j^{(i)},\sigma_j^2=\frac{1}{m}\sum_{i=1}^m(x_j^{(i)}-\mu_j)^2\\ p(x^{(i)})=\prod_{j=1}^n\frac{1}{\sqrt{2\pi}\sigma_j}exp(-\frac{(x_j^{(i)}-\mu_j)^2}{2\sigma_j^2})\\ Gaussian\;distribution\\ flag\;x^{(i)}\;is\;anomaly\;if\;p(x^{(i)})<\epsilon

# Example:Aircraft engines motivating

  • 10000 good (normal) engines

    20 flawed engines (anomalous)

  • Training set: 6000 good engines (all y=0)

    Cross validation: 2000 good engines(y=0), 10 anomalous engines(y=1)

    Test set: 2000 good engines(y=0), 10 anomalous engines(y=1)

Fit model p(x) on training set.

On a cross validation/test set example, predict:

y={1if  p(x)<ϵ(anomaly)0if  p(x)ϵ(nomal)y=\begin{cases}1\quad if\;p(x)<\epsilon(anomaly)\\ 0\quad if\;p(x)\geq\epsilon(nomal) \end{cases}

  • Possible evaluation metrics:
    • Precision/Recall
    • F1-score

When we have one or more skewed classes (the number of anomaly examples is pretty relative low), we tend to choose anomaly detection rather than logistic regression or neural network.

  • Multivariate Gaussian Model
    • Fit model p(x) by setting:

μ=1mi=1mx(i),Σ=1mi=1m(x(i)μ)(x(i)μ)Tμ=[μ1μ2...μn]Rn×1,Σ=[σ120...00σ22...0............00...σn2]Rn×n\mu=\frac{1}{m}\sum_{i=1}^mx^{(i)},\Sigma=\frac{1}{m}\sum_{i=1}^m(x^{(i)}-\mu)(x^{(i)}-\mu)^T\\ \mu=\begin{bmatrix}\mu_1\\\mu_2\\...\\\mu_n\end{bmatrix}\in\mathbb{R}^{n\times 1},\Sigma=\begin{bmatrix}\sigma_1^2&0&...&0\\0&\sigma_2^2&...&0\\ ...&...&...&...\\0&0&...&\sigma_n^2\end{bmatrix}\in\mathbb{R}^{n\times n}

Given a new example x, compute:

p(x)=1(2π)n2Σ12exp(12(xμ)TΣ1(xμ))must  have  m>n(m10n  is  ok)p(x)=\frac{1}{(2\pi)^{\frac{n}{2}}|\Sigma|^\frac{1}{2}}exp(-\frac{1}{2}(x-\mu)^T\Sigma^{-1}(x-\mu))\\ must\;have\;m>n(m\geq 10n\;is\;ok)

# Recommender System

# Example: movie rating

9

nu=no.users,nm=no.moviesr(i,j)=1if  user  j  has  rated  movie  iy(i,j)=rating  by  user  j  on  movie  i(if  defined)θ(j)=parameter  vector  for  user  jx(i)=feature  vector  for  movie  iFor  user  j,movie  i,predicted  rating:(θ(j))T(x(i))m(j)=no.of  movies  rated  by  user  jminθ(j)12m(j)i:r(i,j)=1((θ(j))T(x(i))y(i,j))2+λ2m(j)k=1n(θk(j))2n_u=no.users,n_m=no.movies\\ r(i,j)=1\quad if\;user\;j\;has\;rated\;movie\;i\\ y^{(i,j)}=rating\;by\;user\;j\;on\;movie\;i(if\;defined)\\ \theta^{(j)}=parameter\;vector\;for\;user\;j\\ x^{(i)}=feature\;vector\;for\;movie\;i\\ For\;user\;j,movie\;i,predicted\;rating:(\theta^{(j)})^T(x^{(i)})\\ m^{(j)}=no.of\;movies\;rated\;by\;user\;j\\ min_{\theta^{(j)}}\frac{1}{2m^{(j)}}\sum_{i:r(i,j)=1}((\theta^{(j)})^T(x^{(i)})-y^{(i,j)})^2+\frac{\lambda}{2m^{(j)}}\sum_{k=1}^n(\theta_k^{(j)})^2

Just like linear regression problem

J(θ(1),...,θ(nu))=12j=1nui:r(i,j)=1((θ(j))Tx(i)y(i,j))2+λ2j=1nuk=1n(θk(j))2minJJ(\theta^{(1)},...,\theta^{(n_u)})=\frac{1}{2}\sum_{j=1}^{n_u}\sum_{i:r(i,j)=1}((\theta^{(j)})^Tx^{(i)}-y^{(i,j)})^2+\frac{\lambda}{2}\sum_{j=1}^{n_u}\sum_{k=1}^n(\theta_k^{(j)})^2\\ minJ

Gradient descent:

θk(j)=θk(j)αi:r(i,j)=1((θ(j))Tx(i)y(i,j))xk(i),k=0θk(j)=θk(j)α(i:r(i,j)=1((θ(j))Tx(i)y(i,j))xk(i)+λθk(j)),k0\theta_k^{(j)}=\theta_k^{(j)}-\alpha\sum_{i:r(i,j)=1}((\theta^{(j)})^Tx^{(i)}-y^{(i,j)})x_k^{(i)},k=0\\ \theta_k^{(j)}=\theta_k^{(j)}-\alpha(\sum_{i:r(i,j)=1}((\theta^{(j)})^Tx^{(i)}-y^{(i,j)})x_k^{(i)}+\lambda\theta_k^{(j)}),k\neq0\\

  • Collaborative filtering

    10

    xθx  θ...x\rightarrow\theta\rightarrow x\rightarrow\;\theta\rightarrow...

1. Initialize:

x(1),...,x(nm),θ(1),...,θ(nu)x^{(1)},...,x^{(n_m)},\theta^{(1)},...,\theta^{(n_u)}

to small random values.

2. Minimize:

J(x(1),...,x(nm),θ(1),...,θ(nm))for  every  j=1,...,nu,i=1,...,nmxk(i)=xk(i)α(j:r(i,j)=1((θ(j))Tx(i)y(i,j))θk(j)+λxk(i))θk(j)=θk(j)α(i:r(i,j)=1((θ(j))Tx(i)y(i,j))xk(i)+λθk(j))J(x^{(1)},...,x^{(n_m)},\theta^{(1)},...,\theta^{(n_m)})\\ for\;every\;j=1,...,n_u,i=1,...,n_m\\ x_k^{(i)}=x_k^{(i)}-\alpha(\sum_{j:r(i,j)=1}((\theta^{(j)})^Tx^{(i)}-y^{(i,j)})\theta_k^{(j)}+\lambda x_k^{(i)})\\ \theta_k^{(j)}=\theta_k^{(j)}-\alpha(\sum_{i:r(i,j)=1}((\theta^{(j)})^Tx^{(i)}-y^{(i,j)})x_k^{(i)}+\lambda\theta_k^{(j)})

  • Low rank matrix factorization & Mean normalization

    X=[(x(1))T(x(2))T...(x(nm))T]Θ=[(θ(1))T(θ(2))T...(θ(nu))T]Y=XΘTY=[5500?5??0??40??0054?0050?]Y=[2.52.52.52.5?2.5??2.5??22??2.252.252.751.75?1.251.253.751.25?]X=\begin{bmatrix} -(x^{(1)})^T-\\ -(x^{(2)})^T-\\ ...\\ -(x^{(n_m)})^T-\\ \end{bmatrix} \Theta=\begin{bmatrix} -(\theta^{(1)})^T-\\ -(\theta^{(2)})^T-\\ ...\\ -(\theta^{(n_u)})^T-\\ \end{bmatrix}\\ Y=X\Theta^T\\ Y=\begin{bmatrix} 5&5&0&0&?\\ 5&?&?&0&?\\ ?&4&0&?&?\\ 0&0&5&4&?\\ 0&0&5&0&? \end{bmatrix}\Rightarrow Y=\begin{bmatrix} 2.5&2.5&-2.5&-2.5&?\\ 2.5&?&?&-2.5&?\\ ?&2&-2&?&?\\ -2.25&-2.25&2.75&1.75&?\\ -1.25&-1.25&3.75&-1.25&? \end{bmatrix}

# Large data set

  • Batch gradient descent

    θj=θjα1mi=1m(hθ(xj(i))y(i))xj(i)\theta_j'=\theta_j-\alpha\frac{1}{m}\sum_{i=1}^m(h_\theta(x_j^{(i)})-y^{(i)})\cdot x^{(i)}_j\\

    In every iteration, we use all m examples.

  • Mini-batch gradient descent

    get b = mini-batch size

    for  k=1,11,21,...{θj=θjα110i=kk+9(hθ(xj(i))y(i))xj(i)}for\;k=1,11,21,...\{\\ \theta_j'=\theta_j-\alpha\frac{1}{10}\sum_{i=k}^{k+9}(h_\theta(x_j^{(i)})-y^{(i)})\cdot x^{(i)}_j\\ \}\\

    In every iteration, we use a subset of training examples.

  • Stochastic gradient descent

    1. Randomly reshuffle your training set

    for  i=1  to  m{change  α=const1i+const2θj=θjα(hθ(xj(i))y(i))xj(i)}for\;i=1\;to\;m\{\\ change\;\alpha=\frac{const_1}{i+const_2}\\ \theta_j'=\theta_j-\alpha(h_\theta(x_j^{(i)})-y^{(i)})\cdot x^{(i)}_j\\ \}

    In every iteration, we use only one training set and slowly reduce learning rate. Calculate average cost function every 1000 iterations


# 课内部分,用矩阵运算较多

# 机器学习分类

  • Binary classification
  • Multiclass classification
  • Multi-label Classification
    • Train separate classifier for each label. But there might be correlations between the classes.
  • Regression
  • Supervised Learning
  • Unsupervised Learning
  • Semi-supervised Learning (some data has labels, some doesn’t)
  • Reinforcement Learning (e.g. Chat Robots)
    • There is no supervisor, only a reward signal.
    • Time really matters (sequential).
    • Reinforcement learning is based on the reward hypothesis.
    • Agent goal: maximize cumulative reward.
    • Select actions to maximize the expected cumulative reward.
  • Batch (offline) learning: learn from all known data (a very common protocol).
  • Online learning: learn from the sequential data.
  • Active learning: well-motivated in modern machine learning problems where data may be abundant but labels are scarce or expensive to obtain.
    • Active learning (sequentially) queries the yny_n of the strategically chosen xnx_n, which is to be labeled by an oracle (e.g., a human annotator).
    • Key hypothesis: if the learning algorithm is allowed to choose the data from which it learns, it will perform better with less training.
  • Learning with concrete/raw/abstract features.

# 矩阵求导运算

ddx(Ax)=Adxdx=IdyTxdx=dxTyx=yddx(xTAx)=(A+AT)x,A  is  square\begin{aligned} & \frac{d}{d\mathbf{x}}(A\mathbf{x})=A\\ & \frac{d\mathbf{x}}{d\mathbf{x}}=\mathbf{I}\\ & \frac{d\mathbf{y}^T\mathbf{x}}{d\mathbf{x}}=\frac{d\mathbf{x}^T\mathbf{y}}{\mathbf{x}}=\mathbf{y}\\ & \frac{d}{d\mathbf{x}}(\mathbf{x}^TA\mathbf{x})=(A+A^T)\mathbf{x},A\;is\;square \end{aligned}

# Linear Regression & Logistic Regression

Linear Regression:

E(w)=12Xwy2=12(Xwy)T(Xwy)E(w)=XTXwXTy2E(w)=XTX\begin{aligned} & E(\mathbf{w})=\frac{1}{2}\|X\mathbf{w}-\mathbf{y}\|^2=\frac{1}{2}(X\mathbf{w}-\mathbf{y})^T(X\mathbf{w}-\mathbf{y})\\ & \nabla E(\mathbf{w})=X^TX\mathbf{w}-X^T\mathbf{y}\\ & \nabla^2 E(\mathbf{w})=X^TX \end{aligned}

使用牛顿法:

w=wE(w)(2E(w))1=(XTX)1XTy\mathbf{w}=\mathbf{w}'-\nabla E(\mathbf{w}')(\nabla^2E(\mathbf{w}'))^{-1}=(X^TX)^{-1}X^Ty

Logistic Regression:

E=i[yiwTxi+ln(1+ewTxi)][E(w)]=i[yi+h(xi)]xi2[E(w)]=ixih(xi)(1h(xi))xiT\begin{aligned} &-E=\sum_i[-y_i\mathbf{w}^T\mathbf{x}_i+ln(1+e^{\mathbf{w}^T\mathbf{x}_i})]\\ & \nabla [-E(\mathbf{w})]=\sum_{i}[-y_i+h(\mathbf{x}_i)]\mathbf{x}_i\\ & \nabla^2[-E(\mathbf{w})]=\sum_i\mathbf{x}_ih(\mathbf{x}_i)(1-h(\mathbf{x}_i))\mathbf{x}_i^T \end{aligned}

其中,h(x)=11+ewTxh(\mathbf{x})=\frac{1}{1+e^{-\mathbf{w}^T\mathbf{x}}}。然后可以使用牛顿法迭代。

推导过程:

我们希望:

p(yi    xi)={h(xi)yi=11h(xi)yi=0p(y_i\;|\;\mathbf{x}_i)=\begin{cases} h(\mathbf{x}_i)&y_i=1\\ 1-h(\mathbf{x}_i)&y_i=0 \end{cases}

可以构造:

p(yi    xi)=h(xi)yi(1h(xi))1yip(y_i\;|\;\mathbf{x}_i)=h(\mathbf{x}_i)^{y_i}(1-h(\mathbf{x}_i))^{1-y_i}

然后:

E=likelihood(h)=ip(xi)p(yi    xi)\begin{aligned} & E=likelihood(h)=\prod_i p(\mathbf{x}_i)p(y_i\;|\;\mathbf{x}_i) \end{aligned}

如果我们认为p(xi)p(\mathbf{x}_i) 都相同,即样本是均匀分布出现的,(实际上不需要这个假设也行,反正p(xi)\prod p(\mathbf{x}_i) 是定值),那么:

Eip(yi    xi)iyilnh(xi)+(1yi)ln(1h(xi))\begin{aligned} E&\propto \prod_ip(y_i\;|\;\mathbf{x}_i)\\ &\propto \sum_iy_ilnh(\mathbf{x}_i)+(1-y_i)ln(1-h(\mathbf{x}_i))\\ \end{aligned}

我们希望最大 likelihood,即要最小化E-E。其中,h(x)=11+ewTxh(\mathbf{x})=\frac{1}{1+e^{-\mathbf{w}^T\mathbf{x}}}

化简可得:

E=iyiln(11+ewTxi)(1yi)ln(11+ewTxi)=i[yiwTxi+ln(1+ewTxi)]\begin{aligned} -E&=\sum_i-y_iln(\frac{1}{1+e^{-\mathbf{w}^T\mathbf{x_i}}})-(1-y_i)ln(\frac{1}{1+e^{\mathbf{w}^T\mathbf{x_i}}})\\ &=\sum_i[-y_i\mathbf{w}^T\mathbf{x}_i+ln(1+e^{\mathbf{w}^T\mathbf{x}_i})] \end{aligned}

然后一阶梯度为:

[E(w)]=i[yixi+ewTxi1+ewTxixi]=i[yi+h(xi)]xi\begin{aligned} \nabla [-E(\mathbf{w})]&=\sum_i[-y_i\mathbf{x}_i+\frac{e^{\mathbf{w}^T\mathbf{x}_i}}{1+e^{\mathbf{w}^T\mathbf{x}_i}}\mathbf{x}_i]\\ &=\sum_{i}[-y_i+h(\mathbf{x}_i)]\mathbf{x}_i \end{aligned}

二阶梯度为(那个xiT\mathbf{x}_i^T 是因为数乘hxih\mathbf{x}_i 的求导法则):

2[E(w)]=ih(xi)wxiT\begin{aligned} \nabla^2 [-E(\mathbf{w})]=\sum_i\frac{\nabla h(\mathbf{x}_i)}{\nabla \mathbf{w}}\mathbf{x}_i^T \end{aligned}

h(xi)w=h(xi)(1h(xi))xi\frac{\nabla h(\mathbf{x}_i)}{\nabla \mathbf{w}}=h(\mathbf{x}_i)(1-h(\mathbf{x}_i))\mathbf{x}_i。sigmoid 函数求导f(x)=f(x)(1f(x))f'(x)=f(x)(1-f(x))
所以:

2[E(w)]=ixih(xi)(1h(xi))xiT\nabla^2[-E(\mathbf{w})]=\sum_i\mathbf{x}_ih(\mathbf{x}_i)(1-h(\mathbf{x}_i))\mathbf{x}_i^T

# Gradient Descent 相关

# Batch vs. Stochastic Gradient Descent

  • Batch gradient descent has to scan through the entire training set before taking a single step—a costly operation if 𝑚 is large.

for  every  j:wj=wjα[XTXwXTy]jfor\;every\;j:\\ \mathbf{w}_j=\mathbf{w}_j-\alpha [X^TX\mathbf{w}-X^Ty]_j

  • SGD can start making progress right away, and continues to make progress with each example it looks at.
    Often, SGD gets w\mathbf{w} “close” to the minimum much faster than batch gradient descent.

for  i=1  to  m,every  j:wj=wjα[XiTXiwXiTyi]jfor\;i=1\;to\;m,every\;j:\\ \mathbf{w}_j=\mathbf{w}_j-\alpha [X_i^TX_i\mathbf{w}-X_i^Ty_i]_j


# Polyak’s Classical Momentum

迭代过程中:

wt+1=wt+vt+1vt+1=μvtαE(wt)\begin{aligned} & \mathbf{w}_{t+1}=\mathbf{w}_t+\mathbf{v}_{t+1}\\ & \mathbf{v}_{t+1}=\mu\mathbf{v}_t-\alpha \nabla E(\mathbf{w}_t) \end{aligned}

其中,v\mathbf{v} 被称为 velocity vector,而μ\mu momentum coefficient,通常略微小于 1。

# Nesterov’s Accelerated Gradient

迭代过程中:

wt+1=wt+vt+1vt+1=μvtαE(wt+μvt)\begin{aligned} & \mathbf{w}_{t+1}=\mathbf{w}_t+\mathbf{v}_{t+1}\\ & \mathbf{v}_{t+1}=\mu\mathbf{v}_t-\alpha \nabla E(\mathbf{w}_t+\mu\mathbf{v}_t) \end{aligned}

# Algorithms with Adaptive Learning Rates: Agrad/Adadelta/RMSpropAdam

# PCA(Principal Component Analysis)

Given a sample set of 𝑚 observations on a vector of 𝑑 variables

{x1,...,xm}Rd\{\mathbf{x}_1,...,\mathbf{x}_m\}\in\mathbb{R}^d\\

Define the first PC of the samples by the linear projection wRd\mathbf{w}\in\mathbb{R}^d:

z={wTx1,...,wTxm}z=\{\mathbf{w}^T\mathbf{x}_1,...,\mathbf{w}^T\mathbf{x}_m\}

w\mathbf{w} is chosen such that var[z]var[z] is maximum.

var[z]=EX[(zzˉ)2]=1mi=1m(wTxiwTxˉ)2=1mi=1mwT(xixˉ)(xixˉ)Tw=wTSw\begin{aligned} var[z] &= EX[(z-\bar{z})^2]\\ &=\frac{1}{m}\sum_{i=1}^m(\mathbf{w}^T\mathbf{x}_i-\mathbf{w}^T\bar{\mathbf{x}})^2\\ &=\frac{1}{m}\sum_{i=1}^m\mathbf{w}^T(\mathbf{x}_i-\bar{\mathbf{x}})(\mathbf{x}_i-\bar{\mathbf{x}})^T\mathbf{w}\\ &=\mathbf{w}^TS\mathbf{w} \end{aligned}

其中,S=1mi=1m(xixˉ)(xixˉ)TS=\frac{1}{m}\sum_{i=1}^m(\mathbf{x}_i-\bar{\mathbf{x}})(\mathbf{x}_i-\bar{\mathbf{x}})^T 称之为协方差矩阵。

我们想要找到使得var[z]var[z] 最大的w\mathbf{w},并且wTw=1\mathbf{w}^T\mathbf{w}=1。故使用 Lagrange 方法(注意到SS 是对称的,所以ST+S=2SS^T+S=2S

L=wTSw+λ(1wTw)Lw=2(SλI)wL=\mathbf{w}^TS\mathbf{w}+\lambda(1-\mathbf{w}^T\mathbf{w})\\ \frac{\partial L}{\partial \mathbf{w}}=2(S-\lambda I)\mathbf{w}

所以就是在求解特征值λ\lambda。并且此时,L=wT(Swλw)+λ=λL=\mathbf{w}^T(S\mathbf{w}-\lambda\mathbf{w})+\lambda=\lambda。如果我们希望LL 尽量大,则需要选择尽量大的特征值λ\lambda。我们选择最大的特征值λ\lambda 和对应的特征向量作为w1\mathbf{w}_1 就得到了 1st PC。

注意,在求解 2st PC 时,我们还希望有w1Tw=0\mathbf{w}_1^T\mathbf{w}=0。故 Lagrange 方法要变为:

L=wTSw+λ(1wTw)+μ(w1Tw)Lw=2(SλI)w+μw1=0L=\mathbf{w}^TS\mathbf{w}+\lambda(1-\mathbf{w}^T\mathbf{w})+\mu(\mathbf{w}_1^T\mathbf{w})\\ \frac{\partial L}{\partial \mathbf{w}}=2(S-\lambda I)\mathbf{w}+\mu\mathbf{w}_1=0

我们把第二个式子左乘w1T\mathbf{w}_1^T,得到:

2w1TSw2λw1Tw+μw1Tw1=02\mathbf{w}_1^TS\mathbf{w}-2\lambda \mathbf{w}_1^T\mathbf{w}+\mu\mathbf{w}_1^T\mathbf{w}_1=0

其中,w1TSw=w1TλTw=0\mathbf{w}_1^TS\mathbf{w}=\mathbf{\mathbf{w}_1^T\lambda^T\mathbf{w}}=0, 2λw1Tw=0,w1Tw1=12\lambda\mathbf{w}_1^T\mathbf{w}=0,\mathbf{w}_1^T\mathbf{w}_1=1,所以有μ=0\mu=0

故此时和上面的情况一样,我们选择次大的特征值λ\lambda 和对应的特征向量作为w2\mathbf{w}_2 就得到了 2nd PC。以此类推


此外,要注意协方差矩阵的性质:

  1. 对称
  2. 半正定(所有主子式非负)
  3. cijciicjj=σiσj|c_{ij}|\leq\sqrt{c_{ii}c_{jj}}=\sigma_i\sigma_j

相关性系数

ρ(X,Y)=COV(X,Y)VAR[X]VAR[Y]\rho(X,Y)=\frac{COV(X,Y)}{\sqrt{VAR[X]VAR[Y]}}

  • it measures the strength and direction of the linear relationship between XX and YY.
  • If XX and YY have non-zero variance, then ρ(X,Y)[1,1]\rho(X, Y)\in[−1,1].
  • YY is a linearly increasing function of XX if and only if ρ(X,Y)=1\rho(X,Y)=1.
  • YY is a linearly decreasing function of XX if and only if ρ(X,Y)=1\rho(X,Y)=-1.
  • XX and YY are uncorrelated, if and only if ρ(X,Y)=0\rho(X,Y)=0.

# LDA (Linear Discriminant Analysis)

考虑一组数据:

{x1,...,xm}Rd\{\mathbf{x}_1,...,\mathbf{x}_m\}\in\mathbb{R}^d\\

其中,有n1n_1 个数据是 class 1,n2n_2 个数据是 class 2。然后定义一个类的 mean vector:

μi=1nijCixj\mu_i=\frac{1}{n_i}\sum_{j\in C_i}\mathbf{x}_j

  • 我们希望找到一个投影w\mathbf{w},使得类间距离最大,即:

max  J1(w)=(wTμ1wTμ2)2=wTSbwmax\;J_1(\mathbf{w})=(\mathbf{w}^T\mu_1-\mathbf{w}^T\mu_2)^2=\mathbf{w}^TS_b\mathbf{w}

其中,Sb=(μ1μ2)(μ1μ2)TS_b=(\mu_1-\mu_2)(\mu_1-\mu_2)^T 被称为类间散度矩阵

  • 同时,我们也希望类内的间距尽量小,即:

min  J2(w)=xC1(wTxwTμ1)2+xC2(wTxwTμ2)2=wTSwwmin\;J_2(\mathbf{w})=\sum_{\mathbf{x}\in C_1}(\mathbf{w}^T\mathbf{x}-\mathbf{w}^T\mu_1)^2+\sum_{\mathbf{x}\in C_2}(\mathbf{w}^T\mathbf{x}-\mathbf{w}^T\mu_2)^2\\ =\mathbf{w}^TS_w\mathbf{w}

其中,Sw=xC1(xμ1)(xμ1)T+xC2(xμ2)(xμ2)TS_w=\sum_{\mathbf{x}\in C_1}(\mathbf{x}-\mu_1)(\mathbf{x}-\mu_1)^T+\sum_{\mathbf{x}\in C_2}(\mathbf{x}-\mu_2)(\mathbf{x}-\mu_2)^T 被称为类内散度矩阵

  • 故最终为:

max  J1(w)J2(w){min  wTSbws.t.  wTSww=1max\;\frac{J_1(\mathbf{w})}{J_2(\mathbf{w})}\Rightarrow \begin{cases} min\;-\mathbf{w}^TS_b\mathbf{w}\\ s.t.\;\mathbf{w}^TS_w\mathbf{w}=1 \end{cases}

  • 求解使用 Lagrange 方法:

L=wTSbw+λ(wTSww1)Lw=2Sbw+2λSww=0(Sw1Sb)w=λwL=-\mathbf{w}^TS_b\mathbf{w}+\lambda(\mathbf{w}^TS_w\mathbf{w}-1)\\ \frac{\partial L}{\partial \mathbf{w}}= -2S_b\mathbf{w}+2\lambda S_w\mathbf{w}=0\Rightarrow (S_w^{-1}S_b)\mathbf{w}=\lambda\mathbf{w}

λ\lambdaSw1SbS_w^{-1}S_b 的特征值。此时目标函数为:

wTSbw=λwTSww=λ-\mathbf{w}^TS_b\mathbf{w}=-\lambda \mathbf{w}^TS_w\mathbf{w}=-\lambda

所以我们还是选择最大的特征值λ\lambda 和其对应的特征向量。


此外,还有另一种求解方法。因为我们不关心w\mathbf{w} 的大小,而只关心它的方向。那么就会有:

(Sw1Sb)w=λwSbw=(μ1μ2)(μ1μ2)Tw=λSww\begin{aligned} & \because (S_w^{-1}S_b)\mathbf{w}=\lambda\mathbf{w}\\ & \therefore S_b\mathbf{w}=(\mu_1-\mu_2)(\mu_1-\mu_2)^T\mathbf{w}=\lambda S_w\mathbf{w}\\ \end{aligned}

其中,(μ1μ2)Tw,λ(\mu_1-\mu_2)^T\mathbf{w},\lambda 都是数字,我们不关心。所以得到方向为:

w=Sw1(μ1μ2)\mathbf{w}=S_w^{-1}(\mu_1-\mu_2)


得出一个二分类器构造法:

  1. 计算μ1,μ2\mu_1,\mu_2
  2. 计算SwS_w
  3. 计算w=Sw1(μ1μ2)\mathbf{w}=S_w^{-1}(\mu_1-\mu_2)
  4. 设置阈值\gamma=\frac{n_1\mathbf{w}^T\mu_1+n_2\mathbf{w}^T\mu_2}

对于某数据x\mathbf{x},只需要比较wTx\mathbf{w}^T\mathbf{x}γ\gamma 的大小就可以完成分类了。

# Feature Selection

# Wrapper Methods

Sequential Forward Selection:
Start with the empty set S=S=\emptyset.

  • While stopping criteria not met:
  • For each feature XfX_f not in SS:
  • Define S=S{Xf}S'=S\cup\{X_f\}.
  • Train model using the features in SS'.
  • Compute the testing accuracy.
  • S=SS=S' where SS' is the feature set with the greatest accuracy.

# Filter Methods

replace evaluation of model with quick to compute statistics 𝐽(X_f)
譬如使用 Pierson 相关系数之类的统计量,直接对特征排序。

  • Score each feature XfX_f individually based on the 𝑓-th column of the data matrix and label vector YY.
    For each feature XfX_f
    Compute J(Xf)J(X_f)

  • Rank features according to J(Xf)J(X_f).

  • Choose the top 𝑘 features with the highest scores.

# Embedded methods

the classifier performs feature selection as part of the learning procedure.
Example: add the regularization term in the objective function

min  Xwy2+λwmin\;\|X\mathbf{w}-\mathbf{y}\|^2+\lambda\|\mathbf{w}\|

# Underfitting & overfitting

  • Underfitting occurs when the model is not able to obtain a sufficiently low error value on the training set.
  • Overfitting occurs when the gap between the training error and test error is too large.

# Occam’s Razor

  • Given two models of similar generalization errors, one should prefer the simpler model over the more complex model.
  • For complex models, there is a greater chance that it was fitted accidentally by errors in data.
  • Therefore, one should include model complexity when evaluating a model.

# 贝叶斯分类

# Sample correction

I(C=c)I(C=c) 表示数据集中,label 为cc 的样本数量。在 Bayers 分类中,我们用i=1NI(C=c)N\frac{\sum_{i=1}^NI(C=c)}{N} 来作为先验概率p(C=c)p(C=c) 的估算。而p(Fj=fj    C=c)=i=1NI(Fj=fj,C=c)i=1NI(C=c)p(F_j=f_j\;|\;C=c)=\frac{\sum_{i=1}^N I(F_j=f_j,C=c)}{\sum_{i=1}^N I(C=c)} 来做特征FjF_j 的条件概率。

这样做会有一些问题:

  • If a given class and feature value never occur together in the training set then the frequency-based probability estimate will be zero.
  • This is problematic since it will wipe out all information in the other probabilities when they are multiplied.

It is therefore often desirable to incorporate a small-sample correction in all probability estimates such that no probability is ever set to be exactly zero:

pλ(C=c)=i=1NI(C=c)+λN+Kλpλ(Fj=fj    C=c)=i=1NI(Fj=fj,C=c)+λi=1NI(C=c)+Sjλp_\lambda(C=c)=\frac{\sum_{i=1}^NI(C=c)+\lambda}{N+K\lambda}\\ p_\lambda(F_j=f_j\;|\;C=c)=\frac{\sum_{i=1}^N I(F_j=f_j,C=c)+\lambda}{\sum_{i=1}^N I(C=c)+S_j\lambda}

其中,KK 为所有的类别数量,SjS_j 为特征FjF_j 所有可能的取值。我们要求j=1Sjpλ(Fj=fj,C=c)=1\sum_{j=1}^{S_j}p_\lambda(F_j=f_j,C=c)=1λ=1\lambda=1 时,称为 Laplace smoothing。λ=0\lambda=0 时就是最大似然估计。

# SVM(Support Vector Machine)

对于一个样本点xi\textbf{x}_i,和一个超平面wTx+b=0\textbf{w}^T\textbf{x}+b=0,我们定义点到平面的距离为:

distance=wTxi+bw distance=\frac{|\textbf{w}^T\textbf{x}_i+b|}{||\textbf{w}||}

那么对于一个可线性二分类的数据集,我们不仅希望得到一个超平面划分它,还希望两边正负数据离超平面的 \textbf {最短距离尽量大}。
即我们希望:

{wTxi+bwm2yi=1wTxi+bwm2yi=1 \begin{cases} \frac{|\textbf{w}^T\textbf{x}_i+b|}{||\textbf{w}||}\geq\frac{m}{2} & y_i=1\\ \frac{|\textbf{w}^T\textbf{x}_i+b|}{||\textbf{w}||}\leq-\frac{m}{2} & y_i=-1 \end{cases}

其中,m2\frac{m}{2} 就是正,负类样本点距离超平面的最小距离:
11
然后处理下,我们可以记w=2wwm,b=2bwm\textbf{w}'=\frac{2\textbf{w}}{||\textbf{w}||m},b'=\frac{2b}{||\textbf{w}||m},则有:

{wTxi+b1yi=1wTxi+b1yi=1yi(wTxi+b)1 \begin{cases} \textbf{w}'^T\textbf{x}_i+b'\geq 1 & y_i=1\\ \textbf{w}'^T\textbf{x}_i+b'\leq-1 & y_i=-1 \end{cases}\\ \Leftrightarrow y_i(\textbf{w}'^T\textbf{x}_i+b')\geq 1

那些满足yi(wTxi+b)=1y_i(\textbf{w}'^T\textbf{x}_i+b')= 1 的样本点,即落在超平面两侧 margin 边缘上的样本点被称为 Support Vector
(注意到wTx+b=0\textbf{w}'^T\textbf{x}+b'=0wTx+b=0\textbf{w}^T\textbf{x}+b=0 是同一个平面,即分界超平面) 它们这些点到分界超平面的距离为:

distance=wTx+bw=wTx+bw=1wm=2w distance=\frac{|\textbf{w}^T\textbf{x}+b|}{||\textbf{w}||}=\frac{|\textbf{w}'^T\textbf{x}+b|}{||\textbf{w}'||}=\frac{1}{||\textbf{w}'||}\\ m=\frac{2}{||\textbf{w}'||}

所以我们得到描述 SVM 的规划问题:

{max  2/ws.t.  yi(wxi+b)1,i=1,2,...,m \begin{cases} max\;2/||\textbf{w}'||\\ s.t.\;y_i(\textbf{w}'\textbf{x}_i+b')\geq 1,i=1,2,...,m \end{cases}

然后为了方便解这个问题,我们稍微将其进行等价变化:

min  f(w)=12w2=12wTws.t.  1yi(wxi+b)0,i=1,2,...,m \begin{aligned} & min\;f(\textbf{w}')=\frac{1}{2}||\textbf{w}'||^2=\frac{1}{2}\textbf{w}'^T\textbf{w}\\ & s.t.\;1-y_i(\textbf{w}'\textbf{x}_i+b')\leq 0,i=1,2,...,m \end{aligned}

然后利用拉格朗日乘子构造拉格朗日函数:

L(w,b,α)=f(w)+i=1mαi[1yi(wxi+b)] \mathcal{L}(\textbf{w}',b',\overrightarrow{\alpha})=f(\textbf{w}')+\sum_{i=1}^m\alpha_i[1-y_i(\textbf{w}'\textbf{x}_i+b')]

所以有:

f(w)=maxαi0L(w,b,α) f(\textbf{w}')= \mathop{max}\limits_{\alpha_i\geq 0}\mathcal{L}(\textbf{w}',b',\overrightarrow{\alpha})

因为αi0\alpha_i\geq 0,然后它乘的[1yi(wxi+b)]0[1-y_i(\textbf{w}'\textbf{x}_i+b')]\leq 0,所以αi0\alpha_i\equiv 0 时取得最大值。
然后重点是:

minw,bf(w)=minw,b  maxαi0L(w,b,α)maxαi0  minw,bL(w,b,α) \mathop{min}\limits_{\textbf{w}',b'}f(\textbf{w}')=\mathop{min}\limits_{\textbf{w}',b'}\;\mathop{max}\limits_{\alpha_i\geq 0}\mathcal{L}(\textbf{w}',b',\overrightarrow{\alpha})\geq \mathop{max}\limits_{\alpha_i\geq 0}\;\mathop{min}\limits_{\textbf{w}',b'}\mathcal{L}(\textbf{w}',b',\overrightarrow{\alpha})

因为最小值的最大值永远小于等于最大值的最小值:

minw,bL(w,b,α)L(w,b,α)maxαi0L(w,b,α)maxαi0LHSminw,bRHS \begin{aligned} & \mathop{min}\limits_{\textbf{w}',b'}\mathcal{L}(\textbf{w}',b',\overrightarrow{\alpha})\leq\mathcal{L}(\textbf{w}',b',\overrightarrow{\alpha})\leq\mathop{max}\limits_{\alpha_i\geq 0}\mathcal{L}(\textbf{w}',b',\overrightarrow{\alpha})\\ \Rightarrow & \mathop{max}\limits_{\alpha_i\geq 0}LHS\leq \mathop{min}\limits_{\textbf{w}',b'}RHS \end{aligned}

根据规划问题的对偶性,如果原问题有最优解的话,上述等号可以取等。具体可参考 Karush-Kuhn-Tucker (KKT) 条件。
于是原问题转化为:

maxαi0  minw,bL(w,b,α)s.t.  α0 \begin{aligned} & \mathop{max}\limits_{\alpha_i\geq 0}\;\mathop{min}\limits_{\textbf{w}',b'}\mathcal{L}(\textbf{w}',b',\overrightarrow{\alpha})\\ & s.t.\;\alpha\geq 0 \end{aligned}

然后就可以进一步化简,有:

minw,bL(w,b,α){Lw=0w=i=1mαiyixiLb=00=i=1mαiyi \begin{aligned} & \mathop{min}\limits_{\textbf{w}',b'}\mathcal{L}(\textbf{w}',b',\overrightarrow{\alpha})\Rightarrow\\ & \begin{cases} \frac{\partial\mathcal{L}}{\textbf{w}'}=0\Rightarrow \textbf{w}'=\sum_{i=1}^m\alpha_iy_i\textbf{x}_i\\ \frac{\partial\mathcal{L}}{b'}=0\Rightarrow 0=\sum_{i=1}^m\alpha_iy_i\\ \end{cases} \end{aligned}

根据上述结论可得出:

L(i=1mαiyixi,b,α)=i=1mαi12i,j=1mαiαjyiyjxiTxj \mathcal{L}(\sum_{i=1}^m\alpha_iy_i\textbf{x}_i, b', \overrightarrow{\alpha})=\sum_{i=1}^m\alpha_i-\frac{1}{2}\sum_{i,j=1}^m\alpha_i\alpha_jy_iy_j\textbf{x}_i^T\textbf{x}_j

所以最终原规划问题等价为:

maxα  i=1mαi12i,j=1mαiαjyiyjxiTxjs.t.  α0,i=1mαiyi=0 \begin{aligned} & \mathop{max}\limits_{\alpha}\;\sum_{i=1}^m\alpha_i-\frac{1}{2}\sum_{i,j=1}^m\alpha_i\alpha_jy_iy_j\textbf{x}_i^T\textbf{x}_j\\ & s.t.\;\alpha\geq 0,\sum_{i=1}^m\alpha_iy_i=0\\ \end{aligned}

实际上,求解完成后,那些αi>0\alpha_i>0 对应的点就是 support vector。其余的点都有αi=0\alpha_i=0
求解完成后,可以得到:

w=i=1mαiyixiys[wxs+b]=1b=1yswxs \begin{aligned} & \textbf{w}'=\sum_{i=1}^m\alpha_iy_i\textbf{x}_i\\ & y_s[\textbf{w}'\textbf{x}_s+b']=1\Rightarrow b'=\frac{1}{y_s}-\textbf{w}'\textbf{x}_s\\ \end{aligned}

其中,(xs,ys)(\mathbf{x}_s,y_s) 是任意一个 support vector,即取一个αs>0\alpha_s>0 的点就好,就满足代入方程等于 1。
至此,我们便求出了下图所示三个超平面,图中的w,b\mathbf{w},\mathbf{b} 就是上述的w,b\mathbf{w}',\mathbf{b}'
12

# Soft margin SVM

考虑允许分类错误:
13

规划问题变为:

{minw12w2+Ci=1mζis.t.  yi(wTxi+b)1ζi,i=1,2,...,n\begin{cases} \mathop{min}\limits_{\mathbf{w}}\frac{1}{2}\|\mathbf{w}\|^2+C\sum_{i=1}^m\zeta_i\\ s.t.\; y_i(\mathbf{w}^T\mathbf{x}_i+b)\geq 1-\zeta_i\\,i=1,2,...,n \end{cases}

它的等价问题就是:

maxα  i=1mαi12i,j=1mαiαjyiyjxiTxjs.t.  Cα0,i=1mαiyi=0 \begin{aligned} & \mathop{max}\limits_{\alpha}\;\sum_{i=1}^m\alpha_i-\frac{1}{2}\sum_{i,j=1}^m\alpha_i\alpha_jy_iy_j\textbf{x}_i^T\textbf{x}_j\\ & s.t.\;C\geq\alpha\geq 0,\sum_{i=1}^m\alpha_iy_i=0\\ \end{aligned}

我们把 SV,即αi>0\alpha_i>0 的继续分类为 bound SV (αi=C\alpha_i=C) 和 unbound SV (α<C\alpha<C)。

对于 bound SV,有yi(wTx+b)=1ζiy_i(\mathbf{w}^T\mathbf{x}+b)=1-\zeta_i

  • 0ζi<10\leq\zeta_i<1 说明样本点ii 被正确分类。
  • ζi>1\zeta_i>1 说明样本点ii 被错误分类。
  • ζi=1\zeta_i=1 说明样本点ii 在边界上。

我们可以将 Soft margin SVM 的规划问题转化为(不再有约束)称为 Hinge Loss

minwi=1n[1yi(wTxi+b)]++λw2\mathop{min}\limits_{\mathbf{w}}\sum_{i=1}^n[1-y_i(\mathbf{w}^T\mathbf{x}_i+b)]_++\lambda\|\mathbf{w}\|^2

其中,[z]_+=\begin{cases}z&z>0\\0&z\leq 0\end

# Coordinate Ascent

就是固定其他分量不变,只变动一个分量,观察函数值上升 / 下降情况。

# SMO(Sequential Minimal Optimization)

考虑一个简单情况 SVM 的训练过程,譬如目标函数是关于α1,α2\alpha_1,\alpha_2 的二次函数,并且有一个约束是形如α1y1+α2y2=z\alpha_1y_1+\alpha_2y_2=z,并且α1,α2[0,C]\alpha_1,\alpha_2\in[0,C]

那么我们可以观察约束得知,α1,α2\alpha_1,\alpha_2 是在一个C×CC\times C 的区域内的一个线段上:
14

于是若要α1[0,C]\alpha_1\in[0,C],可以得到α2\alpha_2 也有个范围α2[L,H]\alpha_2\in[L,H]。我们此时用α1=(zα2y2)/y1\alpha_1=(z-\alpha_2y_2)/y_1 代入,求解目标函数,得到一个α2\alpha_2 的值,使得目标函数最小。

那么如果这个α2\alpha_2 的值在[L,H][L,H] 中,就采用它。否则,就裁剪α2\alpha_2LLHH

# Non Linear SVMP

一张图说明数据转换过程:
15

核函数的选择:

Theorem(Mercer). Let K:Rd×RdRK:\mathbb{R}^d\times\mathbb{R}^d\rightarrow\mathbb{R} be given. Then for KK to be a valid (Mercer) kernel, it is necessary and sufficient that for any {x1,x2,...,xn}\{\mathbf{x}_1,\mathbf{x}_2,...,\mathbf{x}_n\}, the corresponding kernel matrix is symmetric positive semi-definite.

  • Linear kernel K(xi,xj)=xiTxjK(\mathbf{x}_i,\mathbf{x}_j)=\mathbf{x}_i^T\mathbf{x}_j.
  • Polynomial kernel K(xi,xj)=(γxiTxj+c)dK(\mathbf{x}_i,\mathbf{x}_j)=(\gamma\mathbf{x}_i^T\mathbf{x}_j+c)^d.
  • Radial basis (Gussian) K(xi,xj)=exp(γxixj2)K(\mathbf{x}_i,\mathbf{x}_j)=\exp(-\gamma\|\mathbf{x}_i-\mathbf{x}_j\|^2).

# Shatter & VC Dimension

We say a classifier f(w,x)f(\mathbf{w},\mathbf{x}) can shatter points x1,...,xn\mathbf{x}_1,...,\mathbf{x}_n if and only if for all y1,...,yny_1,...,y_n, there exists w\mathbf{w}^*,f(w,x)f(\mathbf{w}^*,\mathbf{x}) can achieve zero error on the training data (x1,y1),...,(xn,yn)(\mathbf{x}_1,y_1),...,(\mathbf{x}_n,y_n).

The VC dimension is defined as the maximum number of points that can be arranged so that f(w,x)f(\mathbf{w},\mathbf{x}) can shatter them.

VC dimension measures the “power” of the learner
The higher the VC-dimension, the more flexible the classifier is.

# Decision Tree

Entroppy:

E(S)=i=1Cpilog2piE(S)=-\sum_{i=1}^Cp_ilog_2p_i

其中,CC 是类别数量,pip_i 是集合中label=ilabel=i 的占比。

Intrinsic Information:

IntI(S,A)=iSiSlogSiSIntI(S,A)=-\sum_i\frac{|S_i|}{|S|}log\frac{|S_i|}{|S|}

其中,SiS_i 是根据特征AA 划分的若干子集。

划分决策树的统计量:

  1. Information Gain (maximum):

Gain(S,A)=E(S)iSiSE(Si)Gain(S,A)=E(S)-\sum_i\frac{|S_i|}{|S|}E(S_i)

  1. Gain Ratio (maximum):

GR(S,A)=Gain(S,A)IntI(S,A)GR(S,A)=\frac{Gain(S,A)}{IntI(S,A)}

  1. Gini index (minimum):

Gini(S,A)=iSiSGini(Si)Gini(S)=1i=1Cpi2Gini(S,A)=\sum_i\frac{|S_i|}{|S|}Gini(S_i)\\ Gini(S)=1-\sum_{i=1}^Cp_i^2

# Missing values

Assume that attribute aa has VV possible values {a1,a2,...,aV}\{a_1,a_2,...,a_V\}.

  • S~\tilde{S}: subset of instances in SS whose values of aa are not missing.
  • S~i\tilde{S}_i: subset of instances in S~\tilde{S} whose value of aa is aia_i.
  • S~k\tilde{S}^k: subset of instances in S~\tilde{S} belonging to the k-th class.

Not we compute and ignore the missing values:

E(S~)=S~S(E(S~)iS~iS~E(S~i))E(S~)=kS~kS~log2S~kS~E(\tilde{S})=\frac{|\tilde{S}|}{|S|}(E(\tilde{S})-\sum_i\frac{|\tilde{S}_i|}{|\tilde{S}|}E(\tilde{S}_i))\\ E(\tilde{S})=-\sum_k\frac{|\tilde{S}^k|}{|\tilde{S}|}log_2\frac{|\tilde{S}^k|}{|\tilde{S}|}

# Avoid overfitting: Pruning

  • Pre-Pruning
    • Stop if all instances belong to the same class
    • Stop if all the attribute values are the same
  • Post-pruning
    • first grow a full tree to capture all possible attribute interactions
    • later trim the fully grown tree in a bottom-up fashion

# Reduced Error Pruning

Let TT be a subtree rooted at vv, we define Gain from pruning at vv = # misclassification in TT-# misclassification in vv:
16

We split the training set into two parts: growing set and pruning set. And build the tree on growing set, apply REP on pruning set, until error does not increase.

# Cost-complexity Pruning