Practice in Lab: Project: Fine-tune pretrained BERT →
Download complete project source →
Project: Fine-tune pretrained BERT
Full fine-tuning updates all 4.39 million classifier parameters. The random classification head is initialized once, while the encoder starts from Google’s published BERT-Tiny weights. AdamW state is substantially larger than the exported model because each trainable parameter receives optimizer moments. This project uses real positive and negative review sentences rather than a synthetic token rule.
Download dependencies, keep the source
In Pythilo, choose this project in New project. Its code is saved locally immediately. Open Project downloads and explicitly tap Download project packages to save PyTorch, the shared UCI dataset/tokenizer and the 17.7 MB pretrained safetensors asset. Nothing downloads when you create, open or run a project. Once that complete download finishes, training and evaluation run entirely offline on the CPU. Progress, pause and resume are in Downloads. Native Python backends remain in the signed app. Each project shares the same downloaded assets.
On a desktop, use the complete source archive linked from this lesson, install torch==2.15.0 and numpy, then run fetch_assets.py once. Run python main.py for the complete dataset; --quick is an explicitly smaller contract-check run. Pythilo’s Run button uses the complete project by default.
Inspect the actual source data
The original dataset contains 3,000 review sentences from Amazon, Yelp and IMDb. The loader checks source hashes, normalizes text identities, removes conflicting labels, deduplicates groups and creates deterministic partitions. It splits only on newline because two IMDb sentences contain embedded NEL characters. Tokenization uses Google’s fixed WordPiece vocabulary; no vocabulary is fitted on the test set.
import pythilo_finetune as ft
splits = ft.dataset()
print({key: len(splits[key]) for key in ("train", "valid", "test")})
print(splits["original_rows"])Build the experiment
Update the complete pretrained encoder; select a checkpoint on validation macro-F1 and evaluate the test once.
Read the complete reference implementation below. The shared pythilo_finetune.py implements BERT, WordPiece, safe asset loading, metrics and artifact primitives; the project owns its data choices, optimizer loop and evidence. Full and LoRA recipes expose zero_grad, cross_entropy, backward, gradient clipping, optimizer.step and validation selection directly.
Evaluate and review
Compare validation and held-out loss, accuracy and macro-F1. The report records row and normalized-text identities, all asset hashes, the pretrained revision, real optimizer-state bytes and artifact sizes. Model quality is measured, not assumed; the short quick fixture can perform poorly. Four epochs on this small English dataset do not establish production readiness. Review subgroup errors, multiple seeds, data rights, calibration, serving latency and representative out-of-domain tests before deployment.
Sources and reuse
Model: Google BERT miniature, revision 30b0a37ccaaa32f332884b96992754e246e48c5f, Apache-2.0. Dataset: Kotzias (2015), Sentiment Labelled Sentences, DOI 10.24432/C57604, CC BY 4.0. Original files are retained verbatim; normalization, grouping and splitting happen in memory. Attribution and the source manifest accompany the downloaded data.
Your task
Update the complete pretrained encoder; select a checkpoint on validation macro-F1 and evaluate the test once. Implement run_project(output, quick=False). Follow the reference report/artifact schema; the checks run quick=True with actual pretrained weights and real data. The full dataset is the default.
The explanation opens here once every check passes.