NLP Tutorial

BERT Family Models

BERT, RoBERTa, ALBERT, DistilBERT, ELECTRA, and XLNet pre-trained models.

BERT

BERT

BERT (Bidirectional Encoder Representations from Transformers) released by Google in 2018, changed everything. It was the first model to deeply understand context by looking at a word's left and right neighbors simultaneously.

Level 1 — Pre-training & Fine-tuning

BERT isn't just one model; it's a two-step process:

  1. Pre-training: The model reads half of the internet to learn how language works.
  2. Fine-tuning: You take that pre-trained model and teach it a specific task (like detecting spam) in just a few minutes.

Level 2 — MLM and NSP

BERT was trained using two clever unsupervised tasks:

  • Masked Language Modeling (MLM): Hiding 15% of words and making the model guess them.
  • Next Sentence Prediction (NSP): Guessing if Sentence B follows Sentence A.

Level 3 — Feature Extraction vs Fine-tuning

Advanced users can use BERT as a Feature Extractor (getting word vectors for other models) or through Full Fine-tuning (updating all BERT weights). Fine-tuning is generally superior for specialized accuracy.

BERT Sentence Embeddings
from transformers import AutoTokenizer, AutoModel
import torch

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

text = "BERT understands context bidirectionally."
inputs = tokenizer(text, return_tensors="pt")
outputs = model(**inputs)

# The 'last_hidden_state' contains contextual embeddings
embeddings = outputs.last_hidden_state

RoBERTa

RoBERTa

In 2019, Facebook AI researchers proved that BERT was "under-trained" and released RoBERTa (Robustly Optimized BERT Approach). Using the exact same architecture as BERT, it achieved much better results simply by training better.

Level 1 — More Data, More Training

RoBERTa was trained on 160GB of text (vs BERT's 16GB) and for much longer. It's the "Bodybuilder" version of BERT.

Level 2 — The Optimization Secret

RoBERTa made three major training changes:

  1. Dynamic Masking: Words are masked differently every time the model sees the sentence.
  2. Removed NSP: Researchers found that "Next Sentence Prediction" didn't actually help.
  3. Larger Batches: Training on massive batches of data improved stability.

Level 3 — When to use RoBERTa?

If you need an encoder for classification, NER, or similarity, and you have enough GPU memory, RoBERTa-Large is almost always a better choice than BERT-Base.

RoBERTa Sentiment Analysis
from transformers import pipeline

# RoBERTa fine-tuned on sentiment
classifier = pipeline("sentiment-analysis", 
                      model="cardiffnlp/twitter-roberta-base-sentiment")

result = classifier("I love this tutorial!")
print(result)

ALBERT

ALBERT

ALBERT (A Lite BERT) was developed to solve the "parameter explosion" problem. It's designed to be much lighter to store while staying just as smart as BERT.

Level 1 — Sharing is Caring

In a normal BERT model, every layer has its own unique weight. In ALBERT, all layers share the exact same weights. This reduces the number of parameters by a massive amount.

Level 2 — Parameter Reduction Secrets

  1. Factorized Embedding: Separates vocabulary size from hidden layer size.
  2. Cross-layer Parameter Sharing: All Transformer layers are identical.
  3. SOP (Sentence Order Prediction): A harder version of BERT's NSP that makes the model learn better logic.

Level 3 — Performance vs Memory

ALBERT-XXLarge is smarter than BERT-Large but significantly harder to train because even though it has fewer stored parameters, it still does the same amount of computation.

ALBERT vs BERT Parameters
# BERT-Base: 110M Params
# ALBERT-Base: 12M Params (9x reduction!)

from transformers import pipeline
albert_qa = pipeline("question-answering", model="albert-base-v2")

DistilBERT

DistilBERT

DistilBERT is the "Light" version of BERT. Developed by Hugging Face, it's 40% smaller, 60% faster, and retains 97% of BERT’s performance.

Level 1 — Knowledge Distillation

Think of it like a Teacher-Student relationship. The huge BERT model (Teacher) teaches a smaller model (DistilBERT/Student). The student learns the "essence" of the knowledge without needing the huge architecture.

Level 2 — Why use it?

  • Inference Speed: Fast enough for real-time mobile apps.
  • Memory: Low RAM usage.
  • Deployment: Cheaper to run on cloud servers.

Level 3 — Training Strategy

DistilBERT is trained with a triple loss function (Distillation, MLM, and Cosine similarity). It removes the Token-type embeddings and Pooler layers from BERT to keep things lean.

DistilBERT Production Usage
from transformers import pipeline

# The go-to model for fast sentiment analysis
classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")

print(classifier("This is the best model for speed!"))

ELECTRA: Efficient Pre-training

What is ELECTRA?

ELECTRA (Efficiently Learning an Encoder that Classifies Token Replacements Accurately) is a pre-training approach that makes language models much faster to train while maintaining high accuracy. While BERT predicts "hidden" words, ELECTRA identifies "fake" words.

Level 1 — Replaced Token Detection (RTD)

The core innovation of ELECTRA is Replaced Token Detection. Instead of masking tokens with [MASK], ELECTRA uses an architecture consisting of two neural networks:

  1. The Generator: A small BERT-like model that replaces some tokens in the original sentence with plausible alternatives (e.g., replacing "cook" with "eat").
  2. The Discriminator: The main ELECTRA model. It looks at the corrupted sentence and predicts for every single word whether it is the original word or a replacement from the generator.
The RTD Workflow
Original: The chef cooked the meal.
Generator Output: The chef ate the meal.
Discriminator Prediction: [Original, Original, REPLACED, Original, Original]

Level 2 — Why ELECTRA is Better

ELECTRA solves the two biggest inefficiencies of BERT's Masked Language Modeling (MLM):

100% Training Signal

BERT only learns from the 15% of tokens that are masked. ELECTRA learns from every single token in the input. This makes it significantly more efficient per training step.

No Mismatch

BERT sees [MASK] during training but never during fine-tuning (inference). ELECTRA sees real words in both cases, eliminating the train-test discrepancy.

Level 3 — Implementation with Transformers

ELECTRA models come in various sizes (Small, Base, Large). ELECTRA-Small is famous for being incredibly powerful even on a single consumer GPU.

Sentiment Analysis with ELECTRA
from transformers import pipeline

# Load ELECTRA-Small fine-tuned for Sentiment Analysis
# It's as accurate as BERT-Base but uses 1/10th the memory!
classifier = pipeline("sentiment-analysis", 
                      model="google/electra-small-discriminator")

texts = [
    "ELECTRA is surprisingly fast and accurate.",
    "The training time was a bit too long for my liking."
]

results = classifier(texts)

for text, res in zip(texts, results):
    label = res['label']
    score = res['score']
    print(f"[{label}] {text} (Score: {score:.4f})")

# Output Note: You will see the model accurately detecting 
# subtle differences in sentiment with lightning speed.
Pro Tip: Use ELECTRA if you are working with limited compute resources or need a fast, low-latency model for production. ELECTRA-Small often outperforms DistilBERT while being smaller in size.

XLNet

XLNet

XLNet was designed to beat BERT by combining the best of BERT (bidirectional context) and the best of GPT (native generation) using a clever trick called Permutation Language Modeling.

Level 1 — Autoregressive + Bidirectional

BERT uses [MASK] tokens which don't exist in the real world. XLNet avoids [MASK] by predicting words in a random order (permutations), allowing it to see surrounding words without breaking the sentence.

Level 2 — Permutation Math

Instead of just 1-2-3-4, XLNet might train on 3-1-4-2. By the time it predicts word 3, it might have already seen words 1 and 4. This captures context from both directions without needing the [MASK] placeholder.

Level 3 — Long Dependency Modeling

XLNet uses Transformer-XL mechanisms, allowing it to maintain context over extremely long documents where BERT would get cut off after 512 tokens.

XLNet in Transformers
from transformers import XLNetTokenizer, XLNetModel

tokenizer = XLNetTokenizer.from_pretrained('xlnet-base-cased')
model = XLNetModel.from_pretrained('xlnet-base-cased')

inputs = tokenizer("XLNet is powerful for long text.", return_tensors="pt")
outputs = model(**inputs)