CourseNatural Language Processing: The Complete Course · Module 7: Sequence Models and Transformers with Hugging Face · part 51 of 73
Part 51 · Module 7: Sequence Models and Transformers with Hugging Face

Lab: Fine-tuned DistilBERT against the TF-IDF router

81 min read·14 Oct 2026
Module 7 · Lab · About 60 minutes · Needs: lessons 1 to 4 of this module, and the Module 6 lab (or its cells, pasted here). A Colab T4 is recommended; the CPU path finishes in a few minutes

What you are building

SETTLED FILES hf_classifier.py test_hf_classifier.py TF-IDF v1 models/, 0.4 MB TRAINED HERE DistilBERT 600 messages, 13 s 268.6 MB FOR ROSA the report per class, language seeds, time, size both scored on the 80 HARD_TEST messages
The lab settles the module's file (blue), trains DistilBERT (amber) beside the served TF-IDF model (grey), and writes one comparison for Rosa (green).

It is Monday 19 October 2026, project week 7. Rosa's question from Lesson 4 still needs a fair answer: does a fine-tuned transformer beat the TF-IDF router on Tidewell's own mail, and what does it cost? The four lessons appended parts to inbox/hf_classifier.py and tests/test_hf_classifier.py. This lab settles both files once, brings back the saved TF-IDF model, fine-tunes DistilBERT with a fixed seed and saves it in models/, then scores both on HARD_TEST, the 80 hand-written messages: per class, per language, with training time, model size and speed. It ends with two more seeds and an interval, because one run on 80 messages can mislead, and a report for Rosa in reports/.

Open a new Colab notebook, call it module-07-lab.ipynb, and run each cell in order.

The rest of this part opens with an account

Signing up is free and takes one email. Nothing to pay, and nothing renews.