Skip to article content

Neural Networks

Back to Article
Probabilistic Perspective
Download Notebook

Probabilistic Perspective

Notebook Cell
!pip install otter-grader
Notebook Cell
fname = "ProbabilisticPerspective.zip"
URL = f"https://raw.githubusercontent.com/wecacuee/ECE490-F25-Neural-Networks/refs/heads/master/notebooks/02-linear-models/{fname}"
import urllib
from  zipfile import ZipFile
urllib.request.urlretrieve(URL, fname)
ZipFile(fname).extractall()
import otter
grader = otter.Notebook(tests_dir="./tests")

Optimization perspective is not enough

So far in this course, we stuck to an optimization perspective on Machine Learning. We identified the following steps:

  1. Collecting a training dataset D={(x1,y1),,(xn,yn)}\calD = \{ (\bfx_1, \bfy_1), \dots, (\bfx_n, \bfy_n) \}

  2. Selecting a model y^=f(x;w)\hat{\bfy} = f(\bfx; \bfw) with weights (also called the parameters) w\bfw.

  3. Selecting a loss function l(yi,y^i)l(\bfy_i, \hat{\bfy}_i)

  4. Training: Minimizing the total or average loss function to find the optimal weights

w=arg minw1ni=1nl(yi,y^i;w)=arg minw1ni=1nl(yi,f(xi;w))L(D;w)\begin{align} \bfw^* &= \arg~\min_{\bfw} \frac{1}{n} \sum_{i=1}^n l(\bfy_i, \hat{\bfy}_i; \bfw) \\ &= \arg~\min_{\bfw} \underbrace{\frac{1}{n} \sum_{i=1}^n l(\bfy_i, f(\bfx_i; \bfw))}_{L(\calD; \bfw)} \end{align}
  1. For a new input xD\bfx^* \notin \calD, use y^=f(x;w)\hat{\bfy}^* = f(\bfx^*; \bfw^*) as the prediction.

For linear models (or other “simple” models) this perspective works fine. However, in general it does not.

Question Imagine you are designing a pedestrian detector (input: image, output: a box around all pedestrians in the image) for an autonomous car (or a face detector for your ring-camera based security system). You collect a training dataset of pedestrians in Iceland and then try to deploy it in India.

  • How well is your pedestrian detector likely to work? Very good, very bad, okayish.

  • Why?

  • How would describe the reason mathematically?

Independent and Identically distributed

A random variable Z1Z_1 is said to be independent from random variable Z2Z_2, denoted as Z1Z2Z_1 \perp Z_2 if either of the three equivalent conditions are met:

  1. P(Z1,Z2)=P(Z1)P(Z2)\bbP(Z_1, Z_2) = \bbP(Z_1) \bbP(Z_2)

  2. P(Z1Z2)=P(Z1)\bbP(Z_1 | Z_2) = \bbP(Z_1)

  3. P(Z2Z1)=P(Z2)\bbP(Z_2 | Z_1) = \bbP(Z_2)

In typical machine learning, we want the data samples to be Independent and Identically Distributed, in short the i.i.d assumption.

Maximum Likelihood estimation

The optimization perspective can be related to maximum likelihood estimation under the iid (Independent and Indentically distributed) assumpotion.

Let the xi\bfx_i and yiy_i be random vectors for all ii. Model the probability distribution as a negative log of the loss function:

P((xi,yi)W)=1Zexp(l(yi,f(xi;W)).P((\bfx_i, y_i)| \bfW) = \frac{1}{Z} \exp(-l(y_i, f(\bfx_i; \bfW)).

If the samples are IID, then we can write the probability of the entire dataset as products of sample probabilities

P(DW)=i=1nP((xi,yi)W)P(\calD|\bfW) = \prod_{i=1}^n P((\bfx_i, y_i)| \bfW)
P(DW)=i=1n1Zexp(l(yi,f(xi;W)).P(\calD|\bfW) = \prod_{i=1}^n \frac{1}{Z} \exp(-l(y_i, f(\bfx_i; \bfW)).

A product of exponents is the summation of their powers,

P(DW)=1Zexp(i=1nl(yi,f(xi;W)).P(\calD|\bfW) = \frac{1}{Z} \exp(-\sum_{i=1}^n l(y_i, f(\bfx_i; \bfW)).

Denote

L(D;W)=i=1nl(yi,f(xi;W).L(\calD; \bfW) = \sum_{i=1}^n l(y_i, f(\bfx_i; \bfW).

Minimizing the loss is same as maximizing the probability P(DW)P(\calD|\bfW).

No free lunch theorem

In the era of so much machine learning hype it is important to look back at David Wolpert’s No free lunch theorems. ECE590 students must read Wolpert’s 1996 paper “The Lack of A Priori Distinctions Between Learning Algorithms” and mark the terms, symbols and concepts that do not make sense.

image.png
image.png

Expectation vs Mean

Given i.i.d. random variables Z1,Z2,...ZnZ_1, Z_2, ... Z_n with distribution P(.)\bbP(.), write the mathematical definition for the mean and that of the expectation. What is the difference?

Question:

What is the diference between Empirical risk and Expected risk?

Answer:

Empirical risk is the average loss over the dataset while Expected risk is the expectation of the loss when the inputs and outputs are treated as random variables.

For example, if a dataset D={(x1,y1),,(xn,yn)}\calD = \{(\bfx_1, y_1), \dots, (\bfx_n, y_n)\} with xiRn\bfx_i \in \bbR^n and yiRy_i \in \bbR is given and the model f(xi;W)f(\bfx_i; \bfW) predicts labels h^i=f(xi;W)\hat{h}_i = f(\bfx_i; \bfW). Then the empirical risk is the average loss over the dataset samples,

RD(W)=1ni=1nloss(y^i,yi).R_\calD(\bfW) = \frac{1}{n} \sum_{i=1}^n loss(\hat{y}_i, y_i).

On the other hand to compute Expected risk, we assume that the dataset D\calD was independently sampled (IID assumption) from a distribution PX,Y\bbP_{X,Y} where the input xi\bfx_i is an instance of random variable XX and yiy_i is an instance of random variable YY.

R(PX,Y,W)=EX,Y[loss(Y^,Y)],R(\bbP_{X,Y}, \bfW) = \bbE_{X,Y} [ loss(\hat{Y}, Y) ],

where Y^=f(X;W)\hat{Y} = f(X; \bfW).

If XX and YY are continous random variables, then the expectation is the weighted integraal over all possible values of values of XX and YY weighted by the joint probability density function (PDF) of XX and YY.

R(PX,Y,W)=EX,Y[loss(Y^,Y)]=xΩXyΩYloss(y^,y)fX,Y(x,y)dxdy,R(\bbP_{X,Y}, \bfW) = \bbE_{X,Y} [ loss(\hat{Y}, Y) ] = \int_{x \in \Omega_X} \int_{y \in \Omega_Y} loss(\hat{y}, y) f_{X,Y}(x, y) dx dy,

where y^=f(x;W)\hat{y} = f(x; \bfW) and ΩX\Omega_X and ΩY\Omega_Y is the sample space for XX and YY.

Generalization gap bounds in Machine Learning

There are many generalization gap bounds in machine learning, often studied under Machine Learning theory; Probably Approximatly Correct (PAC) theory. Mohri 2012, Foundations of Machine Learning is an excellent book to learn more about it.

If the loss l(.,.)l(.,.) is between [0, 1], then for any δ>0\delta > 0 with Probability at least 1δ1-\delta, we have

maxWWR(PX,Y,W)RD(W)2Rm(l)+ln(δ)2n\begin{align} \max_{\bfW \in \calW} R(\bbP_{X,Y}, \bfW) - R_\calD(\bfW) \le 2 \mathscr{R}_m(l) + \sqrt{\frac{-\ln(\delta)}{2n}} \end{align}

where Rademacher Complexity is defined as image.png

image.png

Bayes Rule

Question:

Given two random variables XX and YY specify the relationship between P(XY)P(X|Y) and P(YX)P(Y|X) using Bayes rule?

Answer
P(YX)=P(XY)P(Y)P(X)P(Y|X) = \frac{P(X|Y)P(Y)}{P(X)}
Question:

Using CIFAR 10 dataset D\calD you want to find your neural network with weights W\bfW. Define Prior, Likelihood and Posterior in terms of the weights W\bfW and data D\calD. Write Bayes rule to determin posterior from likehood and prior.

Answer:
  • Prior is P(W)P(\bfW) i.e. the probability of weights W\bfW before we have seen the dataset.

  • Likelihood is P(DW)P(\calD|\bfW) i.e. the probability of observing the dataset once if we pick a particular choice of weighs W\bfW.

  • Posterior is P(WD)P(\bfW|\calD) i.e the probability of chosing weights given the dataset D\calD.

By Bayes rule, we have:

P(WD)=P(DW)P(W)P(D)P(\bfW|\calD) = \frac{P(\calD|\bfW)P(\bfW)}{P(\calD)}
Question:

How can regularization can be interpretted as an application of the Bayes theorem?

Given the dataset D={(x1,y1),,(xn,yn)}\calD = \{ (\bfx_1, y_1), \dots, (\bfx_n, y_n) \}, a model y^i=f(xi;W)\hat{y}_i = f(\bfx_i; \bfW), a regularizer R(W)R(\bfW) and a loss function l(yi,y^i)l(y_i, \hat{y}_i), show that the following optimization problem can be interpreted as maximum-a-posteriori estimation. In the process show that for the interpretation, we need the IID (independently, identically distributed) assumption over the dataset. List any other assumptions that you need for the interpretation.

W=arg minWi=1nl(yi,f(xi;W))+λR(W),\bfW^* = \arg~\min_\bfW \sum_{i=1}^n l(y_i, f(\bfx_i; \bfW)) + \lambda R(\bfW),

where λ\lambda is some positive constant that balances between the loss function and the regularizer.

Answer:

Let the xi\bfx_i and yiy_i be random vectors for all ii. Model the probability distribution as a negative log of the loss function:

P((xi,yi)W)=1Zexp(l(yi,f(xi;W)).P((\bfx_i, y_i)| \bfW) = \frac{1}{Z} \exp(-l(y_i, f(\bfx_i; \bfW)).

If the samples are IID, then we can write the probability of the entire dataset as products of sample probabilities

P(DW)=i=1nP((xi,yi)W)P(\calD|\bfW) = \prod_{i=1}^n P((\bfx_i, y_i)| \bfW)
P(DW)=i=1n1Zexp(l(yi,f(xi;W)).P(\calD|\bfW) = \prod_{i=1}^n \frac{1}{Z} \exp(-l(y_i, f(\bfx_i; \bfW)).

A product of exponents is the summation of their powers,

P(DW)=1Zexp(i=1nl(yi,f(xi;W)).P(\calD|\bfW) = \frac{1}{Z} \exp(-\sum_{i=1}^n l(y_i, f(\bfx_i; \bfW)).

Denote

L(D;W)=i=1nl(yi,f(xi;W).L(\calD; \bfW) = \sum_{i=1}^n l(y_i, f(\bfx_i; \bfW).

The original optimization problem can be written as:

W=arg minWL(D;W)+λR(W)\bfW^* = \arg~\min_\bfW L(\calD; \bfW) + \lambda R(\bfW)

Taking negative exponent on both sides turns the problem into a maximization problem because exp(y)\exp(-y) is a monotonically decreasing function.

W=arg maxWexp(L(D;W))exp(λR(W))\bfW^* = \arg~\max_\bfW \exp(-L(\calD; \bfW))\exp(-\lambda R(\bfW))

The first term is the same as maximizing the likelihood P(DW)P(\calD|\bfW). If we interpret the second term as a prior:

P(W)=1Zexp(λR(W)),P(\bfW) = \frac{1}{Z'} \exp(-\lambda R(\bfW)),

then we can rewrite the original optimization problem as

W=arg maxWP(DW)P(W)\bfW^* = \arg~\max_\bfW P(\calD|\bfW) P(\bfW)

By Bayes theorem P(DW)P(W)=P(WD)P(D)P(\calD|\bfW) P(\bfW) = P(\bfW|\calD)P(\calD), hence we can write the optimization problem as maximizing the posterior

W=arg maxWP(WD)P(D).\bfW^* = \arg~\max_\bfW P(\bfW|\calD) P(\calD).

We can ignore the evidence term P(D)P(\calD), because it is independent of W\bfW the optimization variable. The original problem reduces to maximizing the posterior, hence maximum a posteriori:

W=arg maxWP(WD)\bfW^* = \arg~\max_\bfW P(\bfW|\calD)
Question:

You are doing 0-1 binary classification on the MNIST hand digit classification task. Let detecting the digit 1 be considered the positive class. Using this example, define the terms False negative, False positive, Accuracy, Precision, recall, F1-score.

Answer:
  • False negative: is the case when the predictor classifies the digit as negative i.e. 0 but it is actually 1.

  • False positive: is the case when the predictor classifies the digit as positive i.e. 1 but it is actually 0.

  • Accuracy: Total correct classifictions/Total samples .

  • Precision: True positives / Predicted positives.

  • Recall: True positives / All actual positives.

Question:

What is a ROC (Receiver Operating characteristics) for a binary classifier? Argue why it lies above the diagonal for any reasonable classifier? Also, argue why it must be concave?

Answer:
image.png

ROC curve is plot between False positive rate (FPR) P(Y^(X)=1Y=0)P(\hat{Y}(X) = 1| Y=0) and True positive rate (TPR) P(Y^(X)=1Y=1)P(\hat{Y}(X) = 1| Y = 1).

ROC curve is always lies above the diagonal because you can construct a trivial classifier that predicts the positive class with probability α(0,1)\alpha \in (0, 1), i.e. P(Y^=1)=αP(\hat{Y} = 1) = \alpha without even looking at the input data XX. This trivial classifier will form the diagonal of the ROC curve with both FPR and TPR being equal to α\alpha. Any classifier that works better than this trivial classifier will lie above the diagonal.

Why ROC curve is a concave curve

Suppose you are given hyperparameter values on the ROC curve of a classifier such that:

  1. The first classifier 1 has true positive rate (TPR) is P(Y^1(X)=1Y=1)=TPR(f1)P(\hat{Y}_1(X) = 1| Y = 1) = TPR(f_1) at given FPR P(Y^1(X)=1Y=0)=f1P(\hat{Y}_1(X) = 1| Y=0) = f_1 .

  2. Similarly second classifier 2 has FPR=P(Y^2(X)=1Y=0)=f2FPR = P(\hat{Y}_2(X) = 1| Y = 0) = f_2, assume it’s TPR is TPR(f2)TPR(f_2).

If you pick the classifier f1f_1 randomly with probability P(Y^=Y^1)=αP(\hat{Y} = \hat{Y}_1) = \alpha and the other one with probability P(Y^=Y^2)=1αP(\hat{Y} = \hat{Y}_2) = 1-\alpha, and take the output of the picked classifier.

P(Y^(X)=1Y=1)=P(Y^(X)=1Y^=Y^1,Y=1)P(Y^=Y^1)+P(Y^(X)=1Y^=Y^2,Y=1)P(Y^=Y^2)=P(Y^(X)=1Y^=Y^1,Y=1)α+P(Y^(X)=1Y^=Y^2,Y=1)(1α)=P(Y^1(X)=1Y=1)α+P(Y^2(X)=1Y=1)(1α)=TPR(f1)α+TPR(f2)(1α)\begin{align} P(\hat{Y}(X) = 1 | Y = 1) &= P(\hat{Y}(X) = 1 | \hat{Y} = \hat{Y}_1, Y = 1)P(\hat{Y} = \hat{Y}_1) + P(\hat{Y}(X) = 1 | \hat{Y} = \hat{Y}_2, Y = 1)P(\hat{Y} = \hat{Y}_2) \\ &= P(\hat{Y}(X) = 1 | \hat{Y} = \hat{Y}_1, Y = 1)\alpha + P(\hat{Y}(X) = 1 | \hat{Y} = \hat{Y}_2, Y = 1)(1-\alpha)\\ &= P(\hat{Y}_1(X) = 1 | Y = 1)\alpha + P(\hat{Y}_2(X) = 1 | Y = 1)(1-\alpha) &= TPR(f_1) \alpha + TPR(f_2)(1-\alpha) \end{align}

Then you have FPR of this new classifier as FPR=P(Y^1(X)=1Y=0)α+P(Y^2(X)=1Y=0)(1α)=f1α+f2(1α)FPR =P(\hat{Y}_1(X) = 1| Y = 0)\alpha + P(\hat{Y}_2(X) = 1| Y = 0)(1-\alpha) = f_1\alpha + f_2(1-\alpha) . If you get a better TPR than this randomized classifier, the ROC curve will be concave TPR(αf1+(1α)f2)>αTPR(f1)+(1α)TPR(f2)TPR^*(\alpha f_1 + (1-\alpha) f_2) > \alpha TPR(f_1) + (1-\alpha) TPR(f_2).

The definition of concavity: A function gg is concave if for all points y1y_1, y2y_2 and all α(0,1)\alpha \in (0, 1) the following condition is true, g(y1α+y2(1α)>g(y1)α+g(y2)(1α)g(y_1 \alpha + y_2 (1-\alpha) > g(y_1)\alpha + g(y_2)(1-\alpha) .

Question:

What is the likelihood ratio test for a binary classifier? Is it better or worse than Posterior ratio test?

Answer:

For a binary classifier, likelihood is the probability of observing the evidence x\bfx given a hypothesis class y{0,1}y \in \{0, 1\},

Likelihood(x,y)=P(xy)\text{Likelihood}(\bfx, y) = \bbP(\bfx | y)

Likelihood ratio test gives us a classifier that predicts the positive class y=1y = 1 if the likelihood ratio exceeds a given threshold η\eta,

y^(x)={1 if P(xy=1)P(xy=0)η0 otherwise \hat{y}(\bfx) = \begin{cases} 1 & \text{ if } \frac{\bbP(\bfx | y = 1)}{\bbP(\bfx | y = 0)} \ge \eta \\ 0 & \text{ otherwise } \end{cases}

Posterior is the probability of hypothesis class yy given an observed evidence x\bfx,

Posterior(x,y)=P(yx)=P(xy)P(y)P(x)\text{Posterior}(\bfx, y) = \bbP(y | \bfx) = \frac{\bbP(\bfx | y) \bbP(y)}{\bbP(\bfx)}

Let the posterior ratio test be,

y^(x)={1 if P(y=1x)P(y=0x)ηp0 otherwise \hat{y}(\bfx) = \begin{cases} 1 & \text{ if } \frac{\bbP(y = 1 | \bfx)}{\bbP(y = 0| \bfx)} \ge \eta_p \\ 0 & \text{ otherwise } \end{cases}

Posterior ratio test and likelihood ratio test are equivalent, because the condition for posterior ratio test can be written as likelihood ratio test,

P(y=1x)P(y=0x)ηp    P(xy=1)P(y=1)P(xy=0)P(y=0)ηp    P(xy=1)P(xy=0))P(y=0)P(y=1)ηp.\begin{align} \frac{\bbP(y = 1 | \bfx)}{\bbP(y = 0| \bfx)} &\ge \eta_p\\ \implies \frac{\bbP(\bfx | y = 1)\bbP(y=1)}{\bbP( \bfx|y=0)\bbP(y=0)} &\ge \eta_p \\ \implies \frac{\bbP(\bfx | y = 1)}{\bbP( \bfx|y=0))} &\ge \frac{\bbP(y=0)}{\bbP(y=1)}\eta_p. \end{align}

The last condition is the same condition in the likelihood ratio test with η=P(y=0)P(y=1)ηp\eta = \frac{\bbP(y=0)}{\bbP(y=1)}\eta_p.

Submission

Make sure you have run all cells in your notebook in order before running the cell below, so that all images/graphs appear in the output. The cell below will generate a zip file for you to submit. Please save before exporting!

Upload the generated zip file to the gradescope autograder

# Save your notebook first, then run this cell to export your submission.
grader.export(run_tests=True)