71 min total

Generative AI

Normalizing flows

Normalizing flows
GenAI / Mid 02 — Normalizing flows — notes

What does determinants even represents ?

Determinants were originally created to solve systems of linear equations, Leibniz, Cramer, Laplace developed them as a way to determine whether system had a unique solution, infinitely many solutions or no solution at all

Normalizing Flows ~ (2015 Shakir mohamed)

Are a category of generative models that allows you to transform a simple probability distribution into a complex probability distribution using invertible mappings. Due to invertible nature of model, you can go back and forth between the data space and latent space useful to latent variable inference. NICE models were inspiration to normalizing flows models. Normalizing flow models have tractable likelihood, meaning probability density can be computed exactly and accurately for any data point unlike some VAEs (intractable likelihoods) where integrals/sums cannot be computed exactly so approximations are used

Cramer’s Rule, which solves systems of linear equations, relies directly on the determinant. If the determinant is zero, the system has either no solution or infinitely many solutions

But Solutions to what?

Solution to solve a system of n linear equations with n unknowns

A is a nxn square matrix containing the coefficients of the system

x is the unknown vector

b is the constants

Probability Conservation law
A valid probability density function must have a total probability of 1 when integrated over its entire domain.

The solution (x) can be

Infinitely many solutions

System has no solution

System has a specific solution

Invertible mapping/function
you can uniquely determine the original input from the output. If we apply a function to get y, the inverse function can recover x from y

Ok but what else?

Overtime we realized determinant's relation to Linear systems like

→ can be used to determine matrix invertibility

→ determinants absolute value tells how much the transformation scales volume, to transform space b, we used a transformation matrix a, det(A) can tell how much transformation scales volume. Det(A) = 2, transformation would double the volume.

If we have two random variables

x from space X with a probability distribution

z from space Z with a probability distribution

Space: all set of possible values for a random variable

Input to is a data point a from space A and the data point can be multidimensional depending on our model's parameter count. Output is a single real number - a probability density value which indicates how likely the point a under the model is.

→ if det(A) > 0, no flipping on b, det(A) < 0, reflection occurs

→ and much more …

function f takes x from original space and maps it to z in a new space

Because f is invertible, there exists a function such that

Since f is invertible, every x corresponds exactly to one z, and vice versa.

In normalizing flows, goal is to learn the transformation function f so that it maps data point x from a distribution X to distribution Z correctly.

Probability mass function value at X=3 (discrete value)?

Probability distribution is as follows:-

x12345
p(x)0.10.20.40.20.1

Changes of variables equation:

P(X=3) = 0.4

The value of the PMF is 0.4, meaning there's a 40% chance that the random variable X will be 3.

: this computes likelihood of z under the initial distribution f(x)

: this is a matrix of all first-order partial derivatives of f,

Probability density value at X=3 (continuous value)?

Probability distribution is as follows:-

the probability of the variable taking on any exact value between 2 and 3 is 0.1353

this matrix captures how small changes in x affect changes in initial distribution f(x)

We take abs value to ensure density remains positive

GenAI / Mid 02 — Normalizing flows
  1. We initialize the f(x) as a normal distribution initially
  2. We compute f(x) to get transformed x value
  3. We compute to get the probability distribution value of the transformed value (transformed value is often reffered to as the new z value).
  4. We compute volume correction factor to account for change in volume due to transformation, if function f streches the space, probability density logicaly should drop because same amount of probability is now spread over a large region, if function f compressed the space, the probability density is logically increased now, multiplying with the determinent can cancel out the variations in probability distribution due to streching or shrinking.
  5. The output of whole computation is the exact likelihood of x under current model, the higher the , the better model fits the data

⊖ Determinant of high dimensional matrix is computationally expensive

⊖ we can use some neural network to find function f(x) but we don’t know yet how to invert this network.

Real Valued Non-Volume Preserving (RealNVP) Model

Is a type of normalizing flow generative model that transforms a simple base distribution into complex data distribution

Key problem with normalizing flows was to make transformation computationally efficient. Computing full jacobian determinant of arbitrary functions is expensive, so RealNVP uses a coupling layer, where only part of the input is transformed while other part remains unchanged.

Two streams are created

  1. Scaling stream - Stack of fully connected layers, final layer outputs a tensor size of the input dimension and uses tanh activation function
  2. Translation stream - Stack of fully connected layers, final layer outputs a tensor of input dimension with linear activation

Coupling layer

Termed as coupling as this transformation, one part of the input remains unchanged while the other part is transformed based on the unchanged part, and then two parts are coupled together

GenAI / Mid 02 — Normalizing flows

Jacobian matrix (J) contains all partial

derivatives of output z wrt input x

So for each input x with D dimensions

We break the input into two chunks and

s : scaling neural network

t : translation neural network

GenAI / Mid 02 — Normalizing flows
GenAI / Mid 02 — Normalizing flows

Over here the input x contains 8 dimensions

Structure of Jacobian matrix

  1. Top-Left block: since , derivative of each element is 1 and 0 for different indices, thus block is the identity matrix
  2. Top-Right block: because values do not rely on at all, all these partial derivatives are 0
  3. Bottom-Left block: we compute partial derivatives of exp(s( )) and t( ) functions by applying chain rule and getting a long expression. We get non-zero derivatives populated in this region
  4. Bottom-right block: since , the and t( terms would not participate in outputting any z value. Since its differentiating it would result in a

The output of only depends on therefore when j != i differenciation gets you 0

In summary, Jacobian matrix structure:

GenAI / Mid 02 — Normalizing flows

But why all the coupling hassle?

GenAI / Mid 02 — Normalizing flows

Determinant of a lower triangular matrix is product of its diagonal elements 😂. New Jacobian Complexity →

How is it tractable?

GenAI / Mid 02 — Normalizing flows

Mapping function:

GenAI / Mid 02 — Normalizing flows

Inverse function:

GenAI / Mid 02 — Normalizing flows

How to stack Coupling layers

Since some elements of transformed variable remain unchanged, as coupling only transforms a part of input at a time, we stack multiple coupling layers while alternating masking to increase expressiveness

GenAI / Mid 02 — Normalizing flows

GLOW ~ (2018 same VAE's researcher)

(Generative flow with Invertible 1x1 convolutions)

It optimizes RealNVP one step further by making invertible 1x1 convolution instead of fixed masking patterns to reduce computations and improves image manipulation. Will go into detail in future rightnow not the most optimal time :)

FFJORD ~ (2019 Will Grathwohl UoT)

(Free-Form Continuous Dynamics for scalable reversible generative models). Instead of discrete transformations, FFJORD expresses transformations in continuous manner. Transformations previously were rigid and predefined - FFJORD lets the data evolved continuously over time.

GenAI / Mid 02 — Normalizing flows