Text Classification
Text classification is the task of assigning one or more predefined labels to a piece of text based on its content, intent, or sentiment. A classifier learns a mapping from an Embedding or token sequence to a probability distribution over classes — typically by appending a linear layer with Softmax activation on top of a pre-trained Transformer encoder like BERT. The result is a model that can route customer messages to the right support queue, detect policy violations in user content, or sort research abstracts by topic, all without manual review.
Think of it like a postal sorting machine. Letters arrive in a continuous stream; sensors read the postcode on each envelope and mechanically divert it to the correct bin — fast, consistent, and indifferent to volume spikes.
from transformers import pipeline
classifier = pipeline("text-classification",
model="distilbert-base-uncased-finetuned-sst-2-english")
result = classifier("The movie had stunning visuals but a weak plot.")
print(result) # [{'label': 'NEGATIVE', 'score': 0.62}]
A single Fine-tuning run on a few thousand labelled examples is usually enough to adapt a pre-trained encoder to a new domain.
Text classification underpins many practical AI applications and relies on the same Pretraining-then-fine-tuning pipeline used across NLP. It is distinct from generative tasks: no new tokens are produced, only a label and confidence score. Related techniques include zero-shot classification, where a model like an LLM assigns labels without task-specific fine-tuning (Zero-Shot), and multi-label variants where documents can belong to several categories simultaneously. Loss Function choice — cross-entropy for single-label, binary cross-entropy for multi-label — is a key design decision.
You will encounter text classification in spam filters, content-moderation pipelines, intent detection for chatbots, and any NLP project that needs to route or triage text at scale.