{"slug": "imdb-sentiment-analysis-with-distilbert-lora-tf-idf-baselines-calibration-and", "title": "IMDb Sentiment Analysis with DistilBERT LoRA, TF-IDF Baselines, Calibration, Interpretability, Robustness Testing, and Semi-Supervised Learning", "summary": "A tutorial by an unnamed author demonstrates an end-to-end sentiment analysis workflow on the Stanford NLP IMDb Large Movie Review Dataset, comparing a TF-IDF and Logistic Regression baseline with a DistilBERT model fine-tuned using LoRA via PEFT. The workflow evaluates models using accuracy, macro-F1, ROC-AUC, calibration metrics, and robustness tests, and incorporates semi-supervised learning with pseudo-labeling on the unlabeled IMDb split. The tutorial reports that the transformer approach achieves higher performance than the baseline, with detailed analysis of confident errors, length-based performance, and truncation effects.", "body_md": "In this tutorial, we develop an end-to-end sentiment analysis workflow using the[ Stanford NLP IMDb](https://huggingface.co/datasets/stanfordnlp/imdb) Large Movie Review Dataset and compare classical machine learning with parameter-efficient transformer fine-tuning. We begin by establishing a reproducible environment and auditing the dataset for class ordering, review-length skew, duplicate leakage, and preprocessing artifacts before training a strong TF-IDF and Logistic Regression baseline. We then fine-tune DistilBERT with LoRA through PEFT, evaluate it using accuracy, macro-F1, ROC-AUC, confusion matrices, and ROC curves, and examine threshold selection and probability calibration through Expected Calibration Error and reliability analysis. Beyond headline metrics, we investigate confident errors, performance across review lengths, word-level occlusion saliency, and head-versus-tail truncation to understand how the model reaches its predictions and where long-context limitations affect performance. Finally, we use the unlabeled IMDb split for confidence-based pseudo-labeling, compare the resulting semi-supervised model against our baseline, and save the merged transformer for reusable sentiment inference.\n\n``` python\nimport importlib.util, subprocess, sys, os, time, random, warnings, inspect, hashlib\nwarnings.filterwarnings(\"ignore\")\nos.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\nos.environ[\"WANDB_DISABLED\"] = \"true\"\n_REQUIRED = {\n   \"transformers\": \"transformers\",\n   \"datasets\": \"datasets\",\n   \"peft\": \"peft\",\n   \"accelerate\": \"accelerate\",\n   \"sklearn\": \"scikit-learn\",\n}\n_missing = [pkg for mod, pkg in _REQUIRED.items() if importlib.util.find_spec(mod) is None]\nif _missing:\n   print(f\"Installing: {', '.join(_missing)} ...\")\n   subprocess.run([sys.executable, \"-m\", \"pip\", \"install\", \"-q\", *_missing], check=True)\n   print(\"Done. (If imports fail below, restart the runtime and re-run.)\\n\")\nimport numpy as np\nimport pandas as pd\nimport torch\nimport matplotlib.pyplot as plt\nfrom datasets import load_dataset\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.metrics import (accuracy_score, f1_score, roc_auc_score,\n                            classification_report, confusion_matrix, roc_curve)\nfrom transformers import (AutoTokenizer, AutoModelForSequenceClassification,\n                         TrainingArguments, Trainer, DataCollatorWithPadding,\n                         EarlyStoppingCallback, set_seed)\nfrom peft import LoraConfig, get_peft_model, TaskType\ndef _disable_torchao_probe():\n   patched = []\n   try:\n       import peft.import_utils as _piu\n       _piu.is_torchao_available = lambda: False\n       patched.append(\"peft.import_utils\")\n   except Exception:\n       pass\n   for _name, _mod in list(sys.modules.items()):\n       if _name.startswith(\"peft\") and hasattr(_mod, \"is_torchao_available\"):\n           _mod.is_torchao_available = lambda: False\n           patched.append(_name)\n   return patched\ntry:\n   import torchao as _tao\n   _v = getattr(_tao, \"__version__\", \"?\")\n   if tuple(int(x) for x in _v.split(\".\")[:2]) < (0, 16):\n       print(f\"[compat] torchao {_v} < 0.16 -> disabling PEFT's torchao probe: \"\n             f\"{', '.join(_disable_torchao_probe())}\")\nexcept Exception:\n   _disable_torchao_probe()\nSEED        = 42\nMODEL_NAME  = \"distilbert-base-uncased\"\nMAX_LEN     = 256\nN_TRAIN     = 5000\nN_EVAL      = 2000\nN_UNSUP     = 3000\nEPOCHS      = 2\nBATCH       = 16\nLR          = 3e-4\nFULL_RUN    = False\nif FULL_RUN:\n   N_TRAIN, N_EVAL, EPOCHS = 25000, 25000, 3\nset_seed(SEED); random.seed(SEED); np.random.seed(SEED); torch.manual_seed(SEED)\nDEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nprint(\"=\" * 79)\nprint(f\"device={DEVICE} | torch={torch.__version__} | \"\n     f\"gpu={torch.cuda.get_device_name(0) if DEVICE=='cuda' else 'n/a'}\")\nprint(\"=\" * 79)\nt0 = time.time()\nraw = load_dataset(\"stanfordnlp/imdb\")\nprint(raw, f\"\\nloaded in {time.time()-t0:.1f}s\\n\")\nprint(\"--- example (truncated) ---\")\nprint(\"label:\", raw[\"train\"][0][\"label\"], \"|\", raw[\"train\"][0][\"text\"][:300], \"...\\n\")\nfirst_labels = np.array(raw[\"train\"][\"label\"][:5])\nlast_labels  = np.array(raw[\"train\"][\"label\"][-5:])\nprint(f\"TRAP #1 - split ordering: first 5 labels {first_labels}, \"\n     f\"last 5 labels {last_labels}  -> ALWAYS shuffle before subsampling.\")\ntrain_full = raw[\"train\"].shuffle(seed=SEED)\ntest_full  = raw[\"test\"].shuffle(seed=SEED)\ntrain_ds   = train_full.select(range(min(N_TRAIN, len(train_full))))\neval_ds    = test_full.select(range(min(N_EVAL, len(test_full))))\nprint(f\"   after shuffle+subsample: train balance = \"\n     f\"{np.bincount(train_ds['label'])}, eval balance = {np.bincount(eval_ds['label'])}\")\nlens = np.array([len(t.split()) for t in train_full[\"text\"]])\nq = np.percentile(lens, [50, 75, 90, 95, 99])\nprint(f\"\\nTRAP #2 - length (words): median={q[0]:.0f} p75={q[1]:.0f} p90={q[2]:.0f} \"\n     f\"p95={q[3]:.0f} p99={q[4]:.0f} max={lens.max()}\")\nprint(f\"   ~{(lens > MAX_LEN*0.75).mean()*100:.1f}% of reviews exceed MAX_LEN={MAX_LEN} \"\n     f\"tokens (rough words->tokens factor 1.3). Section 9 measures what that costs.\")\nh_tr = {hashlib.md5(t.encode()).hexdigest() for t in raw[\"train\"][\"text\"]}\nh_te = {hashlib.md5(t.encode()).hexdigest() for t in raw[\"test\"][\"text\"]}\nprint(f\"\\nTRAP #3 - leakage: {len(h_tr & h_te)} exact duplicate reviews across \"\n     f\"train/test; {len(raw['train'])-len(h_tr)} dupes inside train itself.\")\ndef clean(t):\n   return t.replace(\"<br />\", \" \").replace(\"<br/>\", \" \").strip()\nplt.figure(figsize=(11, 3.2))\nplt.subplot(1, 2, 1)\nplt.hist(np.clip(lens, 0, 1000), bins=60)\nplt.axvline(MAX_LEN, ls=\"--\", color=\"k\", label=f\"MAX_LEN={MAX_LEN}\")\nplt.title(\"Review length (words, clipped at 1000)\"); plt.legend()\nplt.subplot(1, 2, 2)\nplt.bar([\"neg\", \"pos\"], np.bincount(raw[\"train\"][\"label\"]))\nplt.title(\"Train class balance (perfectly balanced)\")\nplt.tight_layout(); plt.show()\n```\n\nWe configure the Colab environment, install the required libraries, apply the PEFT–torchao compatibility fix, and set deterministic seeds for reproducible experiments. We load the Stanford IMDb dataset, shuffle and subsample the train and test splits, and inspect class balance, review-length distributions, duplicate leakage, and HTML artifacts. We also visualize review lengths and label frequencies so we understand the dataset structure before building any models.\n\n```\nprint(\"\\n\" + \"=\" * 79 + \"\\n3. TF-IDF BASELINE\\n\" + \"=\" * 79)\nXtr = [clean(t) for t in train_ds[\"text\"]]; ytr = np.array(train_ds[\"label\"])\nXte = [clean(t) for t in eval_ds[\"text\"]];  yte = np.array(eval_ds[\"label\"])\nt0 = time.time()\ntfidf_clf = make_pipeline(\n   TfidfVectorizer(ngram_range=(1, 2), min_df=2, max_features=300_000,\n                   sublinear_tf=True, strip_accents=\"unicode\"),\n   LogisticRegression(C=8.0, max_iter=2000, n_jobs=-1),\n)\ntfidf_clf.fit(Xtr, ytr)\np_tfidf = tfidf_clf.predict_proba(Xte)[:, 1]\nacc_tfidf = accuracy_score(yte, p_tfidf > 0.5)\nauc_tfidf = roc_auc_score(yte, p_tfidf)\nprint(f\"trained in {time.time()-t0:.1f}s -> acc={acc_tfidf:.4f}  auc={auc_tfidf:.4f}\")\nvec, lr = tfidf_clf.steps[0][1], tfidf_clf.steps[1][1]\nfeats, coefs = np.array(vec.get_feature_names_out()), lr.coef_[0]\norder = np.argsort(coefs)\nprint(\"\\nmost NEGATIVE n-grams:\", \", \".join(feats[order[:12]]))\nprint(\"most POSITIVE n-grams:\", \", \".join(feats[order[-12:]][::-1]))\nprint(\"\\n\" + \"=\" * 79 + \"\\n4. LoRA FINE-TUNING\\n\" + \"=\" * 79)\ntok = AutoTokenizer.from_pretrained(MODEL_NAME)\ndef tokenize(batch):\n   return tok([clean(t) for t in batch[\"text\"]], truncation=True, max_length=MAX_LEN)\ntr_tok = (train_ds.map(tokenize, batched=True, remove_columns=[\"text\"])\n                 .rename_column(\"label\", \"labels\"))\nev_tok = (eval_ds.map(tokenize, batched=True, remove_columns=[\"text\"])\n                .rename_column(\"label\", \"labels\"))\nbase = AutoModelForSequenceClassification.from_pretrained(\n   MODEL_NAME, num_labels=2,\n   id2label={0: \"NEGATIVE\", 1: \"POSITIVE\"},\n   label2id={\"NEGATIVE\": 0, \"POSITIVE\": 1},\n)\nlora_cfg = LoraConfig(\n   task_type=TaskType.SEQ_CLS,\n   r=16, lora_alpha=32, lora_dropout=0.05,\n   target_modules=[\"q_lin\", \"v_lin\"],\n   modules_to_save=[\"pre_classifier\", \"classifier\"],\n)\ntry:\n   model = get_peft_model(base, lora_cfg)\nexcept ImportError as e:\n   _disable_torchao_probe()\n   print(f\"[compat] retrying after backend probe failure: {e}\")\n   model = get_peft_model(base, lora_cfg)\nmodel.print_trainable_parameters()\ndef compute_metrics(eval_pred):\n   logits, labels = eval_pred\n   probs = torch.softmax(torch.tensor(logits), dim=-1).numpy()[:, 1]\n   preds = (probs > 0.5).astype(int)\n   return {\"accuracy\": accuracy_score(labels, preds),\n           \"f1_macro\": f1_score(labels, preds, average=\"macro\"),\n           \"roc_auc\": roc_auc_score(labels, probs)}\n_ta = inspect.signature(TrainingArguments.__init__).parameters\n_eval_key = \"eval_strategy\" if \"eval_strategy\" in _ta else \"evaluation_strategy\"\nta_kwargs = dict(\n   output_dir=\"./imdb_lora\", learning_rate=LR,\n   per_device_train_batch_size=BATCH, per_device_eval_batch_size=BATCH * 2,\n   num_train_epochs=EPOCHS, weight_decay=0.01, warmup_ratio=0.06,\n   logging_steps=50, save_strategy=\"epoch\", save_total_limit=1,\n   load_best_model_at_end=True, metric_for_best_model=\"accuracy\",\n   fp16=(DEVICE == \"cuda\"), report_to=\"none\", seed=SEED,\n)\nta_kwargs[_eval_key] = \"epoch\"\n_tr = inspect.signature(Trainer.__init__).parameters\n_tok_key = \"processing_class\" if \"processing_class\" in _tr else \"tokenizer\"\ntrainer = Trainer(\n   model=model, args=TrainingArguments(**ta_kwargs),\n   train_dataset=tr_tok, eval_dataset=ev_tok,\n   data_collator=DataCollatorWithPadding(tok),\n   compute_metrics=compute_metrics,\n   callbacks=[EarlyStoppingCallback(early_stopping_patience=2)],\n   **{_tok_key: tok},\n)\nt0 = time.time()\ntrainer.train()\nprint(f\"\\nfine-tuned in {(time.time()-t0)/60:.1f} min\")\n```\n\nWe train a strong TF-IDF and Logistic Regression baseline and inspect the most influential positive and negative n-grams to establish an interpretable reference point. We then tokenize the IMDb reviews and configure DistilBERT with LoRA adapters that update only a small subset of model parameters while keeping the backbone largely frozen. We use the Hugging Face Trainer with dynamic padding, early stopping, mixed precision, and multiple evaluation metrics to fine-tune the transformer efficiently.\n\n```\nprint(\"\\n\" + \"=\" * 79 + \"\\n5. EVALUATION\\n\" + \"=\" * 79)\npred_out = trainer.predict(ev_tok)\np_lora = torch.softmax(torch.tensor(pred_out.predictions), dim=-1).numpy()[:, 1]\ny_true = np.array(pred_out.label_ids)\nyhat = (p_lora > 0.5).astype(int)\nprint(classification_report(y_true, yhat, target_names=[\"neg\", \"pos\"], digits=4))\ncm = confusion_matrix(y_true, yhat)\nfig, ax = plt.subplots(1, 2, figsize=(11, 4))\nax[0].imshow(cm, cmap=\"Blues\")\nfor i in range(2):\n   for j in range(2):\n       ax[0].text(j, i, cm[i, j], ha=\"center\", va=\"center\", fontsize=14)\nax[0].set_xticks([0, 1], [\"pred neg\", \"pred pos\"])\nax[0].set_yticks([0, 1], [\"true neg\", \"true pos\"]); ax[0].set_title(\"Confusion matrix\")\nfor name, p in [(\"TF-IDF\", p_tfidf), (\"DistilBERT+LoRA\", p_lora)]:\n   fpr, tpr, _ = roc_curve(y_true, p)\n   ax[1].plot(fpr, tpr, label=f\"{name} (AUC={roc_auc_score(y_true, p):.4f})\")\nax[1].plot([0, 1], [0, 1], \"k--\", lw=0.8)\nax[1].set_xlabel(\"FPR\"); ax[1].set_ylabel(\"TPR\"); ax[1].set_title(\"ROC\"); ax[1].legend()\nplt.tight_layout(); plt.show()\nprint(\"\\n\" + \"=\" * 79 + \"\\n6. THRESHOLD & CALIBRATION\\n\" + \"=\" * 79)\nths = np.linspace(0.05, 0.95, 91)\naccs = [(y_true == (p_lora > t)).mean() for t in ths]\nbest_t = ths[int(np.argmax(accs))]\nprint(f\"[email protected] = {accs[45]:.4f} | best threshold = {best_t:.2f} -> acc = {max(accs):.4f}\")\ndef expected_calibration_error(probs, labels, n_bins=10):\n   \"\"\"ECE: |confidence - accuracy| averaged over confidence bins.\"\"\"\n   conf = np.maximum(probs, 1 - probs)\n   correct = (probs > 0.5).astype(int) == labels\n   bins = np.linspace(0, 1, n_bins + 1)\n   ece, xs, ys = 0.0, [], []\n   for lo, hi in zip(bins[:-1], bins[1:]):\n       m = (conf > lo) & (conf <= hi)\n       if m.sum() == 0:\n           continue\n       ece += m.mean() * abs(conf[m].mean() - correct[m].mean())\n       xs.append(conf[m].mean()); ys.append(correct[m].mean())\n   return ece, np.array(xs), np.array(ys)\nece, cx, cy = expected_calibration_error(p_lora, y_true)\nprint(f\"Expected Calibration Error = {ece:.4f}  (0 = perfectly calibrated)\")\nplt.figure(figsize=(9, 3.2))\nplt.subplot(1, 2, 1); plt.plot(ths, accs); plt.axvline(best_t, ls=\"--\", color=\"r\")\nplt.xlabel(\"threshold\"); plt.ylabel(\"accuracy\"); plt.title(\"Threshold sweep\")\nplt.subplot(1, 2, 2); plt.plot([0.5, 1], [0.5, 1], \"k--\", lw=0.8)\nplt.plot(cx, cy, \"o-\"); plt.xlabel(\"mean confidence\"); plt.ylabel(\"empirical accuracy\")\nplt.title(f\"Reliability diagram (ECE={ece:.3f})\")\nplt.tight_layout(); plt.show()\n```\n\nWe evaluate the fine-tuned DistilBERT-LoRA model using classification metrics, a confusion matrix, and ROC curves while directly comparing its ROC-AUC performance with the TF-IDF baseline. We sweep classification thresholds to determine whether the default probability cutoff of 0.5 gives the best accuracy on our evaluation set. We also calculate Expected Calibration Error and construct a reliability diagram to measure how closely the model’s predicted confidence corresponds to its actual correctness.\n\n```\nprint(\"\\n\" + \"=\" * 79 + \"\\n7. ERROR ANALYSIS\\n\" + \"=\" * 79)\nerr = pd.DataFrame({\n   \"text\": eval_ds[\"text\"], \"y\": y_true, \"p_pos\": p_lora,\n   \"n_words\": [len(t.split()) for t in eval_ds[\"text\"]],\n})\nerr[\"pred\"] = (err.p_pos > 0.5).astype(int)\nerr[\"correct\"] = err.pred == err.y\nerr[\"confidence\"] = np.maximum(err.p_pos, 1 - err.p_pos)\nprint(\"--- 3 most CONFIDENT mistakes (where the model is confidently wrong) ---\")\nfor _, r in err[~err.correct].nlargest(3, \"confidence\").iterrows():\n   print(f\"\\n[true={'pos' if r.y else 'neg'} pred={'pos' if r.pred else 'neg'} \"\n         f\"conf={r.confidence:.3f} words={r.n_words}]\")\n   print(clean(r.text)[:400].replace(\"\\n\", \" \"), \"...\")\nerr[\"bucket\"] = pd.qcut(err.n_words, 4, labels=[\"short\", \"med\", \"long\", \"v.long\"])\nby_len = err.groupby(\"bucket\", observed=True).agg(acc=(\"correct\", \"mean\"), n=(\"correct\", \"size\"))\nprint(\"\\n--- accuracy by review length (truncation hurts long reviews) ---\")\nprint(by_len.to_string())\nprint(\"\\n\" + \"=\" * 79 + \"\\n8. OCCLUSION SALIENCY\\n\" + \"=\" * 79)\ninfer_model = model.merge_and_unload()\ninfer_model.to(DEVICE).eval()\n@torch.no_grad()\ndef predict_proba(texts, bs=64):\n   out = []\n   for i in range(0, len(texts), bs):\n       enc = tok([clean(t) for t in texts[i:i + bs]], truncation=True,\n                 max_length=MAX_LEN, padding=True, return_tensors=\"pt\").to(DEVICE)\n       out.append(torch.softmax(infer_model(**enc).logits, dim=-1)[:, 1].cpu().numpy())\n   return np.concatenate(out)\ndef occlusion(text, max_words=60):\n   words = clean(text).split()[:max_words]\n   base = predict_proba([\" \".join(words)])[0]\n   variants = [\" \".join(words[:i] + words[i + 1:]) for i in range(len(words))]\n   dropped = predict_proba(variants)\n   return words, base - dropped, base\nsample = err[err.correct].nlargest(1, \"confidence\").iloc[0]\nwords, contrib, base_p = occlusion(sample.text)\nprint(f\"P(positive) for the full excerpt = {base_p:.3f} \"\n     f\"(true label = {'pos' if sample.y else 'neg'})\\n\")\ntop = np.argsort(np.abs(contrib))[-15:]\nplt.figure(figsize=(7, 5))\nplt.barh(range(len(top)), contrib[top],\n        color=[\"tab:green\" if contrib[i] > 0 else \"tab:red\" for i in top])\nplt.yticks(range(len(top)), [words[i] for i in top])\nplt.xlabel(\"Δ P(positive) when the word is removed\")\nplt.title(\"Occlusion saliency — green pushes POSITIVE, red pushes NEGATIVE\")\nplt.tight_layout(); plt.show()\nprint(\"\\n\" + \"=\" * 79 + \"\\n9. HEAD vs TAIL TRUNCATION\\n\" + \"=\" * 79)\nprobe = err.nlargest(600, \"n_words\")\nW = 180\nhead_txt = [\" \".join(clean(t).split()[:W]) for t in probe.text]\ntail_txt = [\" \".join(clean(t).split()[-W:]) for t in probe.text]\nyp = probe.y.values\nacc_head = ((predict_proba(head_txt) > 0.5).astype(int) == yp).mean()\nacc_tail = ((predict_proba(tail_txt) > 0.5).astype(int) == yp).mean()\nprint(f\"on the {len(probe)} longest reviews, using only {W} words:\")\nprint(f\"  first {W} words -> acc {acc_head:.4f}\")\nprint(f\"  last  {W} words -> acc {acc_tail:.4f}\")\nprint(\"  Practical takeaway: if the tail wins, feed head+tail to the model or \"\n     \"raise MAX_LEN, rather than blindly truncating from the left.\")\n```\n\nWe examine the model’s most confident incorrect predictions and group reviews by length to identify truncation-related failure patterns and difficult examples. We merge the LoRA adapters into the underlying model and apply leave-one-word-out occlusion to estimate which words push individual predictions toward positive or negative sentiment. We then compare predictions based on the beginning and ending portions of long reviews to determine where the strongest sentiment information resides.\n\n```\nprint(\"\\n\" + \"=\" * 79 + \"\\n10. PSEUDO-LABELLING\\n\" + \"=\" * 79)\nunsup = raw[\"unsupervised\"].shuffle(seed=SEED).select(range(N_UNSUP))\np_uns = predict_proba(unsup[\"text\"])\nkeep = (p_uns > 0.95) | (p_uns < 0.05)\npl_texts = [clean(t) for t, k in zip(unsup[\"text\"], keep) if k]\npl_labels = (p_uns[keep] > 0.5).astype(int)\nprint(f\"kept {keep.sum()}/{N_UNSUP} pseudo-labels at conf>0.95 \"\n     f\"(balance: {np.bincount(pl_labels)})\")\naug = make_pipeline(\n   TfidfVectorizer(ngram_range=(1, 2), min_df=2, max_features=300_000,\n                   sublinear_tf=True, strip_accents=\"unicode\"),\n   LogisticRegression(C=8.0, max_iter=2000, n_jobs=-1),\n).fit(Xtr + pl_texts, np.concatenate([ytr, pl_labels]))\nacc_aug = accuracy_score(yte, aug.predict(Xte))\nprint(f\"TF-IDF baseline      : {acc_tfidf:.4f}\")\nprint(f\"TF-IDF + pseudo-labels: {acc_aug:.4f}  (Δ {acc_aug-acc_tfidf:+.4f})\")\nprint(\"Caveat: gains are bounded by the teacher. Self-training also amplifies \"\n     \"the teacher's biases — always validate on clean, held-out data.\")\nprint(\"\\n\" + \"=\" * 79 + \"\\n11. SAVE & INFER\\n\" + \"=\" * 79)\nSAVE_DIR = \"./imdb-distilbert-lora-merged\"\ninfer_model.save_pretrained(SAVE_DIR); tok.save_pretrained(SAVE_DIR)\nprint(f\"saved merged model to {SAVE_DIR}/  (load with \"\n     f\"AutoModelForSequenceClassification.from_pretrained('{SAVE_DIR}'))\")\ndemos = [\n   \"A masterclass in tension. The final act left the whole theatre silent.\",\n   \"Two hours I will never get back. Wooden acting, incoherent plot.\",\n   \"It's not the disaster the trailer promised, but it never really lands either.\",\n]\nfor d, p in zip(demos, predict_proba(demos)):\n   print(f\"  P(pos)={p:.3f} -> {'POSITIVE' if p > 0.5 else 'NEGATIVE'} | {d}\")\nprint(\"\\n\" + \"=\" * 79)\nprint(f\"SUMMARY (n_train={N_TRAIN}, n_eval={N_EVAL}, max_len={MAX_LEN})\")\nprint(\"=\" * 79)\nprint(pd.DataFrame([\n   {\"model\": \"TF-IDF + LogReg\", \"accuracy\": acc_tfidf, \"roc_auc\": auc_tfidf},\n   {\"model\": \"TF-IDF + pseudo-labels\", \"accuracy\": acc_aug, \"roc_auc\": float(\"nan\")},\n   {\"model\": \"DistilBERT + LoRA\", \"accuracy\": accuracy_score(y_true, yhat),\n    \"roc_auc\": roc_auc_score(y_true, p_lora)},\n]).to_string(index=False))\nprint(\"\"\"\nNEXT EXPERIMENTS\n - Set FULL_RUN = True for the real 25k/25k benchmark (~40 min on a T4).\n - Swap MODEL_NAME to 'roberta-base' (target_modules=['query','value']) or\n   'answerdotai/ModernBERT-base' for an 8k context window — no truncation.\n - Head+tail truncation: first 128 + last 128 tokens, motivated by section 9.\n - Ablate LoRA rank r in {4, 8, 16, 64} and plot accuracy vs trainable params.\n - Replace the pseudo-label teacher with an ensemble and iterate self-training.\n - Push to the Hub: huggingface_hub.login() then infer_model.push_to_hub(...).\n\"\"\")\n```\n\nWe use the fine-tuned transformer to generate high-confidence pseudo-labels for examples from IMDb’s unlabeled split and add these examples to the TF-IDF training corpus. We compare the augmented classifier against the original baseline to measure whether semi-supervised self-training improves predictive accuracy. Finally, we save the merged DistilBERT model and tokenizer, run sentiment inference on custom reviews, and summarize the performance of all models developed throughout the tutorial.\n\nIn conclusion, we developed a rigorous sentiment classification pipeline that goes well beyond simply fine-tuning a transformer and reporting accuracy. We established a competitive TF-IDF baseline, train DistilBERT efficiently with LoRA, and evaluate both predictive quality and probability reliability while identifying how review length, truncation, and highly confident mistakes influence real-world performance. We also interpreted individual predictions through occlusion-based saliency, tested whether sentiment information is concentrated near the beginning or end of long reviews, and extended supervised learning with high-confidence pseudo-labels from the unlabeled dataset.\n\n**Check out the FULL CODES here**.\n\n**Also, feel free to follow us on**\n\n**and don’t forget to join our**[Twitter](https://x.com/intent/follow?screen_name=marktechpost)\n\n**and Subscribe to**\n\n[150k+ML SubReddit](https://www.reddit.com/r/machinelearningnews/)**. Wait! are you on telegram?**\n\n[our Newsletter](https://www.aidevsignals.com/)\n\n[now you can join us on telegram as well.](https://t.me/machinelearningresearchnews)Need to partner with us for promoting your GitHub Repo OR Hugging Face Page OR Product Release OR Webinar etc.? [Connect with us](https://forms.gle/wbash1wF6efRj8G58)\n\nSana Hassan, a consulting intern at Marktechpost and dual-degree student at IIT Madras, is passionate about applying technology and AI to address real-world challenges. With a keen interest in solving practical problems, he brings a fresh perspective to the intersection of AI and real-life solutions.", "url": "https://wpnews.pro/news/imdb-sentiment-analysis-with-distilbert-lora-tf-idf-baselines-calibration-and", "canonical_source": "https://www.marktechpost.com/2026/08/09/imdb-sentiment-analysis-with-distilbert-lora-tf-idf-baselines-calibration-interpretability-robustness-testing-and-semi-supervised-learning/", "published_at": "2026-08-09 07:17:35+00:00", "updated_at": "2026-08-09 13:04:01.826145+00:00", "lang": "en", "topics": ["machine-learning", "large-language-models", "natural-language-processing", "ai-research"], "entities": ["Stanford NLP IMDb", "DistilBERT", "LoRA", "PEFT", "TF-IDF", "Logistic Regression"], "alternates": {"html": "https://wpnews.pro/news/imdb-sentiment-analysis-with-distilbert-lora-tf-idf-baselines-calibration-and", "markdown": "https://wpnews.pro/news/imdb-sentiment-analysis-with-distilbert-lora-tf-idf-baselines-calibration-and.md", "text": "https://wpnews.pro/news/imdb-sentiment-analysis-with-distilbert-lora-tf-idf-baselines-calibration-and.txt", "jsonld": "https://wpnews.pro/news/imdb-sentiment-analysis-with-distilbert-lora-tf-idf-baselines-calibration-and.jsonld"}}