Why T > 1 Breaks Sigmoid Distillation

This started as a practical production problem. I needed to migrate a multi-label Locked-Image Tuning (LiT) classifier head from a pretrained SigLIP 2 backbone onto a fine-tuned SigLIP 2 checkpoint already indexing images in production, so distilling the old head into a new one on top of the frozen production backbone was the cleanest way to avoid retraining from scratch. Following Hinton et al. (2015), I started with the standard distillation temperature $T = 4.0$: in Softmax distillation, raising $T$ exposes the teacher’s uncertainty across competing classes, giving the student richer signal than hard labels. Because each image could have multiple labels at once, the classifier used independent Sigmoid outputs with Binary Cross-Entropy (BCE) loss, and distilling with $T = 4.0$ hurt performance every time instead of helping. ...

November 25, 2026 · 17 min · Akash Verma

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 Starting with a sentence, “The quick brown fox jumps over the lazy dog near the riverbank in the sunny park.” Tokenisation of the following can be: ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog", "near", "the", "riverbank", "in", "the", "sunny", "park"] ...

August 21, 2024 · 3 min · Akash Verma

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 · 7 min · Akash Verma

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 · Akash Verma

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 · 8 min · Akash Verma

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 · Akash Verma

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 · Akash Verma

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 · Akash Verma

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 · 6 min · Akash Verma

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. 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 · 8 min · Akash Verma

Parameter updates

Once the gradient is computed, it is then used to update the parameters. There are several approaches to perform the update. 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 · Akash Verma

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 · Akash Verma

Batch Normalization

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). D = torch.randn(1000, 500) Y = torch.randn(1000, 1) * 0.1 And we have 10 layers each with 500 * 500 neurons. ...

March 10, 2024 · 9 min · Akash Verma

Dead Neurons

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: n = 3 #trigram model emb_sz = 2 #latent factor n_hidden = 100 vocab_sz = len(chars) # it's 27 Here is the architecture: Fig. 1. Neural probabilistic language model architecture (source: Bengio et al., 2003). C = torch.randn(vocab_sz, emb_sz) w1 = torch.randn(n*emb_sz, n_hidden) b1 = torch.randn(n_hidden) w2 = torch.randn(n_hidden, vocab_sz) b2 = torch.randn(vocab_sz) # Setting up gradient requirements parameters = [C, w1, b1, w2, b2] for p in parameters: p.requires_grad = True # --------------------------------------------------- # training epoch = 200_000 bs = 32 # batch sz for i in range(epoch): # mini batch idx = torch.randint(0, xtrain.shape[0], size=(bs,)) xbs, ybs = xtrain[idx], ytrain[idx] # batch # forward pass emb = C[xbs] hpreact = emb.view(emb.shape[0], -1) @ w1 + b1 h = torch.tanh(hpreact) logits = h @ w2 + b2 loss = F.cross_entropy(logits, ybs) perplexity = torch.exp(loss) # backward pass for p in parameters: p.grad = None loss.backward() # update lr = 0.01 if i < 100_000 else 0.001 for p in parameters: p.data -= p.grad.data * lr # track lossi.append(loss.log10().item()) if i%10000 == 0: print(f"epoch: {i:7d}/{epoch:7d} | loss: {loss.item():.4f} | perplexity: {perplexity.item():.4f}") epoch: 0/ 200 | loss: 9.9113 | perplexity: 20157.0859 epoch: 10/ 200 | loss: 4.8961 | perplexity: 133.7642 epoch: 20/ 200 | loss: 3.5018 | perplexity: 33.1759 epoch: 30/ 200 | loss: 3.0638 | perplexity: 21.4093 epoch: 40/ 200 | loss: 2.8681 | perplexity: 17.6032 epoch: 50/ 200 | loss: 2.8150 | perplexity: 16.6932 Firstly, I want to explain why we need to initialize the weights of our model with good values. As observed, the initial loss is very high, and then subsequent losses are very low. The weights we initialized are significantly incorrect, as some are very confidently wrong while others can be overly confident. This necessitates spending time and computational resources to correct them before starting unbiased learning. Thus, improving parameter initialization can help us skip this unnecessary step, which otherwise wastes energy and computation. ...

March 4, 2024 · 8 min · Akash Verma

Gated Recurrent Unit (GRU)

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 · Akash Verma

Long Short-Term Memory (LSTMs)

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. The forget gate modulates what information gets discarded from the cell state. The input gate decides what new information gets stored in the cell state. And the output gate decides what information propagates to the next steps. ...

February 7, 2024 · 12 min · Akash Verma

Stateful Recurrent Neural Network

If we have data like: (from the example of previous blog) X – Y 'one', '.', 'two' – '.' '.', 'three', '.' – 'four' Where 3 words are used as input to predict 1 word from a vocabulary as an output, we can create a neural network architecture that takes three words as input and returns a prediction of the probability for each possible next word in the vocabulary. We will use three standard linear layers but with two tweaks! ...

February 6, 2024 · 13 min · Akash Verma

Tokenize & Numericalize

Here are the main steps for language modeling: Tokenization– Converting Text into list of words (creating the vocab) Numericalization– Converting each word in vocab to number, by replacing them with their indices (simple!) The next steps are: Language Model Data Creation (X&Y) and Language Model Creation Jargon: Token One element of a list created by the tokenization process. It could be a word, part of a word (a subword), or a single character. ...

February 5, 2024 · 8 min · Akash Verma

Self-Supervised & Transfer Learning in Language Models

A language model is a model that is trained to guess the next word in a text (having read the ones before). This kind of task is called self-supervised learning. Jargon: self supervised learning Training a model using labels that are embedded in the independent variable, rather than requiring external labels. Self supervised learning is not usually used for the model that is trained directly, but instead is used for pretraining a model used for transfer learning. Self supervised learning is used to train a base model, and that base model is used to train different model for specific task like text classification!1 ...

February 3, 2024 · 5 min · Akash Verma

Embeddings in sequential Neural Network

Alright who cares about the follow up blog, here is a notion notes to save me. This is the notes for tweaking Random forest on a tabular dataset to squeeze some performance, I tried this on a Kaggle compe and it worked out pretty badly. Score of something like 2.xx. Maybe I Will improve it when time comes. zzz As we already initialized the embeddings of our users and products for example, We take the result of the embedding lookup and concatenate those activations together. This gives us a matrix that we can then pass through linear layers and nonlinearities in the usual way. ...

February 1, 2024 · 3 min · Akash Verma

Embeddings in Recommendation Systems

I will write a follow up blog about Regression and Random Forest, today completes the half of the first month of 2024, I learned lots of stuffs that I am interested and participated on a Kaggle compe, predicing Energy consumption and production using solar panels, just to try what I learned and now I am one step closer to understanding the “Attention”. I will continue this journey and still be writing this blog for future me like creating notes, maybe in more readable form. ...

January 15, 2024 · 10 min · Akash Verma

Cross Entropy in Classification

Multi label classification refers to the problem of identifying the categories of objects in images that may not contain exactly one type of object. So each data can have either single or multiple label(s). Example, a image has car, bicycle, person, tree. Why we cant use softmax and NLL loss? Softmax outputs values that sum to 1, and because the use of exp it tends to push on activation to be much larger than the others. ...

December 22, 2023 · 4 min · Akash Verma

Logarithms in Deep Learning

To learn the foundation very clearly, I coded MLP, from scratch and trained MNIST dataset. (it was a 3 vs 7 model, a binary classifier). For that I used a Linear function in each neuron, and Relu as activation. and for the final layer I used Sigmoid. I wanted to expand this model from just a binary classifier to multi-class classifier (where each instance belongs to one and only one class) I learned about Softmax activation that can be used in the final layer and then creating a loss function for MNIST model. ...

December 11, 2023 · 8 min · Akash Verma

Algorithm behind universal function approximator

A artificial neural network can learn (almost) anything, and so its called a universal function approximator. To understand how it works, we need to know function. Function, let’s say f(x) is just a system of inputs and outputs, a number in, a number out. x -> f(x) -> y We give a input x, and it outputs y. We can plot all the functions on a graph, where it gives an output for an input. What is important is, if you know a function you can always calculate the output (y) for a given input (x). ...

November 4, 2023 · 6 min · Akash Verma

Traffic on blocked ports

While there are many blogs and tutorials available on accessing a Virtual Cloud Network in a VM instance, such as this comprehensive guide(by oracle). Some of the VM instances I’m working with are not vanilla; they come with the distributor’s packet blockers and they block traffics on all the ports. I worked around Iptables a default firewall for linux, I think. It is used to set up, maintain, and inspect the tables of IP packet filter rules in the Linux kernel. Several different tables may be defined. Each table contains a number of built-in chains and may also contain user-defined chains. ...

September 30, 2023 · 3 min · Akash Verma

I relearned entropy

I learned about black hole, how are they possible? (just started). On the sunday morning, I started reading a paper about BBC Reith Lecture. It all started with Albert Einstein writing a paper in 1939 claiming that stars could not collaspe under gravity because matter could not be compressed beyond a certain point, many scientist thought the same but an American scientist John Wheeler, who in many ways is the hero of the black hole story. In his work in 1950s and 1960s, emphasized that many stars would eventually collapse and pointed out problems that possibility posed for theoretical physics. Durring most of the life of a normal star, over billons of years, it will support itself against its own gravity by thermal pressure caused by nuclear processes which convert hydrogen into helium. ...

September 15, 2023 · 3 min · Akash Verma