Images, language and games

Machine learning for natural language processing

Machine learning for natural language processing sorts documents into categories, extracts names, dates and amounts from text, and answers questions from a document collection. Fine-tuned transformer models handle most of these tasks, while TF-IDF with a linear model remains a strong, fast baseline.

Where to start

Learn tokenisation, TF-IDF features and a linear classifier first, so you can measure what a fine-tuned transformer adds. Then learn precision, recall and F1, which matter more than accuracy when classes are unbalanced.

How machine learning is used in natural language processing

Text classification

Models assign labels such as topic, sentiment or urgency to documents, emails and reviews. Fine-tuning a pretrained language model works well even with modest labelled data.

Real example: ULMFiT. ULMFiT showed that fine-tuning a pretrained language model outperformed the previous state of the art on six text classification tasks.

Extracting names, dates and facts

Named entity recognition and relation extraction pull structured facts out of text, such as drugs, genes or contract parties. Domain-specific pretraining helps in specialist text.

Real example: BioBERT. BioBERT is a language model pretrained on biomedical text that improved on BERT for biomedical named entity recognition, relation extraction and question answering.

Search and question answering

Language models help a search engine understand what a query means, not just which words it contains. The same models rank passages and extract answers.

Real example: BERT in Google Search. Google announced in 2019 that it was applying BERT models to better understand search queries.

Machine translation

Sequence-to-sequence models translate text between languages, trained on large collections of parallel sentences. Recent work extends this to languages with little training data.

Real example: No Language Left Behind (Meta AI). Meta AI's No Language Left Behind project built translation models covering 200 languages, including many low-resource languages.

Can you learn machine learning for NLP?

Yes. NLP is very accessible because pretrained models and libraries do much of the heavy lifting. Learn Python and text handling first, then classical models such as TF-IDF with logistic regression, then transformers and fine-tuning.

  1. Python and text handling. Clean, tokenise and count text with Python strings, pandas and regular expressions. Taught in the Python track of Foundations of AI
  2. Classical text models. Build TF-IDF features with logistic regression and evaluate with precision, recall and F1, which gives a fast baseline. Taught in the Machine Learning track of Foundations of AI
  3. Neural networks and embeddings. Learn how words and sentences become vectors and how sequence models use them. Covered by the Deep Learning course (coming next)
  4. Transformers and fine-tuning. Fine-tune a pretrained model such as DistilBERT with Hugging Face Transformers for classification or entity extraction. Covered by the Generative AI course (planned)

Moving into natural language processing

If you already work in the field

If you work with language professionally, as a linguist, translator, analyst or editor, you know where meaning is ambiguous and how to write labelling rules. Add Python and Hugging Face, then start with a classification project on documents you already know well.

If you come from data science or ML

If you come from tabular machine learning, learn tokenisation, embeddings and transformer fine-tuning. Pay attention to annotation guidelines and inter-annotator agreement, which limit how good any NLP model can be.

Roles that do this work: NLP engineerMachine learning engineer, NLPComputational linguistApplied scientist, language

Tools you will use

ToolWhat you use it for
Hugging Face TransformersLoad pretrained language models and fine-tune them for classification, tagging and question answering.
spaCyTokenise text, tag entities and parse sentences in fast production pipelines.
scikit-learnBuild TF-IDF and linear-model baselines and compute precision, recall and F1.
Sentence TransformersTurn sentences into embeddings for semantic search, clustering and similarity.
NLTKTeaching-friendly tokenisers, corpora and classic NLP algorithms for exploring text.

Public data to practise on

  • IMDb Large Movie Review Dataset: Movie reviews labelled positive or negative, the standard first dataset for sentiment classification.
  • Universal Dependencies: Treebanks in many languages annotated with parts of speech and syntax, for tagging and parsing models.
  • MultiNLI: Sentence pairs labelled entailment, neutral or contradiction across several genres, for natural language inference.

Your first project

Download the IMDb movie review dataset, predict review sentiment by fine-tuning DistilBERT, and compare it with TF-IDF and logistic regression using accuracy and F1 on the test set.

Keep the result, your code and a short note on what didn't work. That record is worth more than the score.

Common questions

Is NLP still relevant now that LLMs exist?
Yes. LLMs are themselves NLP models, and smaller fine-tuned models are often cheaper, faster and easier to evaluate for fixed tasks such as classification and extraction.
Do I need to know linguistics for NLP?
No formal linguistics is required to start, but understanding syntax, ambiguity and how languages differ helps with error analysis and multilingual work.
What is the best first NLP project?
Sentiment or topic classification on a public dataset such as IMDb reviews. Compare TF-IDF with logistic regression against a fine-tuned transformer.
Which library should I learn first for NLP?
scikit-learn for baselines and Hugging Face Transformers for pretrained models cover most practical work. spaCy is useful for fast entity recognition and text pipelines.

First Principles, the SchoolWhool newsletter

One machine learning idea, explained properly, every week.

The intuition, the common mistake, and a five-minute way to try it yourself. Written by Gaurav.

Learn the machine learning behind it, from first principles.

Start with Python from zero, free. Every stage after that builds on the last, and a person answers your doubts.