Chapter 4.4 - normalization
What is Normalization
we total the sum to 1 for understanding importance of each word wrt journey. This is called ==attention weights==
Suppose we take the attention scores calculated previously for the word "journey":
import torch
attn_scores = torch.tensor([0.9544, 1.4950, 1.4754, 0.8434, 0.7070, 1.0865])
The Naive Method (Simple Division)
One basic way to normalize is to simply divide each score by the total sum of all scores.
attn_weights_naive = attn_scores / attn_scores.sum()
print("Naive weights:", attn_weights_naive)
print("Sum:", attn_weights_naive.sum())
While this works for basic numbers, it is mathematically unstable in neural networks (especially if there are negative numbers or very large values).
The Standard Method (Softmax)
In modern LLMs and Transformers, we use a special mathematical function called Softmax. Softmax does two magical things:
- It uses exponents () to ensure all numbers (even negatives) become strictly positive.
- It divides by the new total sum so everything perfectly adds up to 1 (100%).
For fast, optimized, and numerically stable working, we always use PyTorch's built-in function!
attn_weights = torch.softmax(attn_scores, dim=0)
print(attn_weights)
By applying Softmax, our raw attention score of 1.4950 for "journey" becomes a clean attention weight of 0.2379 (or ~23.8%).
So attention weight of journey = 0.2379
Mathematically, Softmax looks like this:
For example, calculating the weight for our very first score ():
📊 Interactive Softmax & Temperature Visualizer
Adjust the raw Logits or move the Temperature slider to explore how temperature controls randomness in LLM next-token generation (low T makes output greedy/deterministic, high T flattens probabilities).