Global Attention

To understand how global attention works quickly, take a long sentence, tokenize it, and then compare how the original attention mechanism (using Key-Value-Query, or KQV) works on the tokens versus how it operates with global tokens derived from blocks of those tokens. Step 1: Original Sentence and Tokenization Sentence: “The quick brown fox jumps over the lazy dog near the riverbank in the sunny park.” Tokenization: Tokens: ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog", "near", "the", "riverbank", "in", "the", "sunny", "park"] Step 2: KQV Attention Mechanism KQV Calculation Input Tokens: ...

August 21, 2024 · 3 min · akash5100

Creating simple RAG

Life update, last week I joined Plane.so as AI engineer let’s see if I can make it up to AI researcher! This is the paper which introduced RAG. The problem with LLMs is that they “hallucinate” and have fixed world knowledge, bleh… we already know that RAG architecture tries to address it. My main motive to create RAG is to understand vector embeddings more in-depth and how we create it, search it and what happens next if we find those retieved documents? ...

June 22, 2024 · 6 min · akash5100

Interpretable Features from Neural Networks

I watched a podcast of Jensen and Ilya, in which Ilya talked about how multimodality enables neural networks to learn more features than just a single modality. For example, large language models like GPT-4, without vision, can recognize that the color pink is close to red, but they can’t explain why, because they haven’t seen a single pixel. Multimodality combines image and text, both trained in a unified way. And GPT-4 with vision can tell exactly which pixel is red and why. To achieve intelligence smarter than human-level intelligence, we will definitely need multimodality, because our world is very visual, and neural networks can learn a lot from it. ...

June 3, 2024 · 8 min · akash5100

CNNs

This is a quick skim notes for CS231n Introduction to CNN lecture 7, I used slides from this lecture to create notes and this is NOT an attempt to replicate notes by cs231n, its already the best notes on CNNs out there. CNNs are similar to ordinary neural networks, they have trainable weights and bias, receives input, bunch of trainable layers followed with non-linearity. Each layer is completely differentiable, means they can learn. At the end an output layer predicting classes or so. ...

May 21, 2024 · 7 min · akash5100

Gradient-Checkpointing

You got a very deep neural network to train, lets say wide 128-layers. Does it fits in memory? Yes (barely). The activations and gradients in forward and backward pass respectively takes a lot of memory. But you want to train more deep NN. Why? because, we build the compute (stack more layer) ~= win. (scaling hypothesis) / Blessing of scaling. See the original gradient checkpointing implementation.1 Here is a visualization of vanilla training: ...

May 16, 2024 · 2 min · akash5100

Case Study on Transformer-based architecture

work in progress. publishing this but will be updated every few days, as I learn new stuffs. learning > blog I am learning how the transformer-based architectures got evolved from 2017 (“Attention is all you need”) till today (May 2024). So, I thought why not write a case-study like this (by Andrej Karpathy). Table of content How can unsupervised learning work? GPT (June 2018) BERT (October 2018) Transformer-XL (September 2018) & XLNet (June 2019) GPT-2 (February 2019) Sparse Transformers (April 2019) How can unsupervised learning work? Why it works? Generalization?, hypothetically… ...

May 9, 2024 · 11 min · akash5100

Key-Value caching

Long story short, Key value caching is used to reduce the inference speed of AR models (autoregressive / decoder-only). It does so by caching the already computed attention scores (K and V) of previous 1– t-1 tokens. While the generated token is t. Inference refers to the process of using a trained model to make predictions or generate output for new, unseen input data. In other words, it’s the process of applying the model to real-world data to get a result or prediction. ...

May 6, 2024 · 3 min · akash5100

I challenged myself to visualize attentions (nothing special)

nothing very special as I expected, because it was just a heat map of embeddings, next time, something better for sure. I aimed to create visualization like this research paper or by Andrej Karpathy. I started with creating vocabulary for my model using BPE (Byte Pair algorithm). Hyperparameter for BPE merges I used is 2000-256, it would create vocab of size 2000. How? recall your learning and solve that mystery. Before running BPE on ‘Elvis Presley’ wiki page (because it was very long, no other reason), I split them into words using the Llama3 tokenizer Regex: ...

May 4, 2024 · 5 min · akash5100

Transformers

Back in 2017, the deep learning folks were trying to achieve state-of-the-art performance in sequence to sequence modeling. They used RNN’s like LSTM and GRU. The best performing model connects the encoder and decoder through an attention mechanism. Where the encoder is used to encode some sequence into rich embeddings and decoder decodes that embedding to different sequence. Example, language translation. Table of contents Sequence to sequence modeling and limitations of RNNs Transformer Model Architecture Attention Types of attention function Why scaling Multihead attention Auto-regression mask for self-attention Head (Masked-attention) Feed Forward (Linear transformation) Input Embeddings (Vocab and Positional) Regularizations used in the paper Transformer Architectures Encoder-Decoder Encoder only Decoder only Sources Sequence to sequence modeling and limitations of RNNs RNN had the vanishing gradient problem, models like LSTM and GRU emerged to tackle this problem but realized that altho the text may look better than rubbish, its still rubbish. It may look like English, but jumbled words and the sentence has no meaning. ...

April 28, 2024 · 9 min · akash5100

Parameter updates

Once the gradient is computed, it is then used to update the parameters. There are several approaches to perform the update. Table of contents First order(SGD), momentum, nesterov momentum Stochastic gradient descent Momentum Update Nesterov Momentum (NAG) Per-parameter adaptive learning rates (Adagrad, RMSProp) Adagrad RMSProp (Gef. Hinton) Adam AdamW AdamScheduleFree Second order methods Sources First order(SGD), momentum, nesterov momentum Stochastic gradient descent This is the vanilla update, the simplest form of update that changes the parameters along the negative gradient direction, as the gradient indicates the direction of increase, whereas we usually wish to minimize a loss. Assuming a vector of parameters x and the gradient dx, the simplest form of the update is as follows. ...

April 12, 2024 · 8 min · akash5100

Understanding loss.backward()

Relying on any library is not good; therefore, this time, removing this black box and understand what’s happening inside. Writing a backward pass manually would be helpful. Backpropagation is not something that works magically or automatically if you are hoping to debug. In other words, it is easy to fall into the trap of abstracting away the learning process, believing that you can simply stack arbitrary layers together and backprop will magically make them work on your data. So let’s look at a few explicit examples where this is not the case in quite unintuitive ways. ...

April 2, 2024 · 14 min · akash5100

Batch Normalization

Table of contents Strange output of coupling Source Understanding why training deep neural networks can be fragile is crucial. Issues like dead neurons or saturation of non-linearity, and the vanishing or exploding gradients have caused problem for deep learning for years. However, there’s a beacon of hope that emerged around 2015: Batch Normalization. Here is a visualization of problem using graph. Say we have 1000 datapoints and each has 500 embeddings (latent factors). ...

March 10, 2024 · 9 min · akash5100

Dead Neurons

Table of contents Peeking inside a hidden layer But how can we decide which number to multiply? (Initialization) Summary I implemented some techniques used in a research paper for n-gram language modeling. It is a simple MLP-based trigram language model that takes three characters’ tokens as input, passes them into an embedding layer, then a hidden linear layer, and finally predicts the next character’s token. Here are the hyperparameters I used: ...

March 4, 2024 · 8 min · akash5100

Gated Recurrent Unit (GRU)

Table of contents Reset Gate Update Gate Memory Cell Final Memory Summary Sources GRU was first proposed in 2014 (RNN was in 1986, LSTM in 1995). GRU raised the question of whether we need to be that flexible like LSTM to learn the sequence. GRU is less flexible than LSTM but it is good enough for sequence learning. GRU redesigned the LSTM cell by introducing reset gate, update gate, and new memory cell; therefore, the number of gates were reduced from four to three. It was empirically shown in (Chung et al., 2014) that the performance of LSTM improves by using GRU cells. Later in 2017, the GRU was further simplified by merging the reset and update gates into a forget gate (Heck & Salem, 2017). Nowadays, GRU is the most commonly used LSTM structure. ...

February 16, 2024 · 5 min · akash5100

Long Short-Term Memory (LSTMs)

Table of contents Regularizing LSTM 1. Dropout 2. Activation Regularization and Temporal Activation Regularization Gradient Clipping (good to know) Summary LSTM were designed to deal with the issue of exploding or vanishing gradients in RNNs. This is challenging because, neural nets generally struggle to learn long-term dependencies. LSTM introduces gate mechanism and has 2 hidden state, that allows information to flow unmodified over many timesteps. Each LSTM cell has a set of gates (forget, input, cell/memory and out) that carefully regulate the information into and out of the cell. ...

February 7, 2024 · 12 min · akash5100