2 Deep Learning

2.1 Linear Classification

Given a dataset \( \mathbf {D} = \{(x_i, y_i)\}_{i=1}^n, x_i \in \mathbb {R}^d,\; y_i \in \{\pm 1\},\) a classifier is a function with adjustable parameters \(\theta \), \( \mathbf {f}_{\theta }: \mathbb {R}^d \rightarrow \{\pm 1\}\), that predicts the output label conditioned on \(x_i\). For example, inferring whether the behavior of a given program is malicious is a classification problem, where the label could be \(-1\) for malicious and \(1\) for benign. The goal is to have \(\mathbf {f}_{\theta }(x_i) \approx y_i\).

A linear classifier has a score of the form

\[ \mathbf {s}_{\theta }(x) = w^T x + b \]

Where \(\theta = \{ w,b\}\), \(b\) is the bias that shifts the decision boundary and \(w\) determines the orientation of the decision boundary (given by \(w^T x + b = 0\) ). If \(w^T x + b \geq 0\) then the predicted label is 1 otherwise -1. The classifier is then defined as

\[ \mathbf {f}_{\theta }(x) = \operatorname {sign}(s_{\theta }(x)) = \operatorname {sign}(w^T x + b) = \operatorname {sign}(\tilde {w}^T \tilde {x}) \]

where

\[ \tilde {w} = \begin {pmatrix} w \\ b \end {pmatrix}, \qquad \tilde {x} = \begin {pmatrix} x \\ 1 \end {pmatrix} \]

If a linear classifier classifies the data correctly, then the data is said to be linearly separable. More formally, a dataset is linearly separable if there exist \(w\) and \(b\), i.e., a hyperplane, such that

\[ y_i(w^T x_i + b) > 0 \qquad \forall i \]

The minimum margin, i.e distance to closest sample, is given by:

\[ \min _i y_i \frac {w^T x_i + b}{\|w\|}. \]

Where we divide by \(||w||\) otherwise the projection is scaled by the size of \(w\). We multiply by \(y_i\) so that the margin is always positive for correctly classified samples, regardless of which side of the hyperplane they lie (one can also just use \(\frac {|w^T x_i + b|}{\|w\|} )\). The signed distance to the hyperplane is \(\frac {w^T x_i + b}{\|w\|}\); a negative value indicates that the sample lies on the negative side of the hyperplane. One algorithm for finding such a separating hyperplane is the Perceptron Algorithm or Gradient Ascent.

PIC

One can formulate the search for the maximum margin separating hyperplane as the following constrained optimization problem:

\[ \max _{w,b} \left \{ \min _i y_i \frac {w^T x_i+b}{\|w\|} \right \} \qquad \text {such to} \qquad y_i(w^T x_i+b)>0, \quad \forall i \]

Given the produced score \(w^T \mathbf {x} + b\), one can turn it into a proper probability distribution using the sigmoid function:

\[ p_{\theta }(y = 1 \mid \mathbf {x}) = \frac {e^{w^T \mathbf {x}+b}} {1 + e^{w^T \mathbf {x}+b}} = \frac {1} {1 + e^{-(w^T \mathbf {x}+b)}}. \]

which parameterizes the Bernoulli distribution with probability mass \(p_{\theta }(\mathbf {y} \mid \mathbf {x}) = (p_{\theta }(y = 1 \mid \mathbf {x}))^{\frac {1+y}{2}} (1-p_{\theta }(y = 1 \mid \mathbf {x}))^{\frac {1-y}{2}}\). This makes sense because \(p_{\theta }(y = 1 \mid \mathbf {x}) > \frac {1}{2}\) if and only if \(w^Tx + b> 0\).

Now, consider the scenario where we have \(L\) labels, then the classifier produces one score per class, we could write the output as \(s_{\theta }(x) = Wx + b \in \mathbb {R}^{L}\), where \(s_{\theta }(x)\) is commonly referred to as the logits. Here, the input is projected onto \(L\) different weight vectors, each with its own bias and the predicted class corresponds to the index with the highest score i.e \(\arg \max _k (Wx+b)_k\). The classifier is defined as \(\mathbf {f}_{\theta }(x) = \arg \max _{k \in \{1,...,L\}} (Wx + b)_k\). One way to learn the parameters \(W \in \mathbb {R}^{L \times d}\) and \(b \in \mathbb {R}^{L}\) is to use the Multiclass Perceptron algorithm or Gradient Ascent .

Similar to the binary case, one can turn the score function into a proper conditional distribution by using the softmax operation \( p_{\theta }(y=k \mid x) = \operatorname {softmax}(s_{\theta }(x))_k \). It makes sense because the softmax preserves ordering. The probability vector \(\pi = (\pi _1,...,\pi _L)\) where \(\pi _k:= p_{\theta }(y=k \mid x)\) and \(\sum _{k=1}^{L} \pi _k = 1\) parametrizes a categorical distribution \(\text {Cat}(y, \pi = \operatorname {softmax}(Wx+b))\) with the following probability mass function:

\[ p_{\theta }(y \mid x) = \prod _{k=1}^{L} \pi _k^{\mathbf {1}_{y=k}} \]

Now remember, we learned that we can interpret the output of a model as the parameters of a probability distribution. Later, we will see how deep neural networks are trained to learn these parameters so that the resulting conditional distribution approximates the empirical conditional distribution using maximum likelihood estimation.

Linear classification is often insufficient because its decision boundary is restricted to a hyperplane in the original input space. In many practical problems, the target classes are not linearly separable in this space. Several nonlinear transformations may therefore be required to map the input to a representation in which the classes become linearly separable. This is one of the motivations behind deep neural networks.

2.2 Deep Learning

Deep learning consists of learning a sequence of transformations stacked together, where each layer receives the output of the previous layer, applies a linear transformation followed by a nonlinear activation function, and produces an output for the next layer. This is why it is called deep, as many layers are stacked together, and learning, because the parameters of these transformations, typically projection matrices for fully connected layers or kernels for convolutional layers, are learned from the dataset.

Binary Classification

In the field of machine and deep learning, we assume that the dataset \( \mathbb {D} = \{(x_i,y_i)\mid x_i\in \mathbb {X},\,y_i\in \mathbb {Y}\}, \) was generated through a random sampling process following unknown data distribution \(p_{\mathrm {data}}(x,y)\), that is, \((x_i,y_i)\sim p_{\mathrm {data}}(x,y)\). We do not get to know this distribution. However, we can approximate it using the empirical distribution.

The empirical joint distribution is given by

\[ \hat {p}_{\text {data}}(x,y) = \frac {1}{N} \sum _{i=1}^{N} \delta (x-x_i)\,\delta (y-y_i) \]

while the empirical marginal distribution of \(x\) is

\[ \hat {p}_{\text {data}}(x) = \frac {1}{N} \sum _{i=1}^{N} \delta (x-x_i) \]

Recall that in binary classification, the goal is to learn a mapping from the input space \(\mathbb {X}\) to the label space \(\mathbb {Y}\), \( f_\theta (x)\in \mathbb {Y} =\{0,1\} \).

PIC

Instead of learning to map to the label directly, we will instead learn to predict, i.e interpret \(f_\theta (x)\) as \( p_\theta (y=1\mid x)\). The output of the model \(f_\theta (x)\) then parameterizes the Bernoulli distribution \(p_\theta (y\mid x)\):

\[ p_\theta (y\mid x) = p_\theta (y=1\mid x)^y \left (1-p_\theta (y=1\mid x)\right )^{1-y} \]

or if you will

\[ p_\theta (y\mid x) = f_\theta (x)^y \left (1-f_\theta (x)\right )^{1-y}. \]

We want to find \(\theta ^{*} := \arg \min _\theta \mathbb {E}_{(x,y)\sim p_{\mathrm {\text {data}}}(x,y)} \left ( -\log p_\theta (y\mid x) \right ) = \arg \min _\theta H(p_{\text {data}}(y|x),p_{\theta }(y|x)\mathbf | p_\text {data}(x)) \)

That is, the objective function of supervised learning is to estimate the conditional (cross) entropy which measure distance between two densities. In fact, mimnimizing the cross entropy is the same as minimizing the KL divergence, which measures the discrepancy between the true conditional distribution and the model conditional distribution \(p_{\theta }(y|x)\) (as we can drop the parameter independent term), that is, \(\arg \min _\theta H(p_{\text {data}}(y|x),p_{\theta }(y|x)\mathbf | p_\text {data}(x)) = \arg \min _\theta D_{KL}(p_{data}(y|x),p_{\theta }(y|x)\mathbf | p_{\text {data}}(x) )\).

However, we do not know \(p_{\mathrm {data}}(x,y)\), so we will approximate it using the empirical distribution, that is, minimizing the negative conditional log-likelihood of the dataset \(\mathbb {D}\) (empirical cross entropy):

\[ \theta _{\mathrm {MLE}} := \arg \min _\theta \underbrace {\frac {1}{N} \sum _{i=1}^{N} -\log p_\theta (y_i\mid x_i)}_{\mathbf {L}_{\hat {p}_{\text {data}}}(\theta )} \]

By the weak law of large numbers \(\mathbf {L}_{\hat {p}_{\text {data}}}(\theta )\) converges to \(\mathbb {E}_{(x,y)\sim p_{\mathrm {\text {data}}}(x,y)} \left ( -\log p_\theta (y\mid x) \right )\) and thus we can justify using the empirical negative conditional log-likelihood to estimate \(\theta ^{*}\).

Using the Bernoulli probability mass function definition,

\[ \theta _{\mathrm {MLE}} = \arg \min _\theta \frac {1}{N} \sum _{i=1}^{N} - \left ( y_i\log f_\theta (x_i) + (1-y_i)\log \left (1-f_\theta (x_i)\right ) \right ) \]

We can define the loss (which tell us how bad the model is at prediction) as

\[ \mathbf {L}_{\hat {p}_{\text {data}}}(\theta ) = \frac {1}{N} \sum _{i=1}^{N} \underbrace { - \left [ y_i\log f_\theta (x_i) + (1-y_i)\log \left (1-f_\theta (x_i)\right ) \right ] }_{\mathcal {\mathbf l}(f_\theta (x_i),y_i)}. \]

The goal is then

\[ \min _\theta \mathbf {L}_{\hat {p}_{\text {data}}}(\theta ) \]

where

\[ \mathbf {L}_{\hat {p}_{\text {data}}}(\theta ) \rightarrow _{n\rightarrow +\infty } \underbrace {\mathbb {E}_{(x,y)\sim p_{\mathrm {data}}} \left [ \mathcal {\mathbf l}(f_\theta (x),y) \right ]}_{\mathbf {L}_{p_{\text {data}}}(\theta )} \text {(in probability)} \]

Multiclass Classification

For multiclass classification, we parameterize a categorical distribution \(p_\theta (y\mid x)\).

The model produces a vector of scores followed by a softmax, resulting in

\[ f_\theta (x)\in \mathbb {R}^{K} \]

where \(K\) is the number of classes and

\[ (f_\theta (x))_k = p_\theta (y=k\mid x) \]

PIC

The categorical distribution can be written as

\[ p_\theta (y\mid x) = \prod _{k=1}^{K} (f_\theta (x))_k^{\mathbf {1}_{\{y=k\}}}, \qquad y\in \{1,\ldots ,K\} \]

We want to find

\[ \theta ^* := \arg \min _\theta \mathbb {E}_{(x,y)\sim p_{\mathrm {data}}(x,y)} \left ( -\log p_\theta (y\mid x) \right ) \]

As in the binary case, this corresponds to minimizing the conditional cross-entropy

\[ \theta ^* = \arg \min _\theta H\left ( p_{\mathrm {data}}(y\mid x), p_\theta (y\mid x) \mid p_{\mathrm {data}}(x) \right ) \]

Since

\[ \begin {aligned} &H\left ( p_{\mathrm {data}}(y\mid x), p_\theta (y\mid x) \mathbf { | } p_{\mathrm {data}}(x) \right ) \\ &\qquad = H\left ( p_{\mathrm {data}}(y\mid x) \mathbf { | } p_{\mathrm {data}}(x) \right ) + D_{\mathrm {KL}}\left ( p_{\mathrm {data}}(y\mid x) \| p_\theta (y\mid x) \mathbf { | } p_{\mathrm {data}}(x) \right ), \end {aligned} \]

and the first term does not depend on \(\theta \), minimizing the conditional cross-entropy is equivalent to minimizing the conditional KL divergence. However, again we do not know \(p_{\mathrm {data}}(x,y)\), so we approximate it using the empirical distribution. We obtain the MLE

\[ \theta _{\mathrm {MLE}} = \arg \min _\theta \frac {1}{N} \sum _{i=1}^{N} -\log p_\theta (y_i\mid x_i) \]

Using the categorical probability mass function,

\[ \theta _{\mathrm {MLE}} = \arg \min _\theta \frac {1}{N} \sum _{i=1}^{N} \left ( -\sum _{k=1}^{K} \mathbf {1}_{\{y_i=k\}} \log (f_\theta (x_i))_k \right ) \]

For each sample, the inner term can also be written as

\[ -\sum _{k=1}^{K} \mathbf {1}_{\{y_i=k\}} \log (f_\theta (x_i))_k = -\operatorname {onehot}(y_i)^T \log f_\theta (x_i) \]

The loss can then be formulated as

\[ \mathbf {L}_{\hat {p}_{\text {data}}}(\theta ) = \frac {1}{N} \sum _{i=1}^{N} \underbrace { -\operatorname {onehot}(y_i)^T \log f_\theta (x_i) }_{\mathbf l(f_\theta (x_i),y_i)}. \]

For the empirical conditional distribution,

\[ \hat {p}_{\text {data}}(y=k\mid x_i) = \mathbf {1}_{\{y_i=k\}} \]

and therefore we can equivalently write

\[ \theta _{\mathrm {MLE}} = \arg \min _\theta \frac {1}{N} \sum _{i=1}^{N} \left ( -\sum _{k=1}^{K} \hat {p}_{\text {data}}(y=k\mid x_i) \log p_\theta (y=k\mid x_i) \right ) \]

By the weak law of large numbers, for a fixed \(\theta \),

\[ \mathbf {L}_{\hat {p}_{\text {data}}}(\theta ) \xrightarrow [N\to \infty ]{P} \mathbb {E}_{(x,y)\sim p_{\mathrm {data}}(x,y)} \left ( \mathbf l (f_\theta (x),y) \right ) \]

2.3 Discriminative and Generative Modeling

Deep learning models can broadly be divided into two categories: discriminative models and generative models. Discriminative models learn the conditional distribution \(p_\theta (y \mid x)\), where \(x\) denotes the input and \(y\) the corresponding target. The objective is to approximate the unknown data distribution \(p_{\text {data}}(y \mid x)\). For example, in binary classification, the output of the neural network is interpreted as the parameter of a Bernoulli distribution, as covered earlier, \(p_\theta (y=1\mid x)=f_\theta (x)\) where \(f_\theta (\cdot )\) denotes the deep neural network. Given training samples \(\{(x_i,y_i)\}\) assumed to be sampled from the unknown distribution \(p_{\text {data}}(x,y)\), the model is typically trained by minimizing the cross-entropy loss, which is equivalent to maximizing the likelihood of the observed labels or, equivalently, minimizing the KL divergence between the true conditional distribution \(p_{\text {data}}(y\mid x)\) and the model distribution \(p_\theta (y\mid x)\). The empirical cross-entropy converges in probability to the conditional cross-entropy as the number of samples increases (\(n\rightarrow +\infty \)).

Generative models, on the other hand, learn how the data itself is generated by approximating the data distribution \( p_{\text {data}}(x) \). For example, Generative Adversarial Networks or GANs learn an implicit distribution by mapping samples drawn from a simple prior distribution \(p(z)\), typically a standard Gaussian, through a generator network \(G_\theta (z)\). On the other hand, Variational Autoencoders or VAEs explicitly introduce a latent variable \(z\) and model the joint distribution \( p_\theta (x,z)=p_\theta (x\mid z)p(z)\), where \(p(z)\) is a predefined prior, usually a standard Gaussian. The data likelihood is then given by \( p_\theta (x) = \int p_\theta (x\mid z)p(z)\,dz, \) which is generally intractable to compute exactly. Large Language Models or LLMs are also generative models. They learn the joint probability distribution of a sequence of tokens by factorizing it using the chain rule (autoregressive or AR modeling), \( p_{\theta }(x_1,\ldots ,x_n) = p_{\theta }(x_1) \prod _{i=2}^{n} p_{\theta }(x_i\mid x_1,\ldots ,x_{i-1}). \). Training consists of learning each conditional distribution so that the model can accurately predict the next token given all previous ones (which is easier than generating celebrity faces). Once trained, the model can either compute the likelihood of a sequence or generate new text by repeatedly sampling or selecting the next token from the learned conditional distribution.

2.4 The Variational Autoencoder (VAE) framework

In the field of deep learning, we assume that the data was generated from some unknown distribution (i.e there is a data-generating process behind), and we attempt to approximate it using a parametric model \(p_\theta (x)\). We observe random samples \(\{x_i\}_{i=1}^n\) where \(x_i \sim p_{\text {data}}(x)\) (the datapoints are assumed to be iid). The goal of learning is to find \(\theta \) such that \(p_\theta (x) \approx p_{\text {data}}(x)\) i.e., we approximate the underlying data distribution (using the empirical distribution). Once we have learned \(p_\theta (x)\), we can generate artificial or synthetic samples. However, under the manifold hypothesis, \(p_{\text {data}}(x)\) is concentrated on a low-dimensional manifold embedded in a very high-dimensional ambient space. This motivates introducing a lower-dimensional latent space from which the observed data can be generated.

PIC

We learn how to transform a simple fixed distribution in latent space, for example \(p(z)=\text {Normal}(0,I)\), into the data distribution. Rather than learning a mapping as in GANs, we use conditional modeling and learn the conditional density \(p_\theta (x \mid z)\). More precisely, we model

\[ p_\theta (x \mid z) \approx p_{\text {data}}(x \mid z) \]

To estimate \(p_{\text {data}}(x \mid z)\), we use a deep neural network whose output parameterizes the conditional distribution \(p_\theta (x \mid z)\) (i.e., the decoder parameterizes this distribution). We assume that the observed data were generated from latent variables, i.e., pairs \((x,z)\) were generated, but only \(x\) is observed while \(z\) remains hidden.

Since \(x\) and \(z\) are dependent, we can write the marginal likelihood as (assuming the prior is fixed and known)

\[ p_\theta (x) = \int p_\theta (x,z)\,dz = \int p_\theta (x\mid z)p_{\theta }(z)\,dz = \mathbb {E}_{p_{\theta }(z)}\!\left [p_\theta (x\mid z)\right ] \]

Our objective is to learn \(\theta \) such that

\[ p_\theta (x) \approx p_{\text {data}}(x) \]

Unfortunately, \(p_\theta (x)\) intractable (i.e no analytic solution) because \(p_\theta (x\mid z) = \mathcal {N}\!\left (x;f_\theta (z),\sigma ^2 I\right )\) is parameterised using a multi-layer neural-network decoder, making the integral impossible to evaluate analytically. We could use naive Monte Carlo estimation by sampling a large number of latent variables from the prior \(z_k \sim p(z)\) and compute \(p_\theta (x)\) approximately

\[ p_\theta (x) \approx \frac {1}{K}\sum _{k=1}^{K} p_\theta (x\mid z_k), \]

Giving

\[ \log p_\theta (x) \approx \log \left ( \frac {1}{K} \sum _{k=1}^{K} p_\theta (x\mid z_k) \right ) \]

While writing the marginal likelihood as an expected value with respect to the prior distribution seems to solve the problem, it leads to an unstable estimator, as most of the sampled \(z\) values will result in likelihood values close to zero. We are likely to miss, by chance, those samples that would result in high likelihood values, thereby increasing the variance of the estimator (very large random samples are needed) [??]. To see why, imagine drawing latent vectors from the prior. For a given observation \(x\), only a small subset of latent variables \(z\) are likely to explain it well. Consequently,\( p_\theta (x\mid z)\approx 0\) for most sampled \(z\). By Bayes’ rule,

\[ p_\theta (z\mid x) = \frac {p_\theta (x\mid z)p(z)}{p_\theta (x)} \]

this implies that the posterior places almost all of its probability mass on a very small region of the latent space, while assigning nearly zero probability to the rest.

The key idea behind Variational Autoencoder or VAE is therefore to introduce an approximate posterior distribution \(q_\phi (z\mid x)\) to produce latent variables that are likely to assign a high probability to the observation through \(p_\theta (x\mid z)\). This provides an efficient way to identify the regions of latent space that explain the data and serves as the first step toward learning a latent representation that can later be used for generation by sampling from the prior.

PIC

We start from the marginal likelihood

\[ p_\theta (x) = \frac {p_\theta (z,x)}{p_\theta (z\mid x)} \]

and therefore

\[ \log p_\theta (x) = \log p_\theta (z,x) - \log p_\theta (z\mid x) \]

Introducing an arbitrary distribution \(q(z)\), we can write

\[ \log p_\theta (x) = \log p_\theta (z,x) - \log q(z) + \log q(z) - \log p_\theta (z\mid x) \]

or,

\[ \log p_\theta (x) = \log \frac {p_\theta (z,x)}{q(z)} - \log \frac {p_\theta (z\mid x)}{q(z)} \]

Taking the expectation with respect to \(q(z)\),

\[ \mathbb {E}_{q(z)} \left [ \log p_\theta (x) \right ] = \mathbb {E}_{q(z)} \left [ \log \frac {p_\theta (z,x)}{q(z)} \right ] - \mathbb {E}_{q(z)} \left [ \log \frac {p_\theta (z\mid x)}{q(z)} \right ] \]

Since \(\log p_\theta (x)\) does not depend on \(z\),

\[ \log p_\theta (x) = \mathbb {E}_{q(z)} \left [ \log \frac {p_\theta (z,x)}{q(z)} \right ] + D_{\mathrm {KL}} \left ( q(z)\,\|\,p_\theta (z\mid x) \right ) \]

Using

\[ p_\theta (z,x) = p_\theta (x\mid z)p_\theta (z) \]

we obtain

\[ \log p_\theta (x) = \mathbb {E}_{q(z)} \left [ \log \frac { p_\theta (x\mid z)p_\theta (z) }{ q(z) } \right ] + D_{\mathrm {KL}} \left ( q(z)\,\|\,p_\theta (z\mid x) \right ) \]

Expanding the first term gives

\[ \log p_\theta (x) = \mathbb {E}_{q(z)} \left [ \log p_\theta (x\mid z) \right ] + \mathbb {E}_{q(z)} \left [ \log \frac {p_\theta (z)}{q(z)} \right ] + D_{\mathrm {KL}} \left ( q(z)\,\|\,p_\theta (z\mid x) \right ) \]

Since

\[ \mathbb {E}_{q(z)} \left [ \log \frac {p_\theta (z)}{q(z)} \right ] = - D_{\mathrm {KL}} \left ( q(z)\,\|\,p_\theta (z) \right ) \]

we obtain

\[ \log p_\theta (x) = \mathbb {E}_{q(z)} \left [ \log p_\theta (x\mid z) \right ] - D_{\mathrm {KL}} \left ( q(z)\,\|\,p_\theta (z) \right ) + D_{\mathrm {KL}} \left ( q(z)\,\|\,p_\theta (z\mid x) \right ) \]

Therefore,

\[ \log p_\theta (x) \geq \mathbb {E}_{q(z)} \left [ \log p_\theta (x\mid z) \right ] - D_{\mathrm {KL}} \left ( q(z)\,\|\,p_\theta (z) \right ) \]

because

\[ D_{\mathrm {KL}} \left ( q(z)\,\|\,p_\theta (z\mid x) \right ) \geq 0 \]

The term

\[ \mathbf {L}(\theta ,q;x) = \mathbb {E}_{q(z)} \left [ \log p_\theta (x\mid z) \right ] - D_{\mathrm {KL}} \left ( q(z)\,\|\,p_\theta (z) \right ) \]

is known as the Evidence Lower Bound or ELBO for short.

We now set

\[ q(z) = q_\phi (z\mid x) \]

where \(q_\phi (z\mid x)\) is parameterized by an encoder with parameters \(\phi \).

The VAE objective becomes

\[ \mathbf {L}_{\mathrm {VAE}}(\theta ,\phi ) = \mathbb {E}_{x\sim p_{\mathrm {data}}(x)} \left [ \mathbb {E}_{z\sim q_\phi (z\mid x)} \left [ \log p_\theta (x\mid z) \right ] - D_{\mathrm {KL}} \left ( q_\phi (z\mid x) \,\|\, p_\theta (z) \right ) \right ] \]

For the empirical data distribution,

\[ \mathbf {L}_{\mathrm {VAE}}(\theta ,\phi ) = \frac {1}{N} \sum _{i=1}^{N} \left [ \mathbb {E}_{z\sim q_\phi (z\mid x_i)} \left [ \log p_\theta (x_i\mid z) \right ] - D_{\mathrm {KL}} \left ( q_\phi (z\mid x_i) \,\|\, p_\theta (z) \right ) \right ] \]

where the expectation over \(q_\phi (z\mid x_i)\) can be approximated using Monte Carlo sampling.

We assume that \(q_\phi (z\mid x)\) parameterizes a Gaussian distribution.

For a latent space of dimension \(b\),

\[ q_\phi (z\mid x) = q_\phi (z_1, ..., z_b \mid x) = \prod _{k=1}^{b}q_\phi (z_k \mid x) = \prod _{k=1}^{b} \mathcal {N} \left ( z_k; \mu _k(x,\phi ), \sigma _k^2(x,\phi ) \right ) \]

Equivalently,

\[ q_\phi (z\mid x) = \mathcal {N} \left ( z; \mu _\phi (x), \operatorname {diag} \left ( \sigma _\phi ^2(x) \right ) \right ) \]

The encoder therefore outputs

\[ \mu _\phi (x) \qquad \text {and}\qquad \sigma _\phi (x) \]

or, in practice, typically

\[ \mu _\phi (x) \qquad \text {and}\qquad \log \sigma _\phi ^2(x) \]

Assume the prior is

\[ p_\theta (z) = \mathcal {N}(0,I) \]

Then the KL divergence has a closed-form expression:

\[ D_{\mathrm {KL}} \left ( q_\phi (z\mid x) \,\|\, p_\theta (z) \right ) = \frac {1}{2} \sum _{k=1}^{b} \left ( \mu _k^2(x,\phi ) + \sigma _k^2(x,\phi ) - 1 - \log \sigma _k^2(x,\phi ) \right ) \]

Therefore,

\[ - D_{\mathrm {KL}} \left ( q_\phi (z\mid x) \,\|\, p_\theta (z) \right ) = \frac {1}{2} \sum _{k=1}^{b} \left ( 1 + \log \sigma _k^2(x,\phi ) - \mu _k^2(x,\phi ) - \sigma _k^2(x,\phi ) \right ) \]

Hence, the loss can be simplified to

\[ \mathbf {L}_{\mathrm {VAE}}(\theta ,\phi ) = \frac {1}{N} \sum _{i=1}^{N} \left [ \mathbb {E}_{z\sim q_\phi (z\mid x_i)} \left [ \log p_\theta (x_i\mid z) \right ] + \frac {1}{2} \sum _{k=1}^{b} \left ( 1 + \log \sigma _k^2(x_i,\phi ) - \mu _k^2(x_i,\phi ) - \sigma _k^2(x_i,\phi ) \right ) \right ] \]

Now we need to to compute \( \frac {\partial \mathbf {L}}{\partial \theta } \qquad \text {and}\qquad \frac {\partial \mathbf {L}}{\partial \phi }\). Computing the gradient with respect to \(\theta \) is simple because we can use Monte Carlo sampling to eliminate expectation \(\mathbb {E}_{q_\phi (z\mid x_i)}\).

For example,

\[ \frac {\partial \mathbf {L}}{\partial \theta } \approx \frac {1}{N} \sum _{i=1}^{N} \frac {\partial }{\partial \theta } \log p_\theta (x_i\mid z_i), \qquad z_i\sim q_\phi (z\mid x_i) \]

But what about \( \frac {\partial \mathbf {L}}{\partial \phi }\)

This is a little problematic because the distribution from which we sample depends on \(\phi \).

In general,

\[ \mathbb {E}_{q_\phi (z\mid x_i)} \left [ \frac {\partial }{\partial \phi } \log p_\theta (x_i\mid z) \right ] \neq \frac {\partial }{\partial \phi } \mathbb {E}_{q_\phi (z\mid x_i)} \left [ \log p_\theta (x_i\mid z) \right ]. \]

For the second term,

\[ D_{\mathrm {KL}} \left ( q_\phi (z\mid x) \,\|\, p_\theta (z) \right ) \]

the closed-form expression depends explicitly on \(\phi \), and its gradient can be obtained using automatic differentiation.

The remaining problem is therefore how to compute

\[ \frac {\partial }{\partial \phi } \mathbb {E}_{q_\phi (z\mid x)} \left [ \log p_\theta (x\mid z) \right ] \]

Instead, we use the reparameterization trick.

We write

\[ z = \mu _\phi (x) + \sigma _\phi (x)\odot \epsilon , \qquad \epsilon \sim \mathcal {N}(0,I) \]

We can therefore write

\[ z = g_\phi (x,\epsilon ) = \mu _\phi (x) + \sigma _\phi (x)\odot \epsilon \]

Then

\[ \mathbb {E}_{q_\phi (z\mid x)} \left [ f(z) \right ] = \mathbb {E}_{p(\epsilon )} \left [ f\left (g_\phi (x,\epsilon )\right ) \right ] \]

The randomness has now moved from \(z\) to \(\epsilon \), whose distribution \( p(\epsilon )=\mathcal {N}(0,I)\) does not depend on \(\phi \). Consequently,

\[ \frac {\partial }{\partial \phi } \mathbb {E}_{q_\phi (z\mid x)} \left [ \log p_\theta (x\mid z) \right ] = \frac {\partial }{\partial \phi } \mathbb {E}_{p(\epsilon )} \left [ \log p_\theta \left ( x\mid g_\phi (x,\epsilon ) \right ) \right ] \]

Since \(p(\epsilon )\) does not depend on \(\phi \), we can move the derivative inside the expectation:

\[ \frac {\partial }{\partial \phi } \mathbb {E}_{q_\phi (z\mid x)} \left [ \log p_\theta (x\mid z) \right ] = \mathbb {E}_{p(\epsilon )} \left [ \frac {\partial }{\partial \phi } \log p_\theta \left ( x\mid \mu _\phi (x) + \sigma _\phi (x)\odot \epsilon \right ) \right ] \]

We can now use Monte Carlo sampling. Observe that \(p_\theta (x\mid z)\) depends on \(\phi \) through \( z = \mu _\phi (x) + \sigma _\phi (x)\odot \epsilon \). For example, if the decoder distribution \(p_\theta (x\mid z)\) is Gaussian, the reconstruction term can correspond to an \(L_2\) loss.

We can therefore write the VAE objective as

\[ \mathbf {L}_{\mathrm {VAE}}(\theta ,\phi ) \approx \frac {1}{N} \sum _{i=1}^{N} \left [ \mathbb {E}_{p(\epsilon )} \left [ \log p_\theta \left ( x_i \mid \mu _\phi (x_i) + \sigma _\phi (x_i)\odot \epsilon \right ) \right ] + \frac {1}{2} \sum _{k=1}^{b} \left ( 1 + \log \sigma _k^2(x_i,\phi ) - \mu _k^2(x_i,\phi ) - \sigma _k^2(x_i,\phi ) \right ) \right ] \]

Using a single Monte Carlo sample

\[ \epsilon _i\sim \mathcal {N}(0,I) \]

we obtain

\[ z_i = \mu _\phi (x_i) + \sigma _\phi (x_i)\odot \epsilon _i \]

and the objective becomes

\[ \mathbf {L}_{\mathrm {VAE}}(\theta ,\phi ) = \frac {1}{N} \sum _{i=1}^{N} \left [ \log p_\theta (x_i\mid z_i) + \frac {1}{2} \sum _{k=1}^{b} \left ( 1 + \log \sigma _k^2(x_i,\phi ) - \mu _k^2(x_i,\phi ) - \sigma _k^2(x_i,\phi ) \right ) \right ] \]

This objective is then trained using SGD.

2.5 The Generative Adversarial Network (GAN) Framework

GANs, or Generative Adversarial Networks (and many other generative and machine learning models like diffusion models or autoencoders) are motivated by the manifold hypothesis concept, which states that the data of interest (e.g natural images or celebrity faces) form a thin d-dimensional submanifold \(\mathbb {M}\) embedded in a high-dimensional ambient space \(\mathbb {X}\) (e.g \(\mathbb {R}^D\), where \(d<D\); d is known as intrinsic dimension while \(D\) is the ambient dimension).

GAN tries to learn a mapping from a given latent space \(\mathbb {Z}\) to \(\mathbb {M}\) and then samples from \(\mathbb {M}\) by sampling from \(\mathbb {Z}\).

In GAN, there is no explicit likelihood maximization as in a VAE (i.e it is not a likelihood-based model). Instead, there is a generator \(g_\theta (z)\) that generates a candidate sample \(x_{\text {fake}} = g_\theta (z)\), where \(g_\theta \) is a deep neural network.

PIC

The generator is trained to fool a discriminator (binary classifier) \(d_\phi \) in an adversarial fashion. The discriminator, represented by \(d_\phi \), outputs the probability that a sample is real. The goal of the discriminator is to maximize \(\log d_\phi (x)\) for real samples \(x \sim p_{\text {data}}\), and to maximize \(\log \left (1-d_\phi (g_\theta (z))\right )\) for fake samples \(z \sim p(z)\).

The loss is the standard binary cross-entropy loss (i.e., similar to training a binary classifier) and is given by (which is equivalent to minimizing the Jensen-Shannon divergence)

\[ \min _\theta \max _\phi \mathbf {L}^{p_{\text {data}}}_{\text {GAN}}(\theta ,\phi ) = \mathbb {E}_{x\sim p_{\text {data}}} \left [ \log d_\phi (x) \right ] + \mathbb {E}_{z\sim p(z)} \left [ \log \left ( 1-d_\phi (g_\theta (z)) \right ) \right ] \]

As usual, we do not have access to the true data distribution, and thus we approximate it using the empirical distribution:

\[ \min _\theta \max _\phi \mathbf {L}_{\text {GAN}}^{\hat {p}_{\text {data}}}(\theta ,\phi ) = \frac {1}{n}\sum _{i=1}^{n} \left [ \log d_\phi (x_i) \right ] + \mathbb {E}_{z\sim p(z)} \left [ \log \left ( 1-d_\phi (g_\theta (z)) \right ) \right ] \]

The term \(\mathbb {E}_{z\sim p(z)}\) is approximated using Monte Carlo sampling. During SGD training, when training the discriminator, the generator parameters are kept fixed, i.e., the generator does not learn. Similarly, when training the generator, the discriminator parameters are kept fixed. With PyTorch, this alternating training procedure is straightforward to implement.

Getting a GAN to work can be difficult due to vanishing gradients, non-convergence, instability of the adversarial equilibrium, and especially mode collapse. Mode collapse can happen when the generator learns a distribution concentrated on only a few modes or examples instead of approximating the full data distribution.