# Machine Learning

Machine Learning

## Loss functions

### (Mean) Squared Error

The squared error is:
${\displaystyle J(\theta )=\sum |h_{\theta }(x^{(i)})-y^{(i)}|^{2}}$
If our model is linear regression ${\displaystyle h(x)=w^{t}x}$ then this is convex.

Proof

{\displaystyle {\begin{aligned}\nabla _{w}J(w)&=\nabla _{w}\sum (w^{t}x^{(i)}-y^{(i)})^{2}\\&=2\sum (w^{t}x^{(i)}-y^{(i)})x\\\implies \nabla _{w}^{2}J(w)&=\nabla 2\sum (w^{T}x^{(i)}-y^{(i)})x^{(i)}\\&=2\sum x^{(i)}(x^{(i)})^{T}\end{aligned}}}
so the hessian is positive semi-definite

### Cross Entropy

The cross entropy loss is

• ${\displaystyle J(\theta )=\sum [(y^{(i)})\log(h_{\theta }(x))+(1-y^{(i)})\log(1-h_{\theta }(x))]}$
Notes
• If our model is ${\displaystyle g(\theta ^{T}x^{(i)})}$ where ${\displaystyle g(x)}$ is the sigmoid function ${\displaystyle {\frac {e^{x}}{1+e^{x}}}}$ then this is convex
Proof

We show that the Hessian is positive semi definite.
${\displaystyle \nabla _{\theta }J(\theta )=-\nabla _{\theta }\sum [(y^{(i)})\log(g(\theta ^{t}x^{(i)}))+(1-y^{(i)})\log(1-g(\theta ^{t}x^{(i)}))]}$
${\displaystyle =-\sum [(y^{(i)}){\frac {g(\theta ^{t}x^{(i)})(1-g(\theta ^{t}x^{(i)}))}{g(\theta ^{t}x^{(i)})}}x^{(i)}+(1-y^{(i)}){\frac {-g(\theta ^{t}x^{(i)})(1-g(\theta ^{t}x^{(i)}))}{1-g(\theta ^{t}x^{(i)})}}x^{(i)}]}$
${\displaystyle =-\sum [(y^{(i)})(1-g(\theta ^{t}x^{(i)}))x^{(i)}-(1-y^{(i)})g(\theta ^{t}x^{(i)})x^{(i)}]}$
${\displaystyle =-\sum [(y^{(i)})x^{(i)}-(y^{(i)})g(\theta ^{t}x^{(i)}))x^{(i)}-g(\theta ^{t}x^{(i)})x^{(i)}+y^{(i)}g(\theta ^{t}x^{(i)})x^{(i)}]}$
${\displaystyle =-\sum [(y^{(i)})x^{(i)}-g(\theta ^{t}x^{(i)})x^{(i)}]}$
${\displaystyle \implies \nabla _{\theta }^{2}J(\theta )=\nabla _{\theta }-\sum [(y^{(i)})x^{(i)}-g(\theta ^{t}x^{(i)})x^{(i)}]}$
${\displaystyle =\sum _{i}g(\theta ^{t}x^{(i)})(1-g(\theta ^{t}x^{(i)}))x^{(i)}(x^{(i)})^{T}}$
which is a PSD matrix

## Optimization

Also known as direction of steepest gradient.
To minimize a loss function, just take steps in the opposite direction of the gradient.

Oftentimes with large amounts of data, you can't take the gradient of all your data at once. In this case, we use batches of data.
We can take batches by getting a random order of indices without replacement.

for epoch=1 to n
generate batches
for i=1 to m

Batch Size

### Hessian

Some optimization methods may rely on the Hessian.

How to calculate ${\displaystyle Hv}$

Use finite differencing with directional derivatives:

• ${\displaystyle H_{\theta }\mathbf {v} =\lim _{\epsilon \rightarrow 0}{\frac {g(\theta +\epsilon \mathbf {v} )-g(\theta )}{\epsilon }}}$
How to calculate ${\displaystyle H_{\theta }^{-1}v}$?

Calculate H. Then use gradient descent to minimize ${\displaystyle {\frac {1}{2}}x^{T}Hx-v^{T}x}$.
By first order optimality, the minimum occurs when the gradient is zero.

• ${\displaystyle \nabla _{x}({\frac {1}{2}}x^{T}Hx-v^{T}x)=({\frac {1}{2}}H+{\frac {1}{2}}H^{T})x-v^{T}=Hx-v^{T}}$
• ${\displaystyle \implies x^{*}=H^{-1}v}$

## SVM

Andrew Ng Notes
Support Vector Machine
This is a linear classifier the same as a perceptron except the goal is not to just classify our data properly but to also maximize the margin.
${\displaystyle h_{w,b}(x)=g(W*x+b)}$ where ${\displaystyle g(x)=I[x>=0]-I[x<0]}$ is the sign function.

### Margins

The margin denoted by ${\displaystyle \gamma }$ is the distance between our classifier and the closest point.

Functional Margin

The margin corresponding to one example is:
${\displaystyle {\hat {\gamma }}^{(i)}=y^{(i)}(w^{T}x^{(i)}+b)}$ The margin for our entire sample is the smallest margin per example.

Geometric Margin

The geometric margin is the actual distance.
${\displaystyle {\hat {\gamma }}^{(i)}=y^{(i)}(({\frac {w}{\Vert w\Vert }})^{T}x^{(i)}+{\frac {b}{\Vert w\Vert }})}$

Notes
• ${\displaystyle \mathbf {w} }$ is the normal vector of our hyperplane so ${\displaystyle {\frac {w}{\Vert w\Vert }})^{T}x^{(i)}}$ is the length of the projection of x onto our normal vector.
This is the distance to our hyperplane.

### Lagrangians

The goal for svm is to maximize the margin:
{\displaystyle {\begin{aligned}\max _{{\hat {\gamma }},w,b}&{\frac {\hat {\gamma }}{\Vert w\Vert }}\\{\text{s.t. }}&y^{(i)}(w^{T}x^{(i)}+b)\geq {\hat {\gamma }}\quad \forall i\end{aligned}}}
which is equivalent to, by setting ${\displaystyle {\hat {\gamma }}=1}$,
{\displaystyle {\begin{aligned}\min _{\gamma ,w,b}&\Vert w\Vert ^{2}\\{\text{s.t. }}&y^{(i)}(w^{T}x^{(i)}+b)\geq 1\quad \forall i\end{aligned}}}

In general, given an optimization in the (primal) form:
{\displaystyle {\begin{aligned}\min _{w}&f(w)\\{\text{s.t. }}&h_{i}(w)\leq 0\quad \forall i\\&g_{i}(w)=0\end{aligned}}}
we can rewrite the optimization as
${\displaystyle \min _{w}\max _{\alpha ,\beta \mid \alpha \geq 0}{\mathcal {L}}(w,\alpha ,\beta )}$
where ${\displaystyle {\mathcal {L}}(w,\alpha ,\beta )=f(w)+\sum \alpha _{i}g_{i}(w)+\sum \beta _{i}h_{i}(w)}$ is called the lagrangian.
Since ${\displaystyle \max \min \leq \min \max }$,
we have:
${\displaystyle \max _{\alpha ,\beta \mid \alpha \geq 0}\min _{w}{\mathcal {L}}(w,\alpha ,\beta )\leq \min _{w}\max _{\alpha ,\beta \mid \alpha \geq 0}{\mathcal {L}}(w,\alpha ,\beta )}$
The left term is called the dual problem.
If the solution to the dual problem satisfy some conditions called the KKT conditions, then it is also the solution to the original problem.

### Kernel Trick

Oftentimes, using linear classifiers such as perceptron and SVM may fail to classify data for which the true decision boundary is non-linear.
In this case, one way to get around this is to perform a non-linear preprocessing of the data ${\displaystyle \phi (x)}$.
For example ${\displaystyle \phi (x)={\begin{bmatrix}x\\x^{2}\\x^{3}\end{bmatrix}}}$ If our original model and training only used ${\displaystyle \langle x,z\rangle }$ then we only need ${\displaystyle \phi (x)^{T}\phi (z)}$
A kernel ${\displaystyle K(x,z)}$ is a function that can be expressed as ${\displaystyle K(x,z)=\phi (x)^{T}\phi (z)}$ for some function ${\displaystyle \phi }$

#### Identifying if a function is a kernel

Basic check: Since the kernel is an inner-product between ${\displaystyle \phi (x),\phi (z)}$, it should satisfy the axioms of inner products, namely ${\displaystyle K(x,z)=K(z,x)}$, otherwise it is not a kernel.

#### Mercer's Theorem

Let our kernel function be ${\displaystyle K(z,x)}$. Then for any sample S, the corresponding matrix ${\displaystyle \mathbf {K} }$ where ${\displaystyle K_{ij}=K(x^{(i)},x^{(j)})}$ is symmetric positive definite.

Proof

Symmetry: ${\displaystyle K_{ij}=K(\mathbf {x} ^{(i)},\mathbf {x} ^{(j)}=\phi (\mathbf {x} ^{(i)})^{T}\phi (\mathbf {x} ^{(j)})=\phi (\mathbf {x} ^{(j)})^{T}\phi (\mathbf {x} ^{(i)})=K_{ji}}$
Positive Definite:
Let ${\displaystyle \mathbf {v} \in \mathbb {R} ^{n}}$.
Then
${\displaystyle \mathbf {v} ^{T}\mathbf {K} \mathbf {v} =\mathbf {v} ^{T}[\sum _{j}K_{ij}v_{j}]=\sum _{i}\sum _{j}v_{i}K_{ij}v_{j}}$
${\displaystyle =\sum _{i}\sum _{j}v_{i}\phi (\mathbf {x} ^{(i)})^{T}\phi (\mathbf {x} ^{(j)})v_{j}}$
${\displaystyle =\sum _{i}\sum _{j}v_{i}\sum _{k}\phi _{k}(\mathbf {x} ^{(i)})\phi _{k}(\mathbf {x} ^{(j)})v_{j}}$
${\displaystyle =\sum _{k}\sum _{i}\sum _{j}v_{i}\phi _{k}(\mathbf {x} ^{(i)})\phi _{k}(\mathbf {x} ^{(j)})v_{j}}$
${\displaystyle =\sum _{k}\sum _{i}v_{i}\phi _{k}(\mathbf {x} ^{(i)})\sum _{j}\phi _{k}(\mathbf {x} ^{(j)})v_{j}}$
${\displaystyle =\sum _{k}(\sum _{i}v_{i}\phi _{k}(\mathbf {x} ^{(i)}))^{2}\geq 0}$

#### Common Kernels

Polynomial Kernel
• See Wikipedia:Polynomial kernel
• ${\displaystyle K(x,z)=(c+x^{T}z)^{d}}$
• For ${\displaystyle d=2}$
• we have ${\displaystyle (1+x^{T}z)^{2}=1+2(x^{T}z)+(x^{T}z)^{2}}$
• ${\displaystyle =1+2\sum x_{i}z_{i}+(\sum x_{i}z_{i})(\sum x_{j}z_{j})}$
• ${\displaystyle =1+2\sum x_{i}z_{i}+2\sum _{i
• ${\displaystyle =1+\sum ({\sqrt {2}}x_{i})({\sqrt {2}}z_{i})+\sum _{i
• The dimension of the feature map associated with this kernel is exponential in d
• There are ${\displaystyle 1+n+{\binom {n}{2}}+...+{\binom {n}{d}}=O(n^{d})}$ terms

## Learning Theory

### PAC Learning

Probably Approximately Correct (PAC)
A hypothesis class ${\displaystyle H}$ is PAC learnable if given ${\displaystyle 0<\epsilon ,\delta <1}$, there is some function ${\displaystyle m(\epsilon ,\delta )}$ polynomial in ${\displaystyle 1/\epsilon ,1/\delta }$ such that if we have a sample size ${\displaystyle \geq m(\epsilon ,\delta )}$ then with probability ${\displaystyle 1-\delta }$ the hypothesis we will learn will have an average error ${\displaystyle \leq \epsilon }$.

### Uniform Convergence

If for all hypothesis ${\displaystyle h}$, ${\displaystyle |L_{S}(h)-L_{D}(h)|\leq \epsilon }$, then the training set ${\displaystyle S}$ is called ${\displaystyle \epsilon }$-representative.
Then if a training set is ${\displaystyle \epsilon /2}$-representative,
${\displaystyle L_{D}(h_{s})\leq L_{S}(h_{S})+\epsilon /2\leq L_{S}(h_{D})+\epsilon /2\leq L_{D}(h_{D})+\epsilon }$.
A hypothesis class ${\displaystyle H}$ has uniform convergence if there exists ${\displaystyle m^{UC}(\epsilon ,\delta )}$ such that for every ${\displaystyle \epsilon ,\delta }$, if we draw a sample ${\displaystyle S}$ then with probability ${\displaystyle 1-\delta }$, ${\displaystyle S}$ is ${\displaystyle \epsilon }$-representative.

### VC dimension

#### Shattering

A model ${\displaystyle f}$ parameterized by ${\displaystyle \theta }$ is said to shatter a set of points ${\displaystyle \{x_{1},...,x_{n}\}}$ if for every possible set of binary labellings ${\displaystyle \{y_{1},...,y_{n}\}}$ there exists ${\displaystyle \theta }$ such that ${\displaystyle f}$ makes no errors.

#### Definition

Intuitively, the VC dimension of a hypothesis set is how complex of a model it is.
Stolen from wikipedia:
The VC dimension of a model ${\displaystyle f}$ is the maximum number of points that can be arranged so that ${\displaystyle f}$ shatters them. More formally, it is the maximum cardinal ${\displaystyle D}$ such that some data point set of cardinality ${\displaystyle D}$ can be shattered by ${\displaystyle f}$.

Notes
• To show VCdim is at least n, find a training set of size n that can be shattered by our hypothesis class.
• To show VCdim is leq n, prove no training set of size n+1 can be shattered by our hypothesis class.
• Number of parameters do not necessarily correspond to VC dimension.
${\displaystyle H=\{h(x)=\sin(\theta x)\}}$ has infinite VC dimension with one parameter

#### Theory

Reference
In the case where the Hypothesis class ${\displaystyle {\mathcal {H}}}$ is finite, we have with probability ${\displaystyle 1-\delta }$

• ${\displaystyle |L_{D}(h)-L_{S}(h)|<{\sqrt {\frac {\log |{\mathcal {H}}|+\log(1/\delta )}{2m}}}}$
where ${\displaystyle m}$ is the size of the sample.

For all h in H,

• ${\displaystyle |L_{D}(h)-L_{S}(h)|
for some constants ${\displaystyle K_{1},K_{2}}$

#### Growth Function

The growth function is maximum number of ways ${\displaystyle m}$ examples can be labelled using hypotheses from ${\displaystyle {\mathcal {H}}}$

• ${\displaystyle \tau _{H}(m)=\max _{|C|=m}|H_{C}|}$
Notes
• If ${\displaystyle m\leq VCdim(H)}$, then ${\displaystyle \tau _{H}(m)=2^{m}}$

#### Sauer's Lemma

Reference
After the VCdim, the growth function grows as a polynomial

• If ${\displaystyle VCdim(H)\leq d\leq \infty }$ then ${\displaystyle \tau _{H}(m)\leq \sum _{i=0}^{d}{\binom {n}{i}}}$
• Also if ${\displaystyle m>d+1}$ then ${\displaystyle \tau _{H}(m)\leq \left({\frac {em}{d}}\right)^{d}}$.

• Let ${\displaystyle L_{D}(h)}$ be the true loss of hypothesis h
and ${\displaystyle L_{S}(h)}$ be the empirical loss of hypothesis h
• ${\displaystyle L_{D}(h_{s}^{*})=L_{D}(h_{D}^{*})+[L_{D}(h_{s}^{*})-L_{D}(h_{D}^{*})]}$
• The term ${\displaystyle L_{D}(h_{D}^{*})}$ is called the bias
• The term ${\displaystyle [L_{D}(h_{s}^{*})-L_{D}(h_{D}^{*})]}$ is called variance.
• Larger hypothesis class will get smaller bias but larger variance.
• Overfitting vs. underfitting

#### Definition

The Rademacher complexity, like the VC dimension, measures how "rich" the hypothesis space is.
In this case, we see how well we can fit random noise.
Given a set ${\displaystyle A\subset \mathbb {R} ^{m}}$ the Rademacher complexity is:
${\displaystyle R(A)={\frac {1}{m}}E_{\sigma }[\sup _{a\in A}\sum _{i=1}^{m}\sigma _{i}a_{i}]}$
where each ${\displaystyle \sigma _{i}}$ are from a discrete uniform distribution ${\displaystyle \{-1,1\}}$
Given a sample ${\displaystyle S=\{z_{1},...,z_{n}\}}$ and a function class ${\displaystyle F}$, the empirical rademacher complexity is:
${\displaystyle R(F\circ S)}$
where ${\displaystyle F\circ S=\{(f(z_{1}),...,f(z_{n}))\mid f\in F\}}$

Notes

### Concentration Bounds

#### Hoeffding's inequality

Let ${\displaystyle X_{1},...,X_{n}}$ be bounded in (a,b)
Then ${\displaystyle P(|{\bar {X}}-E[{\bar {X}}]|\geq t)\leq 2\exp(-{\frac {2nt^{2}}{(b-a)^{2}}})}$