71 min total

Generative AI

Neural network foundations

Neural network foundations
GenAI / Mid 01 — Neural network foundations — notes

Feed-Forward Neural Networks

Is a multilayer network, sometimes also called multi-layer perceptrons (MLP), units are connected with no cycles. Each layer is fully connected

GenAI / Mid 01 — Neural network foundations

There is a single matrix W for weights of the entire layer for parallel computation

We don’t count the input layer when numbering layers, but do count the output layers

Some notations:

means weight matrix for first hidden layer

means bias vector for first hidden layer

means number of units at layer j

means output from layer I

layer is for inputs reffered to as

means predicted value (

Training Neural Nets

Goal of training is to learn W and b for each layer that make for each training observation as close as possible to true y

  • Error function: computes the difference between and
  • Loss function: Defines how error value is computed. We use different loss function depending upon the task
  • Cost function: average of loss values over the entire dataset

error is the raw difference, and the loss function determines how it is penalized.

'Loss' comes from the idea that each incorrect prediction results in some loss of accuracy

'Cost' comes from the total price we pay for all our model's mistakes

We need to minimize loss and cost terms at all costs

To minimize loss we need better W and b values

To find W and b values that minimize this loss we use optimizer algorithm (gradient descent, RMS prop, Adam)

Optimizer algorithm requires knowing the partial derivative of loss function wtr each parameter.

Back propagation (1986)

This algorithm computes gradients starting from the last layer towards initial layers.

Before computation graphs backpropagation was implemented in manual inefficient way using explicit differentiation, this quickly resulted in very large equations that are hard to compute.

Computation graphs (2015)

Is a data structure that represents mathematical operations in a neural network as a DAG. It enables automatic differentiation making it easy to apply chain rule. Used in Pytorch, tensorflow, JAX to compute gradients automatically via loss.backward()

GenAI / Mid 01 — Neural network foundations

There are many approaches to solving backprop on paper

  1. Element wise backpropagation (DLP) link [Mid1/14Feb]
  2. Matrix based method (Vectorized approach) link [Mid2/Word Embeddings/SkipGram]
  3. Graph based method
  4. Computational block method

In GenAI we are following a mix of a. and b.

GenAI / Mid 01 — Neural network foundations
W140.2
W15-0.3
W240.4
W250.1
W34-0.5
W350.2
W46-0.3
W56-0.2
x11
x20
x31
B60.1
B4-0.4
B50.2
GenAI / Mid 01 — Neural network foundations — notes
GenAI / Mid 01 — Neural network foundations — notes
GenAI / Mid 01 — Neural network foundations — notes

Regularization techniques

Is a technique to make models generalize better, improves model's performance on unseen data. Overfitting is characterized with very large w values.

  1. L2 Regularization (1989 Yann LeCun) [Deep Learning/C2/Week01]

A technique used to reduce the probability of neural network overfitting

Regularization penalizes the weight matrices of the nodes

Ab agar bht bary weights hongay tou loss bhi bht bara hojaega

The additional term penalized large weight values

L2 was introduced first then L1 came later

L1 regularization induces sparsity, some weights become exactly 0 meaning only important features remain. L2 has better convergence abilities since its differentiable at every instance, L1 is non differentiable at 0 causing oscillations but sub-gradient methods allows us to handle non differentiable issue effectively

GenAI / Mid 01 — Neural network foundations
GenAI / Mid 01 — Neural network foundations

L1 not being differentiable at 0 is that traditional gradient-based optimization algorithms (like Gradient Descent) rely on smooth, differentiable functions to compute precise weight updates. When ​ reaches 0, the derivative doesn't exist, so standard gradient descent cannot compute an exact update. This can make optimization unstable or slow, especially in algorithms that strictly require differentiability. Even though it's not differentiable at 0, L1 encourages sparsity by pushing weights to exactly 0.

When dealing with millions of features L1 regularization can automatically eliminate irrelevant features by setting their weights to exactly 0, reduces computational costs if underlying framework handles 0 weights efficiently, makes model more interpretable. Sparsity also speeds up inference latency.

GenAI / Mid 01 — Neural network foundations — notes
  1. Augmentation

A technique used to artificially increase the training dataset by applying transformations like rotations, flips, scaling, noise addition e.t.c to existing data.