Foundations of Deep Learning¶

Spring 2026¶

Purdue University¶

Computer Science Department¶

Instructor and Copyright: Bruno Ribeiro¶


Understanding Deep Learning Tasks¶

Reading assigment for next class¶

The following chapters of the Deep Learning textbook are part of our first reading assigment:

Reading assigment: Linear Algebra

  • Chapter 2.1 (Scalars, Vectors, Matrices and Tensors)
  • Chapter 2.2 (Multiplying Matrices and Vectors)

Reading Assigment: Probability and Information Theory

  • Chapter 3.12 (Techical Details of Continuous Variables)
  • Chapter 3.13 (Information Theory)
    • KL-divergence

Reading assigment: Numerical Computation

  • Chapter 4.1 (Overflow and Underflow)
    • Softmax issues
  • Chapter 4.2 (Poor Conditioning)
  • Chapter 4.3 (Gradient-Based Optimization)
    • Gradient descent, critical points, local minima and maxima, saddle points
    • Jacobian matrix

Reading assigment: Deep Feedforward Networks

  • Chapter 6.1 (Example: Learning XOR)
  • Chapter 6.2 (Gradient-Based Learning)
  • Chapter 6.3 (Hidden Units)
  • Chapter 6.4 (Architecture Design)
  • Chapter 6.5 (Backpropagation)

References for this class: Understanding Continuous Random Variables¶

The following chapters of the Deep Learning textbook are part of our first reading assigment:

Reading Assigment: Probability and Information Theory

  • Chapter 3.12 (Techical Details of Continuous Variables)
  • Chapter 3.13 (Information Theory)
    • KL-divergence
  • Chapter 3.14 (Structured Probabilistic Models) [We will also go over graphical model basics today]

References for this class: Linear Algebra¶

The following chapters of the Deep Learning textbook are part of our first reading assigment:

Reading assigment: Linear Algebra

  • Chapter 2.1 (Scalars, Vectors, Matrices and Tensors)
  • Chapter 2.2 (Multiplying Matrices and Vectors)

Data-driven Modeling¶

Machine learning can be thought of as data-driven modeling. As a data-driven procedure, we first need to understand the data.

Notation¶

  • $\textbf{x}=(x_i)_{i=1}^3 = (x_1,x_2,x_3)$ denotes an ordered set (a vector), where the order of the variables matter.
  • $\{x_i\}_{i=1}^3 = \{x_1,x_2,x_3\}$ denotes an unordered set, where there is no notion of an order of the variables
    • Sets will often be denoted by the blackboard bold font: $\mathbb{X}$
  • Random variables will be denoted by capital letters $X$ and observations (samples) denoted by lowercase letters $x$.
    • Sometimes, for simplicity, I will denote $P(X=x)$ as simply $p(x)$.
      • If $X \in \mathbb{X}$ is discrete, $p(x)$ is a scalar in the interval $[0,1]$.
      • If $X \in \mathbb{X}$ is continuous, $p(x)$ is a function known as probability density function.
    • We will say $X$ is a random variable with distribution $p(x)$ using $$X \sim p(x).$$
  • Support of a distribution:
    • The support of a distribution is the set of elements that have non-zero probability: $$\text{supp}(X) = \{x : \forall x \in \mathbb{X} , p(x) > 0\}.$$
    • When we say $X \in \mathbb{X}$, we mean $\text{supp}(X) \subseteq \mathbb{X}$.
  • Matrix elements $x_{i,j}$ may be represented without the comma $x_{ij}$, where $x_{ij}$ is an observation that depends on both $i$ and $j$ indices.
    • Tensor elements will be defined similarly, such as $x_{i,j,k}$ for a 3D tensor.
    • We will use $\cdot$ as an index to denote all indices (same as ":" in python), where $x_{\cdot,j}$ denotes all elements of the matrix at the $j$-th column (a column vector).
  • Matrices will be represented by bold capital letters X, whenever possible.
  • Vectors are represented by bold lowercase letter x, unless we are making a statement that is true for a variable of any dimension. All our vectors are column vectors.
  • Applying a function with unidimensional input, e.g. $\exp$ and $\log$, over a vector $\textbf{x}$ implies applying the function over all elements of the vector. E.g., $\exp(\textbf{x})$ is a vector whose $j$-th element is $\exp(\textbf{x}_j)$.

IO: Independent Observations¶

  • IO: Supervised Learning
    • Continuous targets: Regression
    • Categorical targets: Classification
    • Inductive vs Transductive Learning
  • IO: Unsupervised Learning
  • IO: Semi-supervised Learning
  • IO: Reinforcement Learning

IO:SL: Supervised learning tasks¶

In supervised learning we seek to predict the values of outputs or response variables from a given set of input or predictor variables.

  • The data is given as a set $\{(x_i,y_i)\}_{i=1}^n$, $(x,y) \in \mathbb{R}^k$, $k \geq 2$.
  • We will assume observations $\{(x_i, y_i)\}_{i=1}^n$ are independent and identically distributed (i.i.d.) observations sampled from an unknown distribution $p(x,y)$.

Common assumptions:

  • Training data: $\{(x_i^\text{(tr)},y_i^\text{(tr)})\}_{i=1}^{n^\text{(tr)}}$ i.i.d. sampled from $p(x,y)$.
  • Test data: $\{(x_i^\text{(te)},y_i^\text{(te)})\}_{i=1}^{n^\text{(te)}}$ i.i.d. sampled from $p(x,y)$.

Objective:

  • Learn $p(y|x)$

IO:SL: Describing Joint Probability $p(y|x)$ (Statistics):¶

We can describe the join probability distribution $p(x,y)$ using a Directed Acyclic Graph (DAG) (see Chapter 3.14 (Structured Probabilistic Models)):

  • Nodes indicate a random variable
  • The arrow indicates p($\langle$arrow head$\rangle$ | $\langle$arrow tail$\rangle$)
    • If a node has no arrows, it is defined by a marginal distribution
      • E.g., if $X$ has no incoming arrows, it is defined by the marginal $p(x)$
  • The chain rule of probability): $$p(y|x)p(x) = p(x|y)p(y),$$ that is, there is no prefered direction in the description of joint dependencies between random variables $X$ and $Y$.
  • This equivalence of arrow direction is not how we instinctively think about probabilities
    • That is, for any joint probability $p(x,y)$ we can decompose it as either $p(y|x)p(x)$ or $p(x|y)p(y)$
    • This will be important to understand limitations of standard deep learning methods
No description has been provided for this image

Begin Detour: Understanding How Data is Artificially Generated¶

In Python use libraries to sample random variables

import numpy as np

[...]

ramdom_exp_values = np.random.exponential(rate)

ramdom_normal_values = np.random.normal(average,std)
  • But how do these libraries work?

Data Generation Process (i)¶

Inversion transform sampling method

  • This is the most fundamental technique for generating sample values of random variables
  • It uses the cumulative distribution function (CDF) of the random variable
  • The method depends on the fact that, for any random variable X, the CDF, $F_X(x) = P(X \leq x)$, is a non-decreasing function of $x$ that outputs a number in the interval [0,1]
  • Let $F_X^{-1}$ be the inverse of $F_X$, i.e., $x = F_X^{-1}(F_X(x))$.
  • Let $r \sim \text{Uniform}(0,1)$ be a random uniform value in the interval [0,1]
    • This is obtained by a pseudorandom number generator
  • Then, $$ x_\text{sample} = F_X^{-1}(r)$$ is a random sample with distribution $p(x)$.

Wikipedia example for exponential distribution $P(X \leq x) = F_{X}(x)=1-e^{-\lambda x}$ with inverse $x=F_X^{-1}(r)=-{\frac {1}{\lambda }}\ln(1-r)$:

No description has been provided for this image
In [4]:
import numpy as np
from numpy.random import random
from scipy import interpolate
import matplotlib.pyplot as plt

# Distribution density we want to sample from
def f(x):
    # density does not need to be normalized, since we will normalize ourselves
    return np.exp(-x**2) * np.cos(10*x)**4 * (x-1)**4/np.cosh(x/2)

def sample(g):
    x = np.linspace(start=-5,stop=5,num=100000)
    y = g(x)                        # 100k samples of probability density function, pdf
    cdf_y = np.cumsum(y)            # empirical cumulative distribution function, cdf
    cdf_y = cdf_y/cdf_y.max()       # normalizes cdf to 1.0

    inverse_cdf = interpolate.interp1d(cdf_y,x) # finds an approximation of the inverse CDF
    return inverse_cdf

def return_samples(N=1e6):
    # we will now get the inverse CDF
    Finv = sample(f)
    # let's generate some uniform samples
    uniform_samples = random(int(N))
    # and pass them through the inverse CDF of f(x)
    f_samples = Finv(uniform_samples)
    return f_samples


# Obtain 1M samples from the density f using inverse transform sampling
#return_samples()

## plot
x = np.linspace(start=-3,stop=3,num=10000)
fig,ax = plt.subplots()
ax.set_xlabel('x')
ax.set_ylabel('probability density')
ax.plot(x,f(x)/np.sum(f(x)*(x[1]-x[0])) )
ax.hist(return_samples(1e6),bins='fd',density=True,range=(x.min(),x.max()))
ax.legend(['True f(x) density', 'Histogram of samples'])
plt.show() 
No description has been provided for this image

Data Generation Process (ii)¶

  • The coding lemma (Aldous 1985), or its more general version, the noise transfer theorem (Kallenberg 2006, Theorem 6.10) states:

    • Any probability distribution $$Y \sim p(y),$$ with $Y \in \mathbb{R}^{d_Y}$ and $1 \leq d_Y < \infty$, can be described as the data generated by some transform sampling $$𝑌:=f_Y(U),$$ where $$f_Y \text{ is a deterministic function}$$ and $$U∼\text{Uniform}(0,1)$$ is some independent uniform random noise.
  • Notation: $a \sim b$ means $a$ is "sampled from distribution" $b$

Data Generation Process (iii)¶

Conditional Distributions¶

  • Any conditional probability where $X \in \mathbb{R}^{d_X}$ and $Y \in \mathbb{R}^{d_Y}$, with $1 \leq d_X,d_Y < \infty$, the conditional $$Y|X \sim p(y|x)$$ can be described as $$𝑌 :=f_{Y|X}(𝑋,\epsilon_Y),$$ where $:=$ means "defined as" and $$f_{Y|X} \text{ is a deterministic function}$$ and some independent noise such as $$\epsilon_Y∼\text{Uniform}(0,1).$$

End Detour: Understanding How Data is Generated¶

IO:SL: In-distribution v.s. Out-of-distribution (OoD) test data¶

  • Key standard deep learning assumption (In-Distribution Test Data):
    • Training data: $\{(x_i^\text{(tr)},y_i^\text{(tr)})\}_{i=1}^{n^\text{(tr)}}$ i.i.d. sampled from $p(x,y)$.
    • Test data: $\{(x_i^\text{(te)},y_i^\text{(te)})\}_{i=1}^{n^\text{(te)}}$ i.i.d. also sampled from $p(x,y)$.
  • We will revisit this basic assumption over and over throughout the course
    • I will probably say "make the train data more like the test data" 30+ times throughout the course
  • Generalization error is really measuring In-Distribution test errors.
  • If $p(x,y)$ in test is different than training (Out-of-Distribution (OoD)), the difference between the learned generation process and the true generation process may show cracks

    • Assume deep learning model for $p(y|x)$ for a given $p(x)$ that perfectly generalizes in test when test also comes from $p(y,x)$.
    • Same model may make unbelievably bad predictions of $p(y|x)$ when $p(x)$ changes in test.
  • Throughout the course we will learn techniques to deal with these issues.

Out-of-distribution Example: No matter how large, existing NLP models will tend to fail to correctly predict the answer:

(Train data) All men are mortal. Socrates is a man. Therefore, Socrates is mortal.
(Train data) All programmers are happy. Alice is a programmer. Therefore, Alice is happy.

(Test data) All elephants are heavy. Pinocchio is a boy. Therefore, Pinocchio is ⟨predict⟩.

  • I created this test data to break the simplest learned data generation process
    • Note that this test data does not have the same distribution as the training data
    • That is, the test data is out-of-distribution

Example from ChatGPT 3.5 (now LLMs have been better hardened against Out-of-distribution examples):

No description has been provided for this image
  • Note that the data lures the deep learning model to learn a simple structural model... one that is wrong.
    • All standard machine learning methods will have the same difficulty (deep learning / neural networks are not special)
    • "More data" is not the answer when the train and test data have different distributions

IO:SL: Other Important General Concepts¶

Incorrect ML intuition¶

  • Intuitions derived from Linear Models may not apply to deep learning models
  • Intuitions derived from 2D & 3D data may not apply to high-dimensional data (we will cover this later)

IO:SL: Understanding Real-Valued Random Variables¶

Set Cardinality¶

  • "Some infinities that are bigger than others" - Hermann Weyl

In mathematics, the cardinality of a set is a measure of the number of elements in the set.

  • Interestingly, because there are different sizes of infinity, there are also different sizes of infinite sets.
  • The set of real numbers $\mathbb{R}$ has a larger cardinality than the set of natural $\mathbb{N}$, integer $\mathbb{Z}$, and rational numbers $\mathbb{Q}$.
    • Hence, there is an injective function from $\mathbb{N}$ (or $\mathbb{Z}$, or $\mathbb{Q}$) to $\mathbb{R}$, but no bijective function.
  • The set of real numbers $\mathbb{R}^d$ with finite dimension $1 \leq d < \infty$ has the same cardinality as the open real interval (0,1)

    • Hence, there is always a bijective function $f: S \to \mathbb{R}^d$, where $S \subseteq \mathbb{R}$ is a convex set for any constant $d>0$.
      • Since $f$ is bijective, $f^{-1}: \mathbb{R}^d \to S$ exists
      • Such $f$ function is likely non-diferrentiable but can be approximated well for most tasks.
  • Definitions

    • An injective function $i:\langle\text{Domain}\rangle \to \langle\text{Image}\rangle$ is a function that maps distinct elements of its domain to distinct elements of its image
    • A surjective function $s:\langle\text{Domain}\rangle \to \langle\text{Image}\rangle$ is a function that maps distinct elements of its image to at least one element in the domain
    • A bijective function is both injective and surjective.

IO:SL: Consequence: Lower Dimensional Representations in Deep Learning are not Lossy Compressions¶

Consider this dimensionality reduction procedure:

  • $f:\mathbb{R}^{d_X} \to\mathbb{R}^{k}$ is an injective function
  • $g :\mathbb{R}^{k} \to\mathbb{R}^{d_Y}$ is a surjective function.
  • Assume $k \ll \min(d_X,d_Y)$, $k \geq 1$.
No description has been provided for this image
Example: Autoencoders¶
  • $f :\mathbb{R}^{28\times 28} \to\mathbb{R}^{10}$
  • $g :\mathbb{R}^{10} \to\mathbb{R}^{28\times 28}$
    No description has been provided for this image
    Figure 2: Example of an Autoencoder neural architecture
    No description has been provided for this image
Cardinality Implications to Deep Learning¶
  • Deep learning is basically the task of learning functions.
    • Suppose we want to learn functions $f$ and $g$ above
  • If learnable functions $f$ and $g$ are expressive enough to describe most injective and surjective functions:
    • Mapping the input $d_X$-dimensional into a $k$-dimensional intermediate representation, $k < d_X$, is not a low dimensional simplification of the input
    • Remember, $\mathbb{R}^{d_X}$ and $\mathbb{R}^{k}$ have the same cardinality for any values of $1 \leq k,d_X < \infty$.
  • This "low dimensional representation" = "input simplification" is a common mistake folks make when desingning neural networks:
    • The notion of "input simplification" is directly tied to either:
      • Constrains over the expressiveness of functions $f$ and $g$ (we should not be able to learn any injective/surjective functions)
        • If $f$ and $g$ are linear, $\textbf{x} \in \mathbb{R}^{d_X}$ is the input, and the output of $f(\textbf{x})$ is $\textbf{u} \in \mathbb{R}^k$.
        • Then, yes, $\textbf{u}$ is a low dimensional simplification of $\textbf{x}$.
      • Floating point errors, since $\textbf{x}\in \mathbb{R}^{d_X}$ has more floating point (FP) bits than $\textbf{u} \in \mathbb{R}^{k}$.
        • But this compression is relying on numerical precision error, hardly a good idea when talking about learning a function.

Transductive vs Inductive Learning¶

  • Transductive learning:

    • Answers queries about examples available during training
    • In supervised learning:
      • Use labeled training data to build a model that can label all unlabeled examples in the training data
      • If given new (unseen) examples, must re-run the training algorithm
      • Example: Task is to predict the colors of the gray nodes for the graph given in training
        No description has been provided for this image
  • Inductive learning:

    • Learns (inductive) rules that can be applied to new (unseen) examples
    • In supervised learning:
      • Use labeled training data to build a model that can label examples which we never encountered
      • If given new (unseen) examples, uses the predictive model to label them
      • Example: Task is to predict the colors of the gray nodes for the graph given in training and for a new graph
        No description has been provided for this image

IO:UL: Unsupervised learning tasks¶

In unsupervised learning we have a set of $n$ observations $\{x_i\}_{i=1}^n$, $x_i \in \mathbb{R}^d$, $d\geq 1$, are sampled i.i.d. from $p(x)$.

Common assumptions:

  • Training data: $\{x_i^\text{(tr)}\}_{i=1}^{n^\text{(tr)}}$ i.i.d. sampled from $p(x)$.
  • Test data: $\{x_i^\text{(te)}\}_{i=1}^{n^\text{(te)}}$ i.i.d. sampled from $p(x)$.

Objective:

  • Learn $p(x)$

Quiz 1: Is unsupervised learning more powerful than supervised learning? That is, can unsupervised learning perform all tasks that supervised learning can perform plus tasks that supervised learning cannot perform?

Quiz 2: Is unsupervised learning easier or harder to learn than supervised learning for the same task? Why?

A: Because unsupervised learning must learn $p(y,x)$, that is, both $p(x)$ and $p(y|x)$. Supervised learning needs to learn only $p(y|x)$ (Ng and Jordan, 2002)

IO:SSL: Self-supervised Learning¶

Let $M(\textbf{x})$ be a mask function from a set of masks $\mathcal{M}$ which takes a vector $\textbf{x}$ as input and returns some elements of $\textbf{x}$. Let $\overline{M}(\textbf{x})$ be the elements of $\textbf{x}$ that are not in the mask $M(\textbf{x})$.

PS: In some applications, self-supervised learning can be combined with supervised learning.

Image example:

No description has been provided for this image

More generally in temporal tasks (credit Yann LeCun):

No description has been provided for this image

Common assumptions

  • Training data: $\{x_i^\text{(tr)}\}_{i=1}^{n^\text{(tr)}}$ i.i.d. sampled from $p(x)$.
  • Test data: $\{x_i^\text{(te)}\}_{i=1}^{n^\text{(te)}}$ i.i.d. sampled from $p(x)$.

Objective:

Self-supervised learning is trying to learn $$P(M(\textbf{x}) | \overline{M}(\textbf{x})), \quad \forall M \in \mathcal{M}$$

IO:SemiL: Semi-supervised learning tasks¶

In semi-supervised learning, the training data consists of $(y^{(tr)}_i,x^{(tr)}_i)_{i=1}^n$ sampled i.i.d. from an unknown distribution $p(x,y)$ and a set of unlabeled examples $\{x^{(tr)}_j\}_{j=n+1}^m$ sampled i.i.d. from $p(x)$, which is the marginal of $p(x,y)$, i.e., $p(x) = \int_y p(x,y) dy$.

Common assumptions:

  • Training data: $\{(x_i^\text{(tr)},y_i^\text{(tr)})\}_{i=1}^{n^\text{(tr)}}$ i.i.d. sampled from $p(x,y)$ and $\{(x_i^\text{(tr)})\}_{i=n^\text{(tr)}+1}^{m}$ sampled i.i.d. from the marginal $p(x)= \int_y p(x,y) dy$.
  • Test data: $\{(x_i^\text{(te)},y_i^\text{(te)})\}_{i=1}^{n^\text{(te)}}$ i.i.d. sampled from $p(x,y)$.

Objective:

  • Learn $p(y|x)$

Example:

No description has been provided for this image

IO:SL:TL: Transfer Learning¶

In transfer learning, we are given

  1. Task 1: A lot of data $\{(x_i^\text{(tr)},y_{2,i}^\text{(tr)})\}_{i=1}^{n_2^\text{(tr)}}$ i.i.d. sampled from $p(x,y_2)$.
  2. Task 2: A much smaller amount of data $\{(x_i^\text{(tr)},y_{1,i}^\text{(tr)})\}_{i=1}^{n_1^\text{(tr)}}$ i.i.d. sampled from $p(x,y_1)$.

Common assumptions:

  • Training data is i.i.d. sampled
  • There exists a representation function $f$, which, through the noise outsource theorem, defines the observed labels $\forall i$: $$Y_{2,i} = g_2(f(x_i),\epsilon_2)$$ and $$Y_{1,i} = g_1(f(x_i),\epsilon_1),$$ where $\epsilon_2$ and $\epsilon_1$ are i.i.d. random noise sources.
    • We will have a lecture dedicated to transfer learning
  • Test data: $\{(x_i^\text{(te)},y_{1,i}^\text{(te)})\}_{i=1}^{n^\text{(te)}}$ i.i.d. sampled from $p(x,y_1)$.

Objective:

  • Learn the representation $f(x)$ and the probability $p(y_1|x)$

Example of transfer learning task: Consider learning the rare event label $y_{ls} = \textbf{1}\{\text{lightning ground strike}\}$ and with label $y_l = \textbf{1}\{\text{lightning}\}$, where $x$ is a set of atmospheric features (pressure, humidity, ...). Note that $y_l$ shows that even if there was no lightning strike, if $y_l = 1$ (lightning) then we know that given there is lightning, the probability of a strike should be significant, i.e., $p(y_{ls}=1|y_l = 1, x)$ should be large even if we have observed $y_{ls} = 0$ (no lightning strike).

  • In problems where only have data for $p(y_l = 1 | x)$, we will first learn a representation $f(x)$ which can be used to approximate $p(y_l = 1 | x) = g_l(f(x))$ (lightning probability).

    • $g_l$ will often be a logistic regression
  • Once we collect lightning strike data, use $f(x)$ to learn to approximate $p(y_{ls} = 1 | x) = g_{ls}(f(x))$.

Domain Adaptation, Covariate Shift¶

In domain adaptation, the training and test data distributions are different. In real-world applications, this is quite common ocurence.

Common assumptions of covariate shift:

  • Training data: $\{(x_i^\text{(tr)},y_i^\text{(tr)})\}_{i=1}^{n^\text{(tr)}}$ i.i.d. sampled from $p^\text{(tr)}(x,y) = p(y|x)p^\text{(tr)}(x)$.
    • Extra data given in the form $\{x_j\}_j$ such that $p^\text{(x)}(x) \approx p^\text{(te)}(x), \forall x$.
  • Test data: $\{(x_i^\text{(te)},y_i^\text{(te)})\}_{i=1}^{n^\text{(te)}}$ i.i.d. sampled from $p^\text{(te)}(x,y) = p(y|x)p^\text{(te)}(x)$.

Alternative settings:

  • In some domain adaptation tasks, $p(y)$ may also change (Zhang et al. 2013) while $p(x|y)$ remains the same between train and test.
  • In more complex domain adaptation tasks, $p(y|x)$ may also change between training and testing (Long et al. 2016).

Objective:

  • Learn $p(y|x)$, in a way that is robust to changes in $p(x)$ (or $p(y)$). Or learn $p(y|x)$ in a way that is well adapted to a given distribution in the test data $p^\text{(te)}$ which is different from the training data distribution $p^\text{(tr)}$.

Examples:

  • We will later see in the course that one of the simplest examples of domain adaptation is class imbalance (an adaptation for $p(y)$).

  • Consider obtaining training data for selling winter coats with x = (weekly temperature, age, gender) and the coat they buy $y$. Our data comes from Indiana ($\{(x_i^\text{(tr)},y_i^\text{(tr)})\}_{i=1}^{n^\text{(tr)}}$) and then applying our algorithm to sell winter coats in California ($\{(x_i^\text{(te)},y_i^\text{(te)})\}_{i=1}^{n^\text{(te)}}$). We would then get the feature distribution samples from a state similar to California $(x_i^\text{(z)})$ or even from California itself $(x_i^\text{(te)})$ but without the labels $\{(y_i^\text{(te)})\}_i$.

DO: Dependent Observations¶

  • Dependent Observations = Independet Observations if we cast the dependent variables as "a single joint observation"
    • We will revist this when we talk about learning from inputs such as sequences, graphs, and sets.

Longitudinal vs Cross-sectional Data of Human Subjects Text

DO: Longitudinal (temporal) dependencies¶

In longitudinal data, the dependencies can range from trivial to inacessibly complex. The data is represented as a set of sequences. In supervised learning, the sequence is associated with a label. In unsupervised learning, we only have the sequence.

  • Data
    • Unsupervised task: $\{(x_{i,t})_{t=1}^T\}_{i}$, where $x_{i,t}$ is the $t$-th observation of subject $i$.

    • Supervised task: $\{((x_{i,t})_{t=1}^T,y_i)\}_{i}$, where $x_{i,t}$ is the $t$-th observation of subject $i$. The sequence has label $i$.

  • Decomposition: The joint probability of any sequence $(x_{t})_{t=1}^T$ can be decomposed as conditional probabilities: $$p(X_1 = x_1,\ldots,X_T = x_T) = p(X_T = x_T| X_{T-1} = x_{T-1} , \ldots X_1 = x_1) \cdots P(X_1 = x_1).$$

Types of sequences:

  • Independent: A sequence $(x_{t})_{t=1}^T$ is independent if it has probability distribution $p(X_1 = x_1,\ldots,X_T = x_T) = \prod_{t=1}^T p(X_t = x_t).$ Note that $p(X_t = x | X_{t-1} = x_{t-1},\ldots,X_{1} = x_1)= p(X_t = x)$.
    • Time homogeneous independent sequence: A time-homogeneous independent sequence is a sequence where the probability distribution of the observations does not depend on the time of the observation, i.e., $p(X_t = x | X_{t-1} = x_{t-1},\ldots,X_{1} = x_1)= p(X = x)$.
    • most textbook descriptions of independent sequences describe time-homogeneous sequences.
  • First order Markovian: A sequence $(x_{t})_{t=1}^T$ is first order Markovian if its probability distribution $$p(X_1 = x_1,\ldots,X_T = x_T) = p(X_1 = x_1) \prod_{t=2}^T p(X_t = x_t | X_{t-1} = x_{t-1}).$$ Note that $$p(X_t = x | X_{t-1} = x_{t-1},\ldots,X_{1} = x_1)= p(X_t = x_t | X_{t-1} = x_{t-1}).$$
    • Time homogeneous Markov process: A time-homogeneous Markovian sequence is a sequence where the probability distribution of the observations does not depend on the time of the observation, i.e., $$p(X_t = x | X_{t-1} = x_{t-1},\ldots,X_{1} = x_1) = p(X_t = x | X_{t-1} = x_{t-1})= p(X_\text{next} = x_\text{next} | X_\text{previous} = x_\text{previous}).$$
      • most textbook descriptions of Markov processes describe time-homogeneous processes.
  • $K$-th order Markovian: A sequence $(x_{t})_{t=1}^T$ is $K$-th order Markovian if its probability distribution $$ \begin{aligned} p(X_1 = x_1,\ldots,X_T = x_T) = & p(X_1 = x_1) p(X_2 = x_2 | X_1 = x_1) \cdots p(X_K = x_K | X_{K-1} = x_{K-1},\ldots, X_1 = x_1) \\ & \times \prod_{t=K+1}^T p(X_t = x_t | X_{t-1} = x_{t-1},\ldots,X_{t-K} = x_{t-K}). \end{aligned} $$ Note that $$p(X_t = x | X_{t-1} = x_{t-1},\ldots,X_{1} = x_1)= p(X_t = x_t | X_{t-1} = x_{t-1},\ldots,X_{t-K} = x_{t-K}).$$
    • Time homogeneous $k$-th order Markov process: Probabilities are not time-dependent. Just extend the first order time-homogeneous Markov process case to consider the last $K$ steps.
  • Sequences with infinite dependencies: A sequence $(x_{t})_{t=1}^\infty$ has infinite-order dependencies if $\forall K > 1$, $\exists t > K$ such that its probability distribution $p(X_1 = x_1,\ldots,X_t = x_t)$ cannot be decomposed into a $K$-th order Markov chain.

DO: Cross-sectional dependencies¶

In cross-sectional data, the dependencies can also range from trivial to inacessibly complex. The data is often represented as a graph, although it graphs rarely capture the full complexity of the data. In supervised learning, the graph or its subgraphs are associated with a label. In unsupervised learning, we only have the graph. Both nodes and edges can have attributes.

Data:

  • DO:SL: Unsupervised tasks:
    • $\{(x_{ij})\}_{i,j}$, where $x_{ij}$ is an observation that depends on both subjects $i$ and $j$.
  • DO:UL: Supervised tasks:
    • $\{(x_{ij},y_{ij})\}_{i,j}$, where $x_{ij}$ is an observation that depends on both subjects $i$ and $j$. The relationship has label $y_{ij}$.
    • $\{(x_{ij})\}_{i,j}$, $\{(z_i,y_i)\}_i$, where $x_{ij}$ is an observation that depends on both subjects $i$ and $j$ and $z_i$ is an observation that depends only on subject $i$. Each subject $i$ has label $y_{i}$ (node attributes).

IO: Learning Representations¶

IO:SL: In supervised learning¶

In supervised learning tasks, we will combine the representation $f$ and the connecting function $g$ into a single parameterized probability function $p(y|x,\textbf{W}) = g(f(x))$, where $\textbf{W}$ are learnable parameters.

Let's start with a very simple task. Consider the task of learning a binary classifier $y \in \{0,1\}$. Assume data generated via the probability model $$ x_i,y_i \sim p(y | x; \textbf{W}) p(x), \tag{1} $$ where $\textbf{W}$ is fixed but unknown distribution parameter.

Given: $n$ independent and identically distributed (i.i.d.) samples of the data $$ \mathcal{D} = \{(x_i,y_i)\}_{i=1}^n. $$

  • i.i.d. means: Given $\textbf{W}$, each sample $(x_i,y_i)$ is independent of all other samples but drawn from the same distribution.

Goal: Estimate parameter $\textbf{W}$ that best describes the data. There are multiple ways to describe "best".

IO:SL: Maximum Likelihood Estimation (MLE)¶

In Maximum Likelihood Estimation (MLE) we choose the parameter $\textbf{W}$ that maximizes the probability of the data, given parameter $\textbf{W}$.

  • The probability of the data given $\textbf{W}$ is called the Likelihood, defined as: $$ \mathcal{L}(\mathcal{D}) = p(\mathcal{D} | \textbf{W}) = \prod_{i=1}^n p(y_i | x_i; \textbf{W}). \tag{2} $$

Quiz 1: Why $p(x)$ is not part of the optimization?

A: Since $x_i,y_i \sim p(x,y)$, the maximum likelihood is an approximation of the (true) risk associated with hypothesis that $\textbf{W}$ are the true values of the parameter, which is defined as the expectation of the loss function: $$ E_{x_i,y_i}\left[ p(y_i | x_i; \textbf{W})\right] = \int_{x,y} p(y | x; \textbf{W}) p(x,y), $$ where in an abuse of notation we say $p(a)$ can be thought as a density function times $da$ if $a$ is continuous and a discrete probability if $a$ is discrete.

Quiz 2: When should $p(x)$ be part of the optimization?

A 2: We will see this later in domain adaptation: When $p^\text{tr}(x) \neq p^\text{te}(x)$. We will revisit this when we cover domain adaptation.

Typically we will maximize the Log-likelihood instead of the likelihood. The Log-likelihood has a number of computational (and theoretical) advantages. Because the logarithm function is concave, the parameter $\textbf{W}^\star$ that maximizes the Likelihood also the parameter that maximizes the Log-likelihood.

  • Log-likelihood: $$ \log \mathcal{L}(\mathcal{D}) = \log p(\mathcal{D} | \textbf{W}) = \sum_{i=1}^n \log p(y_i | x_i; \textbf{W}). $$

  • Maximum log-likelihood parameter estimation: $$ \hat{\textbf{W}} = \arg\!\max_\textbf{W} \log \mathcal{L}(\mathcal{D}) = \arg\!\max_\textbf{W} \sum_{i=1}^n \log p(y_i | x_i; \textbf{W}). $$

IO:SL: Maximum-a-Posteriori Estimation (MAP)¶

Consider again the setting in Eq. $\eqref{eq:py_x}$. Suppose we have a prior $p(\textbf{W})$ over parameter $\textbf{W}$. The prior probability of $\textbf{W}$ is obtained without having seen any data.

The posterior probability of $\textbf{W}$ is given by the Bayes Rule $$ p(\textbf{W} | \mathcal{D}) = \frac{p(\mathcal{D} | \textbf{W})p(\textbf{W})}{p(\mathcal{D})}, $$ where $p(\mathcal{D} | \textbf{W})$ of Eq. $\eqref{eq:like}$ is the likelihood function, and $p(\mathcal{D})$ is the probability of the data (independent of $\textbf{W}$), defined as $$ p(\mathcal{D}) = \int_\textbf{W'} p(\mathcal{D} | \textbf{W'})p(\textbf{W'}) d\textbf{W'}. $$

Using the Bayes rule, we update update our initial belief $p(\textbf{W})$ about $\textbf{W}$ in the light of observed data.

As with the MLE, in MAP we usually maximize the log of the posterior probability: $$\begin{aligned} \hat{\textbf{W}}_\text{MAP} &= \arg\!\max_\textbf{W} \frac{p(\mathcal{D} | \textbf{W})p(\textbf{W})}{p(\mathcal{D})} \\ &= \arg\!\max_\textbf{W} \log \frac{p(\mathcal{D} | \textbf{W})p(\textbf{W})}{p(\mathcal{D})} \\ &= \arg\!\max_\textbf{W} \log p(\mathcal{D} | \textbf{W})P(\textbf{W}) - \log p(\mathcal{D}) \\ &= \arg\!\max_\textbf{W} \sum_{i=1}^n \log p(y_i | x_i; \textbf{W}) + \log p(\textbf{W}) \end{aligned}$$

IO:SL: Supervised Learning Examples (also Inductive Learning Examples)¶

A Probabilistic Formulation of Linear Regression:¶
  • MLE: Consider a response $y$ generated by a linear model plus some independent Normal noise $$ y = \textbf{w}^T \textbf{x} + b + \epsilon, $$ where $\textbf{x} \in \mathbb{R}^d$ is vector of input features, $\textbf{w} \in \mathbb{R}^d$ are the input-dependent parameters, $b \in \mathbb{R}$ is the bias, and $\epsilon$ is a white (zero mean) noise and drawn from a Normal distribution $$ \epsilon \sim N(0,\sigma^2). $$ Then, the response $y$ is draw from the following Normal distribution $$ y \sim N(\textbf{w}^T \textbf{x} + b,\sigma^2), $$ with probability density function $$ p(y | \textbf{x}; \textbf{w}, b, \sigma) = \frac{1}{\sqrt{2 \pi \sigma^2}} \exp\left( - \frac{(y - \textbf{w}^T \textbf{x} - b)^2}{2 \sigma^2}\right).$$

MLE Goal: Estimate $\textbf{w}$ from data. Given a dataset $D = {(\textbf{x}_1,y_1),(\textbf{x}_2,y_2),\ldots,(\textbf{x}_n,y_n)}$, we want to estimate the parameters $\textbf{w}$ and $b$.
Assume $\sigma$ is known. The log-likelihood is given by $$ \log \mathcal{L}(\textbf{w},b) = -\frac{n}{2} \log(2 \pi \sigma^2) - \frac{1}{2 \sigma^2} \sum_{i=1}^n (y_i - \textbf{w}^T \textbf{x}i - b)^2 , $$ noting that $-\frac{n}{2} \log(2 \pi \sigma^2)$ does not depend on $\textbf{w}$ and $b$, the MLE is $$ \textbf{w}^\star,b^\star = \arg\!\max_{\textbf{w},b} - \sum_{i=1}^n (y_i - \textbf{w}^T \textbf{x}i - b)^2 = \arg\!\min_{\textbf{w},b} \sum_{i=1}^n (y_i - \textbf{w}^T \textbf{x}_i - b)^2. $$

  • MAP: Maximum-a-Posteriori Solution. We assume a standard Normal prior distribution over the weight vector $\textbf{w}$: $$ \textbf{w} \sim N(\textbf{0},\frac{1}{\lambda} \textbf{I}). $$ The MAP estimate is $$ \textbf{w}^\star,b^\star = \arg\!\min_{\textbf{w},b} \sum_{i=1}^n (y_i - \textbf{w}^T \textbf{x}_i - b)^2 + \frac{\lambda}{2} \Vert\textbf{w}\Vert_2^2. $$

    Note that for $\sigma = 1$, the above is equivalent to the regularized least-squares objective. Also note that we should not regularize the bias $b$.
  • What we have learned:
    • MLE estimation of a parameter leads to unregularized solutions
    • MAP estimation of a parameter leads to regularized solutions
    • The prior distribution acts as a regularizer in MAP estimation
IO:SL: A Probabilistic Formulation of Cross-Entropy Loss:¶

Binary classification: Similarly, consider a binary classifier, $p(y = 1 | \textbf{x}; \textbf{W})$, $y\in \{0,1\}$, that is described by the logistic regression classifier $$ p(y = 1 | \textbf{x}; \textbf{w},b) = \sigma(\textbf{w}^T \textbf{x} + b), $$ where $\textbf{x} \in \mathbb{R}^d$ is vector of input features, $\textbf{W} = (\textbf{w},b)$ with $\textbf{w} \in \mathbb{R}^d$ and $b \in \mathbb{R}$, and $\sigma$ is the sigmoid function: Source: Wikipedia The Logistic Regression model is a misnomer. Logistic Regression is a classification model.

Classification goal: Estimate $\textbf{w}$ from data. Given a dataset $D = \{(\textbf{x}_1,y_1),(\textbf{x}_2,y_2),\ldots,(\textbf{x}_n,y_n)\}$, we want to estimate the parameters $\textbf{w}$.
The log-likelihood of $\mathcal{D}$ is $$ \mathcal{L}(\mathcal{D}) = \sum_{i=1}^n \log p(y_i | \textbf{x}_i; \textbf{w},b) = \sum_{i=1}^n y_i \log \sigma(\textbf{w}^T \textbf{x}_i + b) + (1 - y_i) \log (1 - \sigma(\textbf{w}^T \textbf{x}_i + b)). $$ The loss $\mathcal{L}(\mathcal{D})$ is also known as the cross-entropy loss.

Decision boundary: What does the decision boundary look like for Logistic Regression? At the decision boundary labels 1/0 becomes equiprobable, i.e., $$\begin{aligned} p(y = 1 | \textbf{x}; \textbf{w},b) & = 0.5 \\ \sigma(\textbf{w}^T \textbf{x} + b) & = 0.5\\ \textbf{w}^T \textbf{x} + b & = 0. \end{aligned}$$ The decision boundary is therefore linear $\implies$ Logistic Regression is a linear classifier of the input $\textbf{x}$.

Example with $\textbf{x} \in \mathbb{R}^2$:

Source: Elements of Statistical Learning

Later we will use a simple trick to "remove" this linearity constraint: we will make sure that the Logistic Regression input is not a linear representation of the real data input. For this, we will later stack multiple Logistic Regressions on top of each other (multilayer perceptron classifier).

Mutli-class classification: Assume the target $y \in \{1,\ldots, C\}$ has $C$ possible classes. Then $$ p(y = c | \textbf{x}; \textbf{W},\textbf{b}) = \text{softmax}(\textbf{W}\textbf{x} + \textbf{b})_c, $$ where $\textbf{x} \in \mathbb{R}^d$ is vector of input features, model parameters are defined as $\textbf{W} \in \mathbb{R}^{C \times d}$ and $\textbf{b} \in \mathbb{R}^C$, and $$ \begin{aligned} \text{softmax}(\textbf{z})_c &= \left( \frac{\exp(\textbf{z}_c)}{\sum_{j =1}^C \exp(\textbf{z}_j)} \right)_c, c = 1,\ldots,C, \end{aligned} $$ where $\text{softmax}(\textbf{z}) \in (0,1)^C$ is a positive vector whose elements are in the interval (0,1) and the vector elements sum to one.
The log-likelihood of $\mathcal{D}$ is $$ \mathcal{L}(\mathcal{D}) = \sum_{i=1}^n \log p(y_i | \textbf{x}_i; \textbf{W},\textbf{b}) = \sum_{i=1}^n \sum_{c=1}^C \textbf{1}\{y_i = c\} \log (\text{softmax}(\textbf{W}\textbf{x} + \textbf{b})_c). $$

In practice, calculating the exponentials in softmax is numerically unstable, since the input values could be extremely large. We introduce a numerical trick by multiplying and dividing by a constant $R$ to mitigate such problem: $$ \text{stable\_softmax}(\textbf{z})_c = \left( \frac{R \exp(\textbf{z}_c)}{R \sum_{j =1}^C \exp(\textbf{z}_j)} \right)_c = \frac{\exp(\textbf{z}_c+\log R)}{\sum_{j=1}^C \exp({\textbf{z}_j + \log R})}$$ A common choice for the constant is $\log R = -\max_{j \in \{1,\ldots,C\}} \textbf{z}_j$.

Another way to deal with numerical problems of softmax is to have a mixture of softmaxes: See (Yang et al, 2018).

Quiz 3: Accurate classification over a large number of classes is essentially an unsolved problem in machine learning. Give it too many classes and the classifier will fail. The issue is not quite a numerical problem with softmax, but rather a more fundamental problem with the loss function. Why?

A: (TL;DR) For large-scale classification, standard losses treat all errors as equally severe. By incorporating class hierarchies or semantic embeddings, we align the loss function with real-world relationships between labels, improving both model accuracy and computational efficiency.

(Details) As the number of classes $C$ grows large (e.g., in extreme multi-label classification), discrete class labels begin to resemble a continuous space. In such cases, we must account for semantic relationships between classes: predicting "wolf" when the true label is "dog" should incur less loss than predicting "elephant". This requires modeling similarity among classes rather than treating all misclassifications equally.

Limitation of Standard Loss Functions¶

The standard negative log-likelihood (NLL) (cross-entropy) loss for multi-class classification is:
$$ \mathcal{L}(\mathcal{D}) = -\sum_{i=1}^n \sum_{c=1}^C \textbf{1}\{y_i = c\} \log (\text{softmax}(\textbf{W}\textbf{x}_i + \textbf{b})_c). $$
This loss penalizes all misclassifications equally: the error for predicting "wolf" when $y_i = \text{"dog"}$ is identical to predicting "elephant". For large $C$, this ignores class relationships, reducing model efficiency and interpretability.

Solutions: Incorporating Class Relationships¶

To address this, we can modify the loss or output structure to reflect class similarity. Two key approaches are:

  1. Hierarchical Softmax (e.g., Babbar & Schölkopf, 2017)
    Organize classes into a hierarchy (e.g., taxonomy of animals). Instead of predicting one of $C$ classes directly, the model predicts paths from the root to the target class in this tree. This:

    • Reduces computation complexity from $\mathcal{O}(C)$ to $\mathcal{O}(\log C)$,
    • Naturally encodes class relationships (e.g., "dog" and "wolf" share a subtree).

    The main challenge here is that the notion of "similarity" is context-dependent, which is hard to capture in the loss.

  2. Low-Dimensional Projections for Sparsity
    Map classes to a low-dimensional latent space where proximity reflects semantic similarity:
    $$ \text{softmax}(\textbf{W}\textbf{x}_i + \textbf{b})_c \approx \sigma\left( \langle \mathbf{v}_y, \mathbf{u}_{x_i} \rangle \right), $$
    where $\mathbf{v}_y$ and $\mathbf{u}_{x_i}$ are embeddings of the class $y$ and input $\mathbf{x}_i$. This forces:

    • Rare classes (e.g., "dog", "wolf") to cluster in the embedding space,
    • Similar predictions for semantically close classes.
In [2]:
def stable_softmax(z):
    z_new = z - max(z)
    exp_z = np.exp(z_new)
    partition = np.sum(exp_z)
    softmax = exp_z/partition
    return softmax

References¶

  • Ng, Andrew Y., and Michael I. Jordan. "On discriminative vs. generative classifiers: A comparison of logistic regression and naive Bayes.", NIPS, 2002.
  • Shimodaira, Hidetoshi. "Improving predictive inference under covariate shift by weighting the log-likelihood function." Journal of statistical planning and inference 90.2 (2000): 227-244.
  • Zhang, K., Schölkopf, B., Muandet, K., & Wang, Z. (2013). Domain Adaptation under Target and Conditional Shift. In ICML. Retrieved from http://proceedings.mlr.press/v28/zhang13d.pdf
  • Zhang, K., Gong, M., & Schölkopf, B. (2015). Multi-Source Domain Adaptation: A Causal View. In AAAI. Retrieved from https://www.aaai.org/ocs/index.php/AAAI/AAAI15/paper/viewFile/10052/9994
  • Long, M., Zhu, H., Wang, J., & Jordan, M. I. (2016). Unsupervised Domain Adaptation with Residual Transfer Networks. In NIPS. Retrieved from http://papers.nips.cc/paper/6110-unsupervised-domain-adaptation-with-residual-transfer-networks
  • Zhilin Yang, Zihang Dai, Ruslan Salakhutdinov, William W. Cohen Breaking the softmax bottleneck: A high-rank RNN language model, ICLR (2018).
  • Kallenberg, O. (2006). Foundations of modern probability. Springer Science & Business Media.
  • Austin, Tim. "On exchangeable random variables and the statistics of large graphs and hypergraphs." Probability Surveys 5 (2008): 80-145.
  • Babbar, Rohit, and Bernhard Schölkopf. "Dismec: Distributed sparse machines for extreme multi-label classification." In Proceedings of the Tenth ACM International Conference on Web Search and Data Mining, pp. 721-729. 2017.