Deep Learning Notes from Zero to Hero

Comprehensive notes from the Machine Learning Research Lab course @ Universitas Indonesia [In Progress]

21 min readEryawan Presma Yulianrifat

Introduction to Deep Learning

1. Fundamental Definition of Machine Learning

Tom Mitchell (1997) provided a concise definition of machine learning:

A computer program is said to learn from experience E with respect to some task T and performance measure P, if its performance at task T, as measured by P, improves with experience E.

Illustrative Examples:

Major Categories of Machine Learning Tasks:

Deep Learning is a specialized subfield of machine learning that employs neural networks with multiple layers to solve complex tasks, particularly excelling in supervised learning scenarios.

2. Supervised Learning: Core Concepts

Supervised learning involves learning a function f: X → Y from labeled pairs (x, y).

Primary Types:

Classification:

Regression:

Real-World Applications:

3. Unsupervised Learning: Discovering Hidden Patterns

The primary goal is to learn the underlying distribution of input data without explicit labels.

Self-Supervised Learning:

A powerful approach that constructs pseudo-labels from the data itself, such as predicting the next word in a sentence.

Notable Examples:

4. Curve Fitting: A Fundamental Regression Example

Task Definition:

Predict target value t from scalar input x (e.g., sin(2πx) + noise)

Model Formulation:

Polynomial representation: y(x, w) = ∑ wⱼ x^ⱼ

Error Measurement:

Squared Loss Function: E(w) = ½ ∑ (y(xₙ, w) − tₙ)²

Model Complexity Considerations:

Detecting Overfitting:

Strategies for Controlling Overfitting:

Model Selection Process:

Probabilistic Framework

Motivation

ML models face uncertainty. Application of machine learning implies dealing with uncertainty. No machine learning model can achieve a perfect accuracy

Probability Theory

Probability Theory. Handling uncertainty requires the framework of probability theory

Frequentist view of probability. Probability is the proportion of the frequency of an event happening in the limit of an infinite number of trials.

Bayesian View of Probability. Probability is the quantification of uncertainty.

Joint Probability P(X,Y)P(X, Y). Probability of X=xX=x AND Y=yY=y denoted as P(X=x,Y=y)=pX,Y(x,y)P(X=x, Y=y)=p_{X,Y}(x, y)

Conditional probability P(XY)P(X\mid Y). Probability of X=xX=x given Y=yY=y denoted as P(X=xY=y)=pXY(xy)P(X=x\mid Y=y)=p_{X\mid Y}(x\mid y)

Marginal probability P(X)P(X) Probability of X=xX=x obtained via sum rule, denoted as P(X=x)=pX(x)=ypX,Y(x,y)P(X=x)=p_X(x)=\sum_{y}p_{X,Y}(x, y) for discrete case or pX,Y(x,y)dy\int_{-\infty}^\infty p_{X,Y}(x, y)dy in continuous case.

Product rule P(X=x,Y=y)P(X=x, Y=y). P(X=x,Y=y)=p(Y=yX=x)P(X=x)P(X=x, Y=y)=p(Y=y\mid X=x)P(X=x).

Probability Distribution. A probability distribution for a random variable X is “complete specification of a way to compute probabilities of events about X.” It specify probability mass function (pmf) for discrete case and probability density function (pdf) for continuous case. Typically, pmf or pdf also depends on a set of parameters that governs the shape of pmf/pdf. Keep in mind that pmf/pdf must satisfy the requirement that they are nonnegative and normalized (their sum/integrate to 1 over all their support set). If a distribution is not normalized, it is called improper.

Bayes Theorem. The core principle in bayesian statistics, how do we measure belief after seeing some data (which is learning in some sense).

P(YX)=P(XY)P(Y)P(X)P(Y\mid X) = \frac{P(X\mid Y)P(Y)}{P(X)}
TermNameIntuition
P(YX)P(Y\mid X)PosteriorWhat we want to know: belief about Y after seeing data X
P(XY)P(X\mid Y)LikelihoodHow likely is data X under hypothesis Y?
P(Y)P(Y)PriorBelief about Y before seeing any data
P(X)P(X)Evidence (aka Marginal Likelihood)How likely is the data X overall, across all possible values of Y?

For those who confuse likelihood and probability, here are genius explanation from my bro ChatGPT.

Likelihood and probability look similar but are conceptually different depending on the context. Whether we call P(XY)P(X\mid Y) the likelihood depends on what is fixed and what is variable.

TermVariableFixedInterpretation
Probability P(XY)P(X\mid Y)XXYYGiven model/parameter YY, how likely is the data XX?
Likelihood L(Y)=P(XY)L(Y)=P(X\mid Y)YYXXGiven observed data XX, how likely is a particular model/parameter YY?

Independent Event.

P(X,Y)=P(X)P(Y)    P(YX)=P(Y)P(X,Y) = P(X)P(Y)\quad\iff\quad P(Y\mid X) = P(Y)

If the posterior probability equal to prior probability (Additional information does not change the prior probability), then XX and YY are independent.

Expectation. Expectation of some function f(x)f(x) is the weighted average of ff under a probability distribution p(x)p(x).

Ep(x)[f(x)]=xf(x)p(x)orf(x)p(x)dx\mathbb E_{p(x)}[f(x)] = \sum_x f(x)p(x) \quad\text{or}\quad \int_{-\infty}^\infty f(x)p(x)dx

Ep(x)[f(x,y)]\mathbb E_{p(x)}[f(x, y)] indicates expectation of a function over several variables, and the subscript p(x)p(x) denotes the distribution under which the expected value of the function is computed. In this case, Ep(x)[f(x,y)]\mathbb E_{p(x)}[f(x, y)] becomes a function of yy.

Conditional Expectation. Conditional expectation is defined w.r.t. a conditional distribution p(xy)p(x\mid y).

Ep(xy)[f(x)]=xp(xy)f(x)orp(xy)f(x)dx\mathbb E_{p(x|y)}[f(x)] = \sum_x p(x\mid y)f(x)\quad\text{or}\quad \int_{-\infty}^\infty p(x\mid y)f(x)dx

Variance. Variance of f(x)f(x) measures how much the value of ff varies over all possible values of xx under a certain p(x)p(x).

Varp(x)[f(x)]=Ep(x)[(f(x)Ep(x)[f(x)])2]Varp(x)=Ep(x)[f(x)2]E[f(x)]2\begin{align*} Var_{p(x)}[f(x)] &= \mathbb E_{p(x)}[(f(x)-\mathbb E_{p(x)}[f(x)])^2] \\ Var_{p(x)} &= \mathbb E_{p(x)}[f(x)^2] - \mathbb E[f(x)]^2 \end{align*}

Covariance. measure the extent to which the two variables vary together.

Covp(x,y)[f(x),g(y)]=Ep(x,y)[(f(x)Ep(x,y)[f(x)])(g(y)Ep(x,y)[g(y)]))Covp(x,y)=Ep(x,y)[f(x)g(y)]Ep(x,y)[f(x)]Ep(x,y)[g(y)]\begin{align*} Cov_{p(x, y)}[f(x), g(y)] &= \mathbb E_{p(x, y)}[(f(x) - \mathbb E_{p(x, y)}[f(x)])(g(y) - \mathbb E_{p(x, y)}[g(y)])) \\ Cov_{p(x, y)} &= \mathbb E_{p(x, y)}[f(x)g(y)] - \mathbb E_{p(x, y)}[f(x)]\mathbb E_{p(x, y)}[g(y)] \end{align*}

If x and y are two vectors of random variables, their covariance is a matrix

Covp(x,y)[x,y]=Ep(x,y)[xyT]Ep(x)[x]Ep(y)[yT]Cov_{p(x, y)}[\vec x, \vec y] = \mathbb E_{p(x, y)}[xy^T] - \mathbb E_{p(x)}[x]\mathbb E_{p(y)}[y^T]

Note that Cov(x,x)=Var(x)Cov(x, x) = Var(x)

Example distributions

  1. Uniform
  2. Exponential
  3. Laplace
  4. Gaussian

Likelihood

Probability vs Likelihood. Probability is the chance that a particular outcome occurs based on the values of the distribution parameters. When calculating probability, we assume that the parameters are trustworthy. Likelihood is how well a sample/data provides support for particular values of the distribution parameter. When calculating likelihood, we’re trying to determine if we can trust the parameter values based on the data that we observe.

L(θX=x)=Pθ(x)=P(X=x;θ)=fθ(x) for continuous pdf fθ(x)L(\theta\mid X=x) = P_\theta(x) = P(X=x; \theta) = f_\theta(x)\text{ for continuous pdf }f_\theta(x)

In continuous case, we use pdf ff which interpreted as density where in discrete case we use pmf pp which interpreted as true probability. The integral of the likelihood over all parameter values may not be equal to 1.

Computing Likelihood. Given dataset D=x1,,xN\mathcal D={x_1,\dots, x_N} that is i.i.d (independent and identically distributed), the likelihood is computed as

L(θD)=i=1NP(xi;θ)=i=1Nfθ(xi)L(\theta\mid \mathcal D) = \prod_{i=1}^N P(x_i;\theta) = \prod_{i=1}^N f_\theta(x_i)

Product/multiplication of large number of small numbers can be problematic. So we often work with log-likelihood.

LL(θ)=logL(θD)=i=1NlogP(xi;θ)LL(\theta) = \log L(\theta\mid \mathcal D) = \sum_{i=1}^N \log P(x_i;\theta)

Density estimation aka Parameter Estimation. Task of estimating the distribution, given a finite set of observations. Ill-posed because there are infinitely many distributions that could have generate the observed finite dataset.

One solution (among others) for density estimation is maximizing likelihood of the parameters with respect to the given dataset. Parameters obtained by likelihood maximization are usually called maximum likelihood estimators.

Information Theory

Shannon’s idea: relate information content to degree of surprise. It view that Surprise     \iff Rarity and Rarity     \iff Contains more information.

Measuring information. Suppose, for a discrete RV XX, we observe that X=xX = x for a particular xx, how much information do we receive? Let h(x)h(x) be the measure of information.

  1. h(x) should be additive. If two unrelated facts x, y occur at the same time, the surprise seeing both of them should be the sum of the individual surprises. So,
h(x,y)=h(x)+h(y)h(x, y) = h(x) + h(y)

But, x and y are unrelated, hence also independent. So, p(x,y)=p(x)p(y)p(x, y) = p(x)p(y) and h(x)h(x) should be logarithm of p(x)p(x).

  1. h(x)h(x) should be monotonic in the opposite direction to p(x)p(x). h(x)h(x) increases iff p(x)p(x) decreases.

We obtain,

h(x)=logbp(x)h(x) = -\log_b p(x)

Entropy: expected amount of information. Entropy H[x] of x: expected amount of information (i.e., amount of surprise) over all possible outcomes of x under distribution of x, which is p(x):

H[x]=Ep(x)[h(x)]=xp(x)h(x)=xp(x)logp(x)\mathbb H[x] = \mathbb E_{p(x)}[h(x)] = \sum_x p(x)h(x) = -\sum_x p(x)\log p(x)

Entropy can also be understood as a measure of uncertainty of a system.

Minimum and Maximum Entropy Problem. The minimum of H[p] is H[p] = 0 that happens when there exists some xix_i such that p(xi)=1p(x_i) = 1 while p(xj)=0p(x_j) = 0 for the other xjx_j‘s. Its maximum however, is harder and more useful, choosing the most unbiased distribution given what you know.

Solving Maximum Entropy Problem using Langragian Contrained Optimization.

We aim to solve for p(xi)p(x_i) (discrete case) that maximize following objective

L(x,λ)=f(x)+λg(x)H[x]=xip(xi)logp(xi)+λ(ip(xi)1)\begin{align*} L(x, \lambda) &= f(x) + \lambda g(x) \\ \mathbb H[x] &= -\sum_{x_i} p(x_i)\log p(x_i) + \lambda\left(\sum_i p(x_i) - 1 \right) \\ \end{align*}

See that contrained g(x)g(x) is to kept them under probability axiom (sum to 1). In the end, we would have something like p(x1)=p(x2)==p(xn)=exp(λ1)=1/Kp(x_1)=p(x_2)=\dots=p(x_n)=\exp(\lambda-1)=1/K. In other words, H[p] is maximum when p(x) is a uniform (discrete) distribution.

Entropy for continuous variable. We can extend entropy to continous variables as differential.

H[x]=limΔ0(ip(xi)Δlogp(xi))=xp(x)logp(x)dx\mathbb H[x] = \lim_{\Delta\to 0}\left(-\sum_i p(x_i)\Delta\log p(x_i) \right) = -\int_x p(x)\log p(x)dx

One can show using calculus of variations and Lagrange function that the distribution that maximizes the differential entropy is the Gaussian.

Cross Entropy. Cross-entropy measures the difference between two probability distributions: the true distribution p(x)p(x) and the predicted distribution q(x)q(x). It tells you how many “nats” or “bits” you need to encode data from p(x)p(x) using the coding scheme designed for q(x)q(x).

Hp(q)=H(q,p)=xq(x)logp(x)Hp(p)=H[p]\begin{align*} \mathbb H_p(q) &= \mathbb H(q, p) = -\sum_x q(x)\log p(x) \\ \mathbb H_p(p) &= \mathbb H[p] \end{align*}

Note that we use parentheses for cross entropy instead of square brackets.

Expression above is the same as saying how many bits (or nats) you need to encode data from q(x)q(x) using the coding scheme designed for p(x)p(x). In machine learning, the message corresponds to the true (unknown) distribution of the prediction outcome, while the code corresponds to the approximating distribution of the prediction obtained by the model.

Kullback-Leibler (KL) Divergence. The average additional amount of information (in nats) needed to specify the value xx (using the approximating distribution q(x)q(x)), is given by:

KLq(pq)=Hq(p)Hp[x]=xp(x)logp(x)q(x)\mathbb {KL}_q(p\mid\mid q) = \mathbb H_q(p) - \mathbb H_p[x] = \sum_x p(x)\log\frac{p(x)}{q(x)}

KL divergence can be interpreted as a measure of dissimilarity between two distributions p(x)p(x) and q(x)q(x).

KL Divergence and Likelihood. If training data comes from the unknown distribution p(x)p(x), we approximate p(x)p(x) using some parametric distribution q(xθ)q(x|\theta). θ\theta can be determined by minimizing KL(pq)\mathbb {KL}(p||q) but p(x)p(x) is unknown. Since we have training data {xi}\{x_i\} drawn from p(x)p(x), we can estimate as a finite sum over these points.

KL(pq)=p(x)logq(x;θ)p(x)dx=1Ni=1N(logq(xi;θ)p(xi))=1Ni=1N(logq(xn;θ)+logp(xi))\begin{align*} \mathbb{KL}(p||q) &= -\int p(x)\log\frac{q(x; \theta)}{p(x)} dx \\ &= \frac{1}{N}\sum_{i=1}^N\left(-\log\frac{q(x_i;\theta)}{p(x_i)}\right) = \frac{1}{N}\sum_{i=1}^N(-\log q(x_n;\theta) + \log p(x_i)) \end{align*}

log p(xi)p(x_i) is independent of θ\theta and q(xθ)q(x_|\theta) is negative log-likelihood. Therefore minimizing KL(pq)\mathbb{KL}(p||q) is equivalent to maximizing log-likelihood θ\theta of qq. In deep learning, KL-divergence is usually used in variational autoencoders, generative adversarial networks, and diffusion models.

Linear Regression

Task. Predict continuous target t from input xRDx\in R^D

Model.

y(x,w)=j=0M1wjϕj(x)=wTϕ(x)y(x, w) = \sum_{j=0}^{M-1} w_j\phi_j(x) = w^T\phi(x)

Basis Function. Linear in weights, but not necessarily in inputs.

Probabilistic Modeling. Assume noise in targets tt such that

t=y(x,w)+ϵeN(0,σ2)t = y(x, w) + \epsilon\quad e\sim\mathcal N(0, \sigma^2) \\

Where

y(x,w)=wTϕ(x)y(x, w) = w^T\phi(x)

Probabilistically, we can write

p(tx,w,σ2)=N(ty(x,w),σ2)p(t\mid x, w, \sigma^2) = \mathcal N(t\mid y(x, w), \sigma^2)

Given a dataset of inputs X={x1,,xN}X=\{x_1,\dots, x_N\} with corresponding targets t=(t1,,tN)Tt=(t_1,\dots,t_N)^T where data points are i.i.d. The likelihood of parameters w,σ2w, \sigma^2 defined as

L(w,σ2X,t)=p(tX,w,σ2)=i=1NN(tiwTϕ(xi),σ2)LL(w,σ2X,t)=i=1NN(tiwTϕ(xi),σ2)LL(w,σ2X,t)=Nlog1σ2π12i=1N(tiwTϕ(xi)σ)2LL(w,σ2X,t)=N2log(2πσ2)1σ2(12i=1N(tiwTϕ(xi))2Sum of Squares Error)\begin{align*} L(w, \sigma^2\mid X, t)&=p(t\mid X,w,\sigma^2)=\prod_{i=1}^N\mathcal N(t_i\mid w^T\phi(x_i), \sigma^2) \\ LL(w, \sigma^2\mid X, t)&= \sum_{i=1}^N N(t_i\mid w^T\phi(x_i), \sigma^2) \\ LL(w, \sigma^2\mid X, t)&= N\log \frac{1}{\sigma\sqrt{2\pi}} -\frac{1}{2}\sum_{i=1}^N \left(\frac{t_i-w^T\phi(x_i)}{\sigma}\right)^2 \\ LL(w, \sigma^2\mid X, t)&= -\frac{N}{2}\log (2\pi\sigma^2) - \frac{1}{\sigma^2}\left(\underbrace{\frac{1}{2}\sum_{i=1}^N ({t_i-w^T\phi(x_i)})^2}_\text{Sum of Squares Error}\right) \\ \end{align*}

if σ2\sigma^2 is assumed to be fixed/constant, maximizing the log-likelihood above is equivalent to minimzing the sum-of-squares error.

The maximization of the log-likelihood in previous equation is done with respect to two parameters: ww and σ2\sigma^2, leading to solutions for both defined as:

wMLE=argmaxwlogp(tX,w,σ2)σMLE2=argmaxσ2logp(tX,w,σ2)w_{MLE} = \arg\max_w\log p(t\mid X, w, \sigma^2)\quad \sigma^2_{MLE}=\arg\max_{\sigma^2} \log p(t\mid X, w, \sigma^2)

Both are called maximum likelihood estimators for the model. For homoskedastic linear regression model, σ2\sigma^2 is actually assumed to be constant, leading to wMLEw_{MLE} as a least-square solution to the sum-of-squares error.

Design Matrix. Is a matrix XRN×DX\in R^{N\times D} for number of data NN and feature dimension DD. The basis function ϕ:RNRM\phi: R^N\to R^M is applied to each row of XX to yield a (row) vector of length MM. y=[yi]i\vec y=[y_i]_i is also commonly used together with Design Matrix.

Maximize log likelihood. We will try to solve this using gradient in a closed form style, make it equal to 0. We will solve it in design matrix form Φ\Phi \in .

w[N2log(2πσ2)1σ2(12i=1N(tiwTϕ(xi))2)]w[N2log(2πσ2)1σ2(12(tΦw)T(tΦw))]=12σ2w(tΦw)T(tΦw)=12σ2[(Φ)T(tΦw)]T(tΦw)T(Φ)=12σ2(tΦw)T(Φ)(tΦw)T(Φ)=1σ2(tΦw)TΦ=0=(tTwTΦT)Φ=tTΦwtΦTΦ=0    ΦTΦw=ΦTt    w=(ΦTΦ)1ΦTt\begin{align*} &\frac{\partial}{\partial w}\left[- \frac{N}{2}\log (2\pi\sigma^2) - \frac{1}{\sigma^2}\left(\frac{1}{2}\sum_{i=1}^N ({t_i-w^T\phi(x_i)})^2\right)\right] \\ &\frac{\partial}{\partial w}\left[- \frac{N}{2}\log (2\pi\sigma^2) - \frac{1}{\sigma^2}\left(\frac{1}{2} (\vec t-\Phi w)^T(\vec t-\Phi w)\right)\right] \\ &= -\frac{1}{2\sigma^2}\frac{\partial}{\partial w}(\vec t-\Phi w)^T(\vec t-\Phi w) \\ &= -\frac{1}{2\sigma^2}[(-\Phi)^T(\vec t-\Phi w)]^T\cdot(\vec t-\Phi w)^T(-\Phi) \\ &= -\frac{1}{2\sigma^2}(\vec t-\Phi w)^T(-\Phi)\cdot(\vec t-\Phi w)^T(-\Phi) \\ &= \frac{1}{\sigma^2}(\vec t-\Phi w)^T\Phi = 0 \\ &= (\vec t^T-w^T\Phi^T)\Phi = t^T\Phi - w^t\Phi^T\Phi =0 \\ &\iff \Phi^T\Phi w = \Phi^T t \iff w = (\Phi^T\Phi)^{-1}\Phi^T t \end{align*}

This is normal equation: wmle=(ΦTΦ)1ΦTtw_{mle} = (\Phi^T\Phi)^{-1}\Phi^T t. Can lead to numerical difficulties when ΦTΦ\Phi^T\Phi is close to singular. Can be addressed using SVD or sequential or online stochastic gradient descent algorithms.

Regularized Least Square Solving for Overfitting Problem. We change the objective to minimize for

ED(w)+λEW(w)=12i(tiwTϕ(xi))2+λ2wTw\begin{align*} &E_D(w) + \lambda E_W(w) \\ &=\frac{1}{2}\sum_i (t_i-w^T\phi(x_i))^2 + \frac{\lambda}{2}w^Tw \\ \end{align*}

We use 2\ell_2 regularizer for EwE_w. This method also called as weight decay allowing our models to turning off some of the parameters ww.

Multiple Regression. Conditional distribution of the KK-dimensional target vector t\vec t.

p(tx,W,σ2)=N(tWTϕ(x),σ2I)=1(2π)Kσ2Iexp(12(tWTϕ(x))T(σI)1(tWTϕ(x)))\begin{align*} p(\vec t\mid x, W, \sigma^2) &= \mathcal N(\vec t\mid W^T\phi(x), \sigma^2 I) \\ &= \frac{1}{\sqrt{(2\pi)^K\vert\sigma^2 I\vert}}\exp\left(-\frac{1}{2}(\vec t - W^T\phi(x))^T(\sigma I)^{-1}(\vec t - W^T\phi(x))\right) \end{align*}

The above Gaussian has:

The Gaussian is assumed isotropic: equal variance in each dimension (thus equal density in every direction) and all dimensions are independence. So, the shape will be spherical and aligned at all axes.

Decision Theory

Approximating Distribution to Decision Probablistic model p(tx,wML,σML2)=N(ty(x,wML),σML2)p(t | x, w_{ML}, \sigma^2_{ML}) = N (t | y(x, w_{ML}), \sigma^2_{ML}) is a predictive distribution expressing uncertanity over value of t for some new input x. But often we need to predict specific value for t, rather than returning the distribution, especially if we must take a specific action. Example, If our aim is to determine the optimal level of radiation to use for treating tumor and model predicts a probability distribution over radiation dose, then we need to use that distribution to determine the specific dose to be administered. We explain this formally using the notion of decision theory.

Formal Objective. Suppose we choose f(x)f(x) for our prediction when the true value is tt. Then, this choice incur a penalty/cost given by a loss function L(t,f(x))L(t, f(x)). We define the objective

E[l]=L(t,f(x))dxdt\mathbb E[l] = \int\int L(t, f(x))\,dx\,dt

Example of L(t,f(x))L(t, f(x)) is squared loss.

L(t,f(x))=(f(x)t)2L(t, f(x)) = (f(x)-t)^2

Choosing decision function f(x). Minimizing above expected loss using squared loss, using calculus of variations, we obtain.

f(x)=tp(tx)dt=Et[tx]f^*(x) = \int tp(t\mid x)\,dt=\mathbb E_t[t\mid x]

f(x)=Et[tx]f^*(x) = \mathbb E_t[t\mid x] is called the regression function.

Which is just conditional expectation of tt given xx. For Gaussian conditional distribution, this becomes the following.

E[tx]=tp(tx)dt=y(x,w)\mathbb E[t\mid x] = \int tp(t\mid x)\,dt = y(x, w)

Bias Variance Tradeoff

Derivation of Bias Variance Tradeoff.

E[L]=(f(x)E[tx])2p(x)dx+var(tx)p(x)dx\mathbb E[L] = \int (f(x)-\mathbb E[t\mid x])^2p(x) dx + \int var(t\mid x)p(x)dx

Note that term E[tx]\mathbb E[t\mid x] is just one choice of the regression function. Second term is independent of f(x)f(x) arises from intrinsic noise of the data – represents minimum value achievable for expected loss.

Consider the first integrand, done over a particular dataset DD: (f(x;D)h(x))2(f(x; D) - h(x))2. We do experiments over the ensemble of datasets, so we can take the average of the above over many possible datasets. Consider we model E[tx]=h(x)\mathbb E[t\mid x]=h(x).

There will be 3 terms with the last term will vanish. The first term is the squared bias of the model. Second term is variance of the

We will ended up with expected loss= bias 2+ variance + irreducible noiseloss =\text{ bias }^2 +\text{ variance }+\text{ irreducible noise}.

Classification

Task. Predicting a class label y{1,,K}y\in\{1,\dots, K\} from input xRDx\in R^D.

Three approaches to solve classification task.

  1. Discriminant function: directly assigns xx to a specific class.
  2. Discriminative probabilistic model: train the conditional distribution p(Ckx)p(C_k | x) directly using a training set, and then use the model to decide the class of xx.
  3. Generative probabilistic model: model the class-conditional density p(xCk)p(x | C_k), and the class prior p(Ck)p(C_k), then compute the posterior p(Ckx)p(C_k | x) via Bayes theorem; after that, use the posterior distribution to decide the class of xx.

Maximum Likelihood on Classification Task. Likelihood is defined the same regardless of regression or classification. All that matters is our estimator definition. Suppose we model classification task as below.

p(y=cx;w)=fc(x;w)p(y = c\mid x; w) = f_c(x; w)

where fc(x;w)f_c(x; w) is a parametric function. Likelihood is in the form of.

L(wX,y)=i=1Np(yi=cixi;w)LL(wX,y)=i=1Nlogp(yi=cixi;w)\begin{align*} L(w\mid X, y) = \prod_{i=1}^N p(y_i = c_i\mid x_i; w) \\ LL(w\mid X, y) = \sum_{i=1}^N \log p(y_i = c_i\mid x_i; w) \end{align*}

Linear discriminant function: Two-class case. Suppose we model

y(x)=wTx+w0y(x) = w^Tx + w_0

The decision to assign xx into class y^\hat y is

y^={C1if y(x)0C2otherwise\hat y = \begin{cases} C_1\quad\text{if } y(x) \ge 0\\ C_2\quad\text{otherwise} \end{cases}

Decision boundary corresponds to the y(x)=0y(x)=0, D1D-1 dimensional hyperplane within DD dimensional input space.

Decision boundary is orthogonal to w. Under the same model above, we could prove that for any vector (x0x1)(x_0-x_1) such that y(x0)=y(x1)=0y(x_0) = y(x_1) = 0 (they are on the same side of the decision boundary), they are orthogonal with ww.

x0,x1:y(x0)=y(x1)=0wTx0=wTx1    wT(x0x1)=0w,x0x1=0\begin{align*} x_0, x_1 : y(x_0) = y(x_1) = 0 \\ w^Tx_0 = w^Tx_1 \implies w^T(x_0 - x_1) = 0 \\ \langle w, x_0-x_1\rangle = 0 \end{align*}

where ,\langle \cdot, \cdot \rangle is the standard dot product. This property holds true for any two points x0,x1x_0, x_1 on the decision boundary. This implies that the distance between any xx and the decision boundary is rww,rRr\frac{w}{\Vert w\Vert}, r\in R.

Multiple classes. The trick above (Defining decision boundary yc(x)>0y_c(x)>0) will draw a single line for single class cc.

Using Least Square to find WW. For one hot vector tiRKt_i\in R^K with prediction wTϕ(xi)RKw^T\phi(x_i)\in R^K, we stack into the matrix form T=[ti]iRN×K,X=[ϕ(xi)]iRN×D,W=[wiT]iRD×KT=[t_i]_i\in R^{N\times K}, X=[\phi(x_i)]_i\in R^{N\times D}, W=[w^T_i]_i \in R^{D\times K} (essentially just stacking vertically).

ED(w)=12iNwTϕ(xi)ti2ED(w)=12iNjK(wTϕ(xi)jtij)2ED(w)=12iNjKXWTF2ijED(w)=12XWTF2ED(w)=12Tr((XWT)T(XWT))\begin{align*} E_D(w) &= \frac{1}{2}\sum_i^N \Vert w^T\phi(x_i) - t_i \Vert^2\\ E_D(w) &= \frac{1}{2}\sum_i^N \sum_j^K (w^T\phi(x_i)_j - t_{ij})^2\\ E_D(w) &= \frac{1}{2}\sum_i^N \sum_j^K {\Vert XW-T \Vert^2_F}_{ij}\\ E_D(w) &= \frac{1}{2}{\Vert XW-T \Vert^2_F} \\ E_D(w) &= \frac{1}{2} \operatorname{Tr}\left((XW-T)^T(XW-T)\right) \\ \end{align*}

Decision Theory

Prediction and Action. Prediction in supervised learning is modeled probabilistically as a posterior distribution p(yx)p(y\mid x). Intuitively, our follow up action for prediction is to choose the value of yy that makes p(yx)p(y\mid x) the highest. But is this mathematically justified?

Mathematical Justification. We assume an agent / decision maker has set of possible action aAa\in\mathcal A. Each action aa has costs and benefits that depend on the state (hidden) of nature yy. These dependencies can be modeled as a loss function (y,a)\ell(y, a) that gives the loss incurred if the agent takes action aa when the state of nature is yy.

Posterior Expected Risk. We define the posterior expected risk as

R(ax)=Ep(yx)[(y,a)]=yYp(yx)(y,a)R(a\mid x) = \mathbb E_{p(y\mid x)}[\ell(y, a)] = \sum_{y\in\mathcal Y} p(y\mid x)\ell(y, a)

Optimal Policy (Bayes Estimator). The optimal policy is the one that minimizes the posterior expected risk.

a=argminaAR(ax)a^* = \arg\min_{a\in\mathcal A} R(a\mid x)

Alternative: Use utility function, Maximum Expected Utility Principle. Define U(y,a)U(y, a) expressing desirability of each possible action and state then seek action that maximize expected utility.

a=argmaxaAEp(yx)[U(y,a)]a^* = \arg\max_{a\in\mathcal A} \mathbb E_{p(y\mid x)}[U(y, a)]

Decision on Classification, Zero-one loss for Binary Classification. Assign loss 1 if action is wrong, 0 otherwise.

zo(y,a)=I{ya}\ell_{zo}(y, a) = \mathbb I\{y\ne a\}
aay=0y=0y=1y=1
a=0a=001
a=1a=110

Posterior expected loss for each action aa is

R(ax)=yYp(yx)zo(y,a)=p(y0x)When a=1+p(y1x)When a=0=p(yax)=1p(y=ax)\begin{align*} R(a\mid x) &= \sum_{y\in\mathcal Y} p(y\mid x)\ell_{zo}(y, a) \\ &= \underbrace{p(y_0\mid x)}_{\text{When }a=1} + \underbrace{p(y_1\mid x)}_{\text{When }a=0} \\ &= p(y\neq a\mid x) = 1-p(y=a\mid x) \end{align*}

Optimal policy is

a=argmina{0,1}R(ax)=argmina{0,1}1p(y=ax)=argmaxa{0,1}p(y=ax)\begin{align*} a^* &= \arg\min_{a\in\{0,1\}} R(a\mid x) \\ &= \arg\min_{a\in\{0,1\}} 1-p(y=a\mid x) \\ &= \arg\max_{a\in\{0,1\}} p(y=a\mid x) \end{align*}

Maximum a posteriori (MAP) estimate. This maximization decision rule is corresponds to the mode of the posterior distribution. Specifically for binary case:

a={0if p(y=0x)p(y=1x)1otherwisea^* = \begin{cases} 0&\text{if }p(y=0\mid x)\ge p(y=1\mid x) \\ 1&\text{otherwise} \end{cases}

Cost Sensitive Classification. Instead of zero-one loss, we may have the following cost matrix where ij\ell_{ij} is the cost of predicting jj when true label is ii.

Predicted labela = 0a = 1
True label
y = 000\ell_{00}01\ell_{01}
y = 110\ell_{10}11\ell_{11}
R(ax)=yYp(yx)yaR(ax)=00p(y=0x)+01p(y=0x)+10p(y=1x)+11p(y=1x)R(a=0x)=00p(y=0x)+10p(y=1x)R(a=1x)=01p(y=0x)+11p(y=1x)\begin{align*} R(a\mid x) &= \sum_{y\in\mathcal Y} p(y\mid x)\ell_{y a} \\ R(a\mid x) &= \ell_{00}p(y=0\mid x) + \ell_{01}p(y=0\mid x) + \ell_{10}p(y=1\mid x) + \ell_{11}p(y=1\mid x) \\ R(a = 0\mid x) &= \ell_{00}p(y=0\mid x) + \ell_{10}p(y=1\mid x) \\ R(a = 1\mid x) &= \ell_{01}p(y=0\mid x) + \ell_{11}p(y=1\mid x) \end{align*}

Decision rule is to pick aa that minimizes the posterior expected loss:

a=argmina{0,1}R(ax)=min{00p(y=0x)+10p(y=1x)a=0,  01p(y=0x)+11p(y=1x)a=1}\begin{align*} a^* &= \arg\min_{a\in\{0,1\}} R(a\mid x) \\ &= \min\left\{\underbrace{\ell_{00}p(y=0\mid x) + \ell_{10}p(y=1\mid x)}_{a=0},\;\underbrace{\ell_{01}p(y=0\mid x) + \ell_{11}p(y=1\mid x)}_{a=1}\right\}\\ \end{align*}

Which turn into this decision rule

00p(y=0x)+10p(y=1x)01p(y=0x)+11p(y=1x)(0001)p(y=0x)+(1011)p(y=1x)0(0001)p(y=0x)(1110)p(y=1x)p(y=0x)p(y=1x)11100001a={0if p(y=0x)p(y=1x)111000011otherwise\begin{align*} \ell_{00}p(y=0\mid x) + \ell_{10}p(y=1\mid x) &\dots \ell_{01}p(y=0\mid x) + \ell_{11}p(y=1\mid x) \\ (\ell_{00}-\ell_{01})p(y=0\mid x) + (\ell_{10}-\ell_{11})p(y=1\mid x) &\dots 0 \\ (\ell_{00}-\ell_{01})p(y=0\mid x) &\dots (\ell_{11}-\ell_{10})p(y=1\mid x) \\ \frac{p(y=0\mid x)}{p(y=1\mid x)} &\dots \frac{\ell_{11}-\ell_{10}}{\ell_{00}-\ell_{01}} \\ a = \begin{cases} 0&\text{if }\frac{p(y=0\mid x)}{p(y=1\mid x)}\ge\frac{\ell_{11}-\ell_{10}}{\ell_{00}-\ell_{01}} \\ 1&\text{otherwise} \end{cases} \end{align*}

Reject Option Classification. Sometimes we want to say “I don’t know” instead of returning an answer (called the reject option). The loss will then become

l(y,a)={0if a=y^λrif a=rejectλeotherwise\ell l(y, a) = \begin{cases} 0&\text{if }a=\hat y\\ \lambda_r&\text{if }a=\text{reject}\\ \lambda_e&\text{otherwise} \end{cases}

The optimal decision rule that minimizes posterior risk is (prove as exervise)

a={argmaxy{1,,C}p(yx)if maxy{1,,C}p(yx)>λ=1λrλerejectotherwisea^* = \begin{cases} \arg\max\limits_{y\in\{1,\dots,C\}} p(y|x) & \text{if } \max\limits_{y\in\{1,\dots,C\}} p(y|x) > \lambda = 1 - \frac{\lambda_r}{\lambda_e} \\ \text{reject} & \text{otherwise} \end{cases}

Evaluation Matrix

Confusion Matrix. We can define the number of true positives (TP), false positive (FP), true negatives (TN), and false negatives (FN) with respect to the threshold τ\tau:

TP=i=1NI{yi=1,y^i=1}TN=i=1NI{yi=0,y^i=0}FP=i=1NI{yi=0,y^i=1}FN=i=1NI{yi=1,y^i=0}\begin{align*} TP &= \sum_{i=1}^N \mathbb I\{y_i = 1, \hat y_i = 1\} \quad TN &= \sum_{i=1}^N \mathbb I\{y_i = 0, \hat y_i = 0\} \\ FP &= \sum_{i=1}^N \mathbb I\{y_i = 0, \hat y_i = 1\} \quad FN &= \sum_{i=1}^N \mathbb I\{y_i = 1, \hat y_i = 0\} \end{align*}

Thus we can define the following confusion matrix:

Prediction01Row sum
Truth
0TNFPNN
1FNTPPP
Col sumN^\hat NP^\hat PN+PN + P

Metric from Confusion Matrix.

Accuracy=TP+TNN=TP+TNTP+TN+FP+FN\begin{align*} \text{Accuracy} &= \frac{TP + TN}{N} = \frac{TP + TN}{TP + TN + FP + FN} \\ \end{align*}

Metrics w.r.t. rowwise normalization (i.e., truths).

TNRτ=TNτNτ=TNτFPτ+TNτ=p(y^=0y=0;τ)\quad TNR_\tau = \frac{TN_\tau}{N_\tau} = \frac{TN_\tau}{FP_\tau + TN_\tau} = p(\hat y = 0|y = 0; \tau) FPRτ=FPτNτ=FPτFPτ+TNτ=p(y^=1y=0;τ)FPR_\tau = \frac{FP_\tau}{N_\tau} = \frac{FP_\tau}{FP_\tau + TN_\tau} = p(\hat y = 1|y = 0; \tau) FNRτ=FNτPτ=FNτTPτ+FNτ=p(y^=0y=1;τ)FNR_\tau = \frac{FN_\tau}{P_\tau} = \frac{FN_\tau}{TP_\tau + FN_\tau} = p(\hat y = 0|y = 1; \tau) TPRτ=TPτPτ=TPτTPτ+FNτ=p(y^=1y=1;τ)TPR_\tau = \frac{TP_\tau}{P_\tau} = \frac{TP_\tau}{TP_\tau + FN_\tau} = p(\hat y = 1|y = 1; \tau)

Metrics w.r.t. columnwise normalization (i.e., predictions).

NPVτ=TNτN^τ=TNτTNτ+FNτ=p(y=0y^=0;τ)NPV_\tau = \frac{TN_\tau}{\hat N_\tau} = \frac{TN_\tau}{TN_\tau + FN_\tau} = p(y = 0|\hat y = 0; \tau) FORτ=FNτN^τ=FNτTNτ+FNτ=p(y=1y^=0;τ)FOR_\tau = \frac{FN_\tau}{\hat N_\tau} = \frac{FN_\tau}{TN_\tau + FN_\tau} = p(y = 1|\hat y = 0; \tau) FDRτ=FPτP^τ=FPτFPτ+TPτ=p(y=0y^=1;τ)FDR_\tau = \frac{FP_\tau}{\hat P_\tau} = \frac{FP_\tau}{FP_\tau + TP_\tau} = p(y = 0|\hat y = 1; \tau) Precτ=PPVτ=TPτP^τ=TPτFPτ+TPτ=p(y=1y^=1;τ)Prec_\tau = PPV_\tau = \frac{TP_\tau}{\hat P_\tau} = \frac{TP_\tau}{FP_\tau + TP_\tau} = p(y = 1|\hat y = 1; \tau)

Choosing from a number of different thresholds. With ROC curves, we can consider a set of different thresholds and compare the resulting performance. For each threshold τ\tau, we plot FPRτFPR_\tau versus TPRτTPR_\tau as a point and draw a curve connecting them. This gives the receiver operating characteristic (ROC) curve.

ROC curve is insensitive to changes in class distribution. Changes in the proportion of positive to negative instances in the (test) set do not cause change to the curve.

Precision-Recall (PR) curve.

F-scores. FβF_\beta score is a combined statistics between precision (Prec) and recall (Rec).Useful for looking at a single threshold. F-score is affected by class distribution changes.

Classification Modeling

Three approaches for training and inference.

  1. Generative Models. Train class conditional densities p(xCk)p(x\mid C_k) for each class CkC_k individually. Then use Bayes theorem to compute the posterior probabilities:
p(Ckx)=p(xCk)p(Ck)p(x)=p(xCk)p(Ck)i=1Cp(xCi)p(Ci)p(C_k\mid x) = \frac{p(x\mid C_k)p(C_k)}{p(x)} = \frac{p(x\mid C_k)p(C_k)}{\sum_{i=1}^C p(x\mid C_i)p(C_i)}

“Generative” because by sampling from them, it is possible to generate synthetic data points in the input space.

  1. Deterministic Models. Train posterior class probabilities p(Ckx)p(C_k\mid x). Then, use decision theory to perform inference to assign each new xx to one of the classes.

  2. Discriminative Models. Find a discriminant function f(x)f(x) that maps each input xx directly onto a class label, e.g., for binary classification ff might be binary valued such that f=0f = 0 represents class C1C_1 and f=1f = 1 represents class C2C_2.

Why compute posterior instead of discriminant function directly?.

Discriminative Classifiers, Logistic Regression. Intuitively, we classify

f(x)={1if wTx+b>00otherwise=I(logp(y=1x)p(y=0x)>0)f(x) = \begin{cases} 1&\text{if }w^Tx + b > 0 \\ 0&\text{otherwise} \end{cases} = \mathbb I\left(\log \frac{p(y=1\mid x)}{p(y=0\mid x)}>0\right)

We assume the data is linearly separable using above equation. In another form, we could formulate f(x)f(x) as a log-odds.

wTx+b=logp(y=1x)p(y=0x)Log Odds    p(y=1x)=11+exp(wTx+b)\begin{align*} & w^Tx + b = \log\underbrace{\frac{p(y=1\mid x)}{p(y=0\mid x)}}_{\text{Log Odds}} \\ &\iff p(y=1\mid x) = \frac{1}{1+\exp(-w^Tx+b)} \end{align*}

The aforementioned model is called (binary) logistic regression

p(y=1x)=σ(wTx+b)p(y=1\mid x) = \sigma(w^Tx+b)

MLE for Logistic Regression.

NLL(w)=1Nlogp(Dw)=1Nlogn=1NBer(ynσ(an))=1Nn=1Nlog[σ(an)yn(1σ(an))1yn]=1Nn=1N[ynlogσ(an)+(1yn)log(1σ(an))]=1Nn=1NH(yn,σ(an))wNLL(w)=1Ni=1N(σ(ai)yi)xi\begin{align*} NLL(w) &= -\frac{1}{N} \log p(D|w) = -\frac{1}{N} \log \prod_{n=1}^N Ber(y_n|\sigma(a_n)) \\ &= -\frac{1}{N} \sum_{n=1}^N \log[\sigma(a_n)^{y_n} \cdot (1-\sigma(a_n))^{1-y_n}] \\ &= -\frac{1}{N} \sum_{n=1}^N [y_n \log \sigma(a_n) + (1-y_n) \log(1-\sigma(a_n))] \\ &= \frac{1}{N} \sum_{n=1}^N H(y_n,\sigma(a_n)) \\ \nabla_w NLL(w) &= \frac{1}{N} \sum_{i=1}^N (\sigma(a_i)-y_i)x_i \\ \end{align*}

Cons that occurs in linearly separable datasets:

Multinomial Softmax. We generalize to multiple class C={1,2,3,}C=\{1, 2, 3, \dots\} based on the following idea

fc(x;wc)=I(p(y=cx)>p(y=1x))f_c(x; w_c) = \mathbb I(p(y=c\mid x)>p(y=1\mid x))

Long story short, this implies

p(y=kx)=exp(wkTx)i=1Cexp(wiTx)p(y=k\mid x) = \frac{\exp(w_k^Tx)}{\sum_{i=1}^C \exp(w_i^Tx)}

Note that for every class kk, it has its own weight wkw_k. The value of p(y=cx)p(y = c\mid x) is invariant under translation in by the same value in each dimension.

S(Wx)=[ew1Txi=1CewiTx,,ewCTxi=1CewiTx]T\mathcal S(Wx) = \left[\frac{e^{w_1^Tx}}{\sum_{i=1}^C e^{w_i^Tx}}, \dots, \frac{e^{w_C^Tx}}{\sum_{i=1}^C e^{w_i^Tx}}\right]^T

Since S(Wx)\mathcal S(Wx) is a vector of C probability values, softmax regression is based on a categorical distribution M whose parameters are precisely those C probability values:

p(yx;θ)=M(yθ),θ=S(Wx)p(y\mid x; \theta) = \mathcal M(y\mid \theta), \theta=\mathcal S(Wx)

Log Sum Trick. When working with softmax distribution, we often want to compute the normalized probability. Define ai=wiTxa_i=w_i^Tx

p(y=cx)=exp(ac)i=1Cexp(ai)=exp(ac)Z(a)p(y=c\mid x) = \frac{\exp(a_c)}{\sum_{i=1}^C \exp(a_i)} = \frac{\exp(a_c)}{Z(a)}

where Z(a)Z(a) is the sum defined the same across all cc.

Its computation may not be numerically stable.

Lets derive a better numerical version.

p(y=cx)=exp(ac)i=1Cexp(ai)=exp(ac)explogi=1Cexp(ai)=exp(aclogi=1Cexp(ai))=exp(aclog(exp(m)i=1Cexp(aim)))=exp(acm+logi=1Cexp(aim))\begin{align*} p(y=c\mid x) &= \frac{\exp(a_c)}{\sum_{i=1}^C \exp(a_i)} = \frac{\exp(a_c)}{ \exp \log \sum_{i=1}^C \exp(a_i)} \\ &= \exp\left(a_c - \log \sum_{i=1}^C \exp(a_i)\right) \\ &= \exp\left(a_c - \log (\exp(m)\sum_{i=1}^C \exp(a_i-m))\right) \\ &= \exp\left(a_c - m + \log \sum_{i=1}^C \exp(a_i-m)\right) \end{align*}

We choose m=maxi(ai)m = \max_i(a_i) so that the largest exponential will be zero. Even if there’s underflow, the answer is still sensible.

Deep Neural Networks

Motivation

Motivation. When one uses linear models, one strongly assumes that the input-output mapping is linear. Model can be made more flexible by performing non linear feature transformation. But hand-crafting such transformation is very restrictive: curse of dimensionality. Real data does not live in all regions of the input space.

Data-dependent basis functions.

Hierarchical Non Linearity.

Multilayer Perceptions Formulation.

zj()=h(aj())=h(i=1n1wij()zi(1)+bj())=h(W()z(1))z_j^{(\ell)}= h(a_j^{(\ell)}) = h\left(\sum_{i=1}^{n_{\ell-1}} w_{ij}^{(\ell)} z_i^{(\ell-1)} + b_j^{(\ell)}\right) = h^\ell(W^{(\ell)}z^{(\ell-1)})

DNN as universal approximator, no free lunch theorem, and inductive bias

Heteroskedastic and Homoskedastic non linear regression.

Representation, Transfer, Multitask, and Meta Learning.

Contrastive Learning. Learn representation such that positive input pairs are close and negative input pairs are far part in embedding space. So error function is defined wrt inputs. Example is infoNCE loss.

E(w)=logexp(fw(x)Tfw(x+))exp(fw(x)Tfw(x+)+i=1Nexp(fw(xi)Tfw(xi)))E(w) = -\log\frac{\exp(f_w(x)^Tf_w(x^+))}{\exp(f_w(x)^Tf_w(x^+)+\sum_{i=1}^N \exp(f_w(x_i)^Tf_w(x_i^-)))}

Vector Calculus

Univeriate, Multivariate, and Vector Functions.

f:RDRKf: \mathbb R^D\to\mathbb R^K

Example definition of multivariate real-valued function of f(x)=xTx,x=(x1,x2)f(\mathbf x) = \mathbf x^T\mathbf x, \mathbf x=(x_1, x_2) specifically specified as

f:R2Rxx12+x22\begin{align*} f: \mathbb R^2\to\mathbb R \\ \mathbf x\to x_1^2+x_2^2 \end{align*}

Different Quotient and Derivative in Univariate Real-Valued Function For a univariate real-valued function f(x)f(x), the difference quotient is given by

δfδx=f(x+δx)f(x)δx\frac{\delta f}{\delta x} = \frac{f(x+\delta x) - f(x)}{\delta x}

while the derivative is given by

dfdx=limδx0f(x+δx)f(x)δx\frac{df}{dx} = \lim_{\delta x\to 0} \frac{f(x+\delta x) - f(x)}{\delta x}

If the limit exists at then ff is differentiable at xx, and the derivative is the tangent of ff at xx. The derivative of ff at points in the direction of the steepest ascent of ff.

Partial Derivatives in Multivariate Real-Valued Function For a multivariate real-valued function f(x)f(\mathbf x), the partial derivative with respect to xix_i is given by

f:RnRfx1=limδ0f(x1+δ,,xD)f(x)δfxi=limδ0f(x1,,xi+δ,,xD)f(x)δ\begin{align*} &f: \mathbb R^n\to \mathbb R \\ &\frac{\partial f}{\partial x_1} = \lim_{\delta \to 0} \frac{f(x_1+\delta, \dots, x_D) - f(x)}{\delta} \\ &\frac{\partial f}{\partial x_i} = \lim_{\delta \to 0} \frac{f(x_1, \dots, x_i+\delta, \dots, x_D) - f(x)}{\delta} \\ \end{align*}

The gradient (or the Jacobian) of ff is the vector collecting all partial derivatives in the row vector.

xf=dfdx=[fx1,fx2,,fxD]\nabla_{\bf x} f = \frac{df}{d\bf x} = \left[\frac{\partial f}{\partial x_1}, \frac{\partial f}{\partial x_2}, \dots, \frac{\partial f}{\partial x_D}\right]

Basic rules of partial differentiation

x(f(x)+g(x))=fx+gxx(f(x)g(x))=fxg(x)+f(x)gxx(gf)(x)=x(g(f(x)))=gffx\begin{align*} \frac{\partial}{\partial\mathbf x} (f(x)+g(x)) &= \frac{\partial f}{\partial\mathbf x} + \frac{\partial g}{\partial\mathbf x} \\ \frac{\partial}{\partial\mathbf x} (f(x)g(x)) &= \frac{\partial f}{\partial\mathbf x}g(x) + f(x)\frac{\partial g}{\partial\mathbf x} \\ \frac{\partial}{\partial\mathbf x}(g\circ f)(\mathbf x) = \frac{\partial}{\partial\mathbf x}(g(f(\mathbf x))) &= \frac{\partial g}{\partial f}\frac{\partial f}{\partial\mathbf x} \end{align*}

Compact writing of chain rule as matrix multiplication only makes sense if the gradient is defined as a row vector. Otherwise, we will need to introduce transposes, which complicates the operation, especially when we are dealing with tensors of higher dimensions.

Vector-Valued Functions. We generalize to f:RnRmf:\mathbb R^n\to\mathbb R^m.

f:RnRmfxi=[f1xifmxi]=[f1x1f1x2f1xnf2x1f2x2f2xnfmx1fmx2fmxn]\begin{align*} f: \mathbb R^n\to\mathbb R^m \\ \frac{\partial\mathbf f}{\partial x_i} = \begin{bmatrix} \frac{\partial f_1}{\partial x_i} \\ \vdots \\ \frac{\partial f_m}{\partial x_i} \end{bmatrix} = \begin{bmatrix} \frac{\partial f_1}{\partial x_1} & \frac{\partial f_1}{\partial x_2} & \cdots & \frac{\partial f_1}{\partial x_n} \\ \frac{\partial f_2}{\partial x_1} & \frac{\partial f_2}{\partial x_2} & \cdots & \frac{\partial f_2}{\partial x_n} \\ \vdots & \vdots & \ddots & \vdots \\ \frac{\partial f_m}{\partial x_1} & \frac{\partial f_m}{\partial x_2} & \cdots & \frac{\partial f_m}{\partial x_n} \end{bmatrix} \end{align*}

The gradient of f\mathbf f wrt a column vector is a row vector of the partial derivatives. This is the Jacobian matrix Jij=fixjJ_{ij}=\frac{\partial f_i}{\partial x_j}.

Numerator Layout vs Denominator Layout. What we use before is numerator layout where i-th output function fif_i corresponds to the i-th row of the jacobian dfdx\frac{d\mathbf f}{d\mathbf x} output matrix. Some authors use denominator layout which require transpose operation.

In Summary.

f:RR,dfdxRf:RnR,xf=dfdxR1×nf:RRm,dfdxRm×1f:RnRm,xfRm×n\begin{align*} f&:\mathbb R\to\mathbb R, \frac{df}{dx} \in R \\ f&:\mathbb R^n\to\mathbb R, \nabla_x f=\frac{df}{dx} \in R^{1\times n} \\ f&:\mathbb R\to\mathbb R^m, \frac{d\mathbf f}{dx} \in R^{m\times 1} \\ f&:\mathbb R^n\to\mathbb R^m, \nabla_x\mathbf f \in R^{m\times n} \\ \end{align*}

In general, for f:RnRmf: \mathbb R^n\to\mathbb R^m, the derivative is in the dimension of output times input, dfdxRm×n\frac{d\mathbf f}{d\mathbf x} \in R^{m\times n}.

Gradients of Matrices. Gradient of matrices w.r.t. vectors (or other matrices) are multidimensional tensors. The rule is the same, output dimension times input dimension.

ARm×n,BRp×qJ=dAdBR(m×n)×(p×q),Jijkl=AijBkl\begin{align*} A\in \mathbb R^{m\times n}, B\in \mathbb R^{p\times q} \\ J = \frac{d\mathbf A}{d\mathbf B} \in \mathbb R^{(m\times n)\times(p\times q)}, J_{ijkl} = \frac{\partial A_{ij}}{\partial B_{kl}} \end{align*}

Flattening Approach to Gradients of Matrices. Given a matrix ARm×nA \in \mathbb{R}^{m \times n}, flatten it into a vector vec(A)Rmn\text{vec}(A) \in \mathbb{R}^{mn}. Compute the gradient dvec(A)dvec(B)\frac{d\text{vec}(A)}{d\text{vec}(B)} where BRp×qB \in \mathbb{R}^{p \times q}, and reshape the resulting vector back R(m×n)×(p×q)\mathbb R^{(m\times n)\times(p\times q)}.

ARm×n,BRp×qvec(A)Rmn,vec(B)RpqJ=dvec(A)dvec(B)Rpq×mnreshapeR(m×n)×(p×q)\begin{align*} A\in \mathbb R^{m\times n}, B\in \mathbb R^{p\times q} \\ \text{vec}(A) \in \mathbb R^{mn}, \text{vec}(B) \in \mathbb R^{pq} \\ J = \frac{d\text{vec}(A)}{d\text{vec}(B)} \in \mathbb R^{pq\times mn} \rightarrow_{reshape} \mathbb R^{(m\times n)\times(p\times q)} \end{align*}

Kronecker product. Allows two matrices of arbitrary dimension to be multiplied. Given matrices ARM×NA \in R^{M\times N}, BRP×QB \in R^{P\times Q}, its Kronecker product ABA \otimes B is a (MP×NQ)(MP \times NQ)-matrix:

AB=[A11BA1NBAM1BAMNB]\begin{align*} A \otimes B = \begin{bmatrix} A_{11}B & \cdots & A_{1N}B \\ \vdots & \ddots & \vdots \\ A_{M1}B & \cdots & A_{MN}B \end{bmatrix} \end{align*}

Vector by Matrix Derivative using Vectorization and Kronecker Product.

Kronecker Product for Tensor Derivative.

Forward and Backward Propagation

Forward Autodiff. Good for small input large output.

vˉk=vkxc, for all k for some cvi connected to vj    viˉ=vixc=vivjvjxc=vivjvˉj\begin{align*} \bar v_k &= \frac{\partial{v_k}}{\partial{x_c}},\text{ for all }k\text{ for some }c \\ v_i \text{ connected to } v_j &\implies \bar{v_i} = \frac{\partial v_i}{\partial x_c} = \frac{\partial v_i}{\partial v_j}\frac{\partial v_j}{\partial x_c} = \frac{\partial v_i}{\partial v_j}\bar v_j \end{align*}

Backward Autodiff. Good for large input small output.

vˉk=fvk, for all kvi connected to vj    viˉ=fvi=vjvifvj=vjvivjˉ\begin{align*} \bar v_k &= \frac{\partial{f}}{\partial{v_k}},\text{ for all }k \\ v_i \text{ connected to } v_j &\implies \bar{v_i} = \frac{\partial f}{\partial v_i} = \frac{\partial v_j}{\partial v_i} \frac{\partial f}{v_j} = \frac{\partial v_j}{\partial v_i} \bar{v_j} \end{align*}