Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 4 — Introduction to Transformers

Lesson 34 of 39 in the free Natural Language Processing notes on Siksha Sarovar, written by Rohit Jangra.

Introduction to Transformers

The Transformer architecture (Vaswani et al., "Attention Is All You Need", 2017) replaced RNNs/LSTMs (previous lesson) as the dominant architecture in NLP. Its key innovation is self-attention, which lets the model directly relate every word in a sequence to every other word — in parallel, without the sequential bottleneck of RNNs.

Why Self-Attention?

Recall the RNN limitation from the previous lesson: information from early words fades by the time the network reaches later words. Self-attention lets every word look directly at every other word in the sentence — regardless of distance — and learn which words matter most for understanding it.

"The animal didn't cross the street because it was too tired"
                                              ^
Self-attention lets "it" directly attend to "animal" (not "street"),
correctly resolving what "it" refers to -- this is coreference resolution
(recall Unit 1's discourse analysis!) learned automatically by the model.

The Core Self-Attention Mechanism (Conceptual)

For each word, the model computes three vectors: Query (Q), Key (K), and Value (V). Attention scores are computed by comparing each word's Query against every other word's Key, then using those scores to create a weighted combination of all Values.

Attention(Q, K, V) = softmax( Q · K^T / sqrt(d_k) ) · V
  • The higher the similarity between a word's Query and another word's Key, the more that other word's Value contributes to the output representation.
  • Multi-head attention runs this process several times in parallel with different learned projections, letting the model capture different types of relationships simultaneously (e.g. one head might track syntactic subject-verb links, another might track coreference).

Positional Encoding — Restoring Word Order

Unlike RNNs, self-attention has no inherent sense of word order (it looks at all words simultaneously). Transformers add a positional encoding vector to each word's embedding to inject information about its position in the sequence.

Encoder vs Decoder Transformers

ArchitectureRoleExample Models
Encoder-onlyBuilds rich contextual representations of input text (understanding tasks)BERT, RoBERTa
Decoder-onlyGenerates text one token at a time, autoregressively (generation tasks)GPT family
Encoder-DecoderEncodes input, then generates output text conditioned on it (transformation tasks)T5, BART (used for summarization, Unit 3; translation)

Using a Pre-trained Transformer (BERT) with Hugging Face

from transformers import AutoTokenizer, AutoModel
import torch

tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
model = AutoModel.from_pretrained("bert-base-uncased")

sentence1 = "I deposited money at the bank."
sentence2 = "We sat on the river bank."

for sentence in [sentence1, sentence2]:
    inputs = tokenizer(sentence, return_tensors="pt")
    outputs = model(**inputs)
    bank_index = tokenizer.tokenize(sentence).index("bank")
    bank_vector = outputs.last_hidden_state[0][bank_index + 1]  # +1 for [CLS] token
    print(sentence, "-> bank vector (first 5 dims):", bank_vector[:5])
# The two "bank" vectors will be DIFFERENT -- unlike Word2Vec's single fixed vector,
# BERT produces CONTEXTUAL embeddings that resolve word sense (WSD, Unit 2) automatically!

Word2Vec vs Contextual (Transformer) Embeddings

Word2Vec (previous lesson)Transformer (BERT-style)
Vectors per wordOne fixed vector, regardless of contextDifferent vector depending on the surrounding sentence
Handles polysemy (WSD, Unit 2)No — "bank" always gets the same vectorYes — resolves sense automatically
TrainingShallow, predicts nearby wordsDeep, many stacked self-attention layers
Typical use todayLightweight baselines, small projectsVirtually all state-of-the-art NLP systems

Transformer Applications Across This Course

Nearly every task covered in this course now has a Transformer-based state-of-the-art solution: POS tagging and NER (Unit 2) via BERT-based token classifiers, sentiment analysis and text classification (Unit 3) via fine-tuned BERT/RoBERTa, and abstractive summarization (Unit 3) via BART/T5. The Transformer architecture is also the direct foundation of the Large Language Models covered in the next lesson.