Linear Classifier (CSCI-567)

2022-02-09
🗂️
AI

Classification:

  • N samples/instances: DTRAIN={(x⃗1,y1),(x⃗2,y2),...,(x⃗n,yn)}D^{TRAIN}=\{(\vec x_1, y_1),(\vec x_2, y_2),...,(\vec x_n, y_n)\}
  • Each x⃗n∈RD\vec x_n \in \mathbb{R}^D is called a feature vector
  • Each yn∈[C]={1,2,...,C}y_n\in[C]=\{1,2,...,C\} is called a label\class\catrgory
  • They are used for learning f:RD→[C]f:\mathbb{R}^D\rightarrow[C] for future prediction

This post is focus on binary classification.

  • Number of classes: C=2
  • Labels: {-1, +1}

Model: F={f(x⃗)=sign(w⃗Tx⃗)∣x⃗∈RD}\mathcal F=\{f(\vec x)=sign(\vec w^T\vec x)|\vec x\in\mathbb{R}^D\}, sign(w⃗Tx⃗)={+1 if w⃗Tx⃗>0−1 if w⃗Tx⃗≤0sign(\vec w^T\vec x)=\begin{cases} +1\ if\ \vec w^T\vec x>0\\\\ -1\ if\ \vec w^T\vec x\leq0\\ \end{cases}

Loss Function

z=ynw⃗Tx⃗z=y_n\vec w^T\vec x

  • perceptron loss:lperceptron(z)=max{0,−z}l_{\mathrm{perceptron}}(z)=max\{0, -z\}
  • hinge loss: lhinge(z)=max{0,1−z}l_\mathrm{hinge}(z)=max\{0,1-z\}
  • logistic loss: llogistic=log(1−exp(−z))l_\mathrm{logistic}=log(1-exp(-z))

Find ERM (Empirical Risk Minimization)

w⃗∗=argminw⃗∈RD∑n=1Nl(ynw⃗Tx⃗n)\vec w^\ast=\mathop{argmin}\limits_{\vec w\in\mathbb{R}^D}\sum\limits_{n=1}^Nl(y_n\vec w^T\vec x_n)

minimizing perceptron loss does not really make sense (try w⃗=0⃗\vec w=\vec 0), but the algorithm derived from this perspective does.

Gradient Descent (GD)

  • Gradient is sometimes referred to as first-order information of a function. Therefore, these methods are called first-order methods.

Goal: minimize F(w⃗)F(\vec w)

Algorithm: move a bit in the negative gradient direction. w⃗(t+1)←w⃗(t)−η∇F(w⃗(t))\vec w^{(t+1)}\leftarrow\vec w^{(t)}-\eta\nabla F(\vec w^{(t)}), where η>0\eta>0 is called step size or learning rate.

  • in theory η\eta should be set in terms of some parameters of FF
  • in practice we just try several small values

Essentially, GD is Taylor approximation: F(w⃗)≈F(w⃗(t))+∇F(w⃗(t))T(w⃗−w⃗(t))F(\vec w)\approx F(\vec w^{(t)})+\nabla F(\vec w^{(t)})^T(\vec w-\vec w^{(t)}), we use −η∇F(w⃗(t))-\eta\nabla F(\vec w^{(t)}) represents w⃗−w⃗(t)\vec w-\vec w^{(t)}, so F(w⃗(t+1))≈F(w⃗(t))−η∥∇F(w⃗(t))∥22≤F(w⃗(t))F(\vec w^{(t+1)})\approx F(\vec w^{(t)})-\eta\parallel\nabla F(\vec w^{(t)})\parallel_2^2\leq F(\vec w^{(t)})

Stochastic Gradient Descent (SGD)

w⃗(t+1)←w⃗(t)−η∇~F(w⃗(t))\vec w^{(t+1)}\leftarrow\vec w^{(t)}-\eta\tilde\nabla F(\vec w^{(t)}), where ∇~F(w⃗(t))\tilde\nabla F(\vec w^{(t)}) is a random variable (called stochastic gradient) E[∇~F(w⃗(t))]=∇F(w⃗(t))\mathbb{E}[\tilde\nabla F(\vec w^{(t)})]=\nabla F(\vec w^{(t)})

It could be much faster to obtain a stochastic gradient!

Convergence guarantees for both GD and SGD on convex objectives.

Applying GD to perceptron loss

F(w⃗)=∑n=1Nmax{0,−ynw⃗Tx⃗n}F(\vec w)=\sum\limits_{n=1}^Nmax\{0, -y_n\vec w^T\vec x_n\}

∇F(w⃗)=∑n=1N−I[ynw⃗Tx⃗n≤0]ynx⃗n\nabla F(\vec w)=\sum\limits_{n=1}^N-\mathbb{I}[y_n\vec w^T\vec x_n\leq0]y_n\vec x_n, only misclassified examples contribute to the gradient

GD update: w⃗←w⃗+η∑n=1NI[ynw⃗Tx⃗n≤0]ynx⃗n\vec w\leftarrow\vec w+\eta\sum\limits_{n=1}^N\mathbb{I}[y_n\vec w^T\vec x_n\leq0]y_n\vec x_n

Each update makes one pass of the entire training set

Applying SGD to perception loss

How to construct a stochastic gradient?

Pick one example n∈[N]n\in[N] uniformly at random, ∇~F(w⃗)=−NI[ynw⃗Tx⃗n≤0]ynx⃗n\tilde\nabla F(\vec w)=-N\mathbb{I}[y_n\vec w^T\vec x_n\leq0]y_n\vec x_n

SGD update: w⃗←w⃗+ηI[ynw⃗Tx⃗n≤0]ynx⃗n\vec w\leftarrow\vec w+\eta\mathbb{I}[y_n\vec w^T\vec x_n\leq0]y_n\vec x_n (η\eta absorbing the constant N) (each update touches only one data point!)

Perceptron Algorithm

Perceptron algorithm is SGD with η=1\eta=1 applied to perceptron loss:

Repeat:

  • Pick a data point x⃗n\vec x_n uniformly at random
  • If sgn(w⃗Tx⃗n)≠ynsgn(\vec w^T\vec x_n)\neq y_n, w⃗←w⃗+ynx⃗n\vec w\leftarrow\vec w+y_n\vec x_n

Note:

  • w⃗\vec w is always a linear combination of the training examples.
  • η=1\eta=1 does not really matter in terms of training error.

If training set is linearly separable

  • perceptron converges in a finite number of stpes
  • training error is 0

sigmoid function: σ(z)=11+e(−z)\sigma(z)=\frac{1}{1+e^{(-z)}}

  • σ(z)\sigma(z) is between 0 and 1
  • σ(w⃗Tx⃗)>=0.5⇔w⃗Tx⃗≥0\sigma(\vec w^T\vec x)>=0.5 \Leftrightarrow\vec w^T\vec x\geq0, consistent with predicting the label with sign(w⃗Tx⃗)sign(\vec w^T\vec x)
  • larger w⃗Tx⃗⇒\vec w^T\vec x\Rightarrow larger σ(w⃗Tx⃗)⇒\sigma(\vec w^T\vec x)\Rightarrow higher confidence in label 1
  • σ(z)+σ(−z)=1\sigma(z)+\sigma(-z)=1 for all zz