Practice in Lab: Project: Transfer sentiment across domains →
Download complete project source →
Project: Transfer sentiment across domains
An IID split does not measure domain transfer. Keep every IMDb review group out of training and validation. Normalize duplicate identities across all three sources before assigning a split; a repeated sentence that occurs in IMDb belongs only to the held-out domain. Select the checkpoint using Amazon/Yelp validation rows. Report IMDb results as a domain-shift measurement without tuning against those labels.
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
Train on Amazon and Yelp, select on their validation partition, then report untouched IMDb performance.
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
Train on Amazon and Yelp, select on their validation partition, then report untouched IMDb performance. 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.