Attention helps a model focus on important parts of input when making decisions. It works like how we pay attention to key words in a sentence to understand its meaning.
Translating a sentence from one language to another
Answering questions based on a paragraph of text
Summarizing a long article into a short summary
Recognizing objects in an image by focusing on important areas
This program shows a simple attention mechanism calculation step-by-step using PyTorch. It prints the scores, weights, and final output vector.
NLP
import torch
import torch.nn.functional as F
# Define query, key, value tensors
query = torch.tensor([[1., 0., 1.]]) # shape (1, 3)
key = torch.tensor([[1., 0., 0.], [0., 1., 0.]]) # shape (2, 3)
value = torch.tensor([[1., 2.], [3., 4.]]) # shape (2, 2)
d_k = query.size(-1) # dimension of key vectors# Calculate attention scores
scores = query @ key.T / (d_k ** 0.5) # shape (1, 2)# Apply softmax to get attention weights
weights = F.softmax(scores, dim=1) # shape (1, 2)# Multiply weights by values to get output
output = weights @ value # shape (1, 2)print(f"Attention scores: {scores}")
print(f"Attention weights: {weights}")
print(f"Output: {output}")
OutputSuccess
Important Notes
Attention helps models decide what to focus on, improving understanding.
Softmax turns scores into probabilities that add up to 1.
Query, key, and value come from the input data or previous layers.
Summary
Attention finds important parts of input to focus on.
It uses query, key, and value vectors to calculate weighted outputs.
Softmax makes scores into weights that sum to one.
Practice
(1/5)
1. What is the main purpose of the attention mechanism in NLP models?
easy
A. To reduce the number of layers in the model
B. To focus on important parts of the input data
C. To increase the size of the input data
D. To randomly shuffle the input tokens
Solution
Step 1: Understand the role of attention
Attention helps the model decide which parts of the input are important to look at when making predictions.
Step 2: Compare options with the concept
Only To focus on important parts of the input data correctly describes this focus on important input parts.
Final Answer:
To focus on important parts of the input data -> Option B
Quick Check:
Attention = Focus on important input [OK]
Hint: Attention means focusing on key input parts [OK]
Common Mistakes:
Thinking attention increases input size
Confusing attention with model depth
Assuming attention shuffles data
2. Which of the following correctly represents the formula to compute attention weights using query (Q) and key (K) vectors?
easy
A. Sigmoid(Q - K)
B. Softmax(Q + K)
C. ReLU(Q x K)
D. Softmax(Q x K^T)
Solution
Step 1: Recall attention weight calculation
Attention weights are computed by taking the dot product of query and key vectors, then applying softmax.
Step 2: Match formula to options
Softmax(Q x K^T) shows softmax applied to Q multiplied by the transpose of K, which is correct.
Final Answer:
Softmax(Q x K^T) -> Option D
Quick Check:
Attention weights = softmax(dot product) [OK]
Hint: Attention weights = softmax of query-key dot product [OK]
Common Mistakes:
Adding Q and K instead of dot product
Using ReLU or Sigmoid instead of softmax
Ignoring transpose on key vector
3. Given query vector Q = [1, 0], key vectors K1 = [1, 0], K2 = [0, 1], and value vectors V1 = [10, 0], V2 = [0, 20], what is the attention output after applying softmax on Q·K^T and multiplying by values?