Skip to content

Commit 52cff6d

Browse files
committed
Fix notebook Part 3 colorbar overlap and Part 4 ELECTRA examples
Part 3: Place colorbar in dedicated axes to prevent overlay on right heatmap. Part 4: Replace chef cooked/ate examples (too similar) with doctor/musician context that triggers fake detection. Update visualization sentence to more plausible research context.
1 parent 776a8bf commit 52cff6d

1 file changed

Lines changed: 25 additions & 16 deletions

File tree

slides/week6/bert_variants_demo.ipynb

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -272,19 +272,20 @@
272272
" ax.set_yticklabels(labels, fontsize=7)\n",
273273
" return im\n",
274274
"\n",
275-
"fig, axes = plt.subplots(1, 2, figsize=(16, 7))\n",
275+
"fig, axes = plt.subplots(1, 2, figsize=(14, 6))\n",
276276
"\n",
277277
"print(\"Computing embeddings for BERT...\")\n",
278278
"bert_embs = get_embeddings(\"bert-base-uncased\", sentences, pooling='cls')\n",
279-
"plot_similarity(bert_embs, \"BERT [CLS] Similarity\", axes[0])\n",
279+
"im1 = plot_similarity(bert_embs, \"BERT [CLS] Similarity\", axes[0])\n",
280280
"\n",
281281
"print(\"Computing embeddings for RoBERTa...\")\n",
282282
"roberta_embs = get_embeddings(\"roberta-base\", sentences, pooling='mean')\n",
283-
"im = plot_similarity(roberta_embs, \"RoBERTa Mean-Pooled Similarity\", axes[1])\n",
283+
"im2 = plot_similarity(roberta_embs, \"RoBERTa Mean-Pooled Similarity\", axes[1])\n",
284284
"\n",
285-
"fig.colorbar(im, ax=axes, shrink=0.8)\n",
286285
"plt.suptitle(\"Semantic Clustering Across Variants\", fontsize=16, color='#00693e')\n",
287-
"fig.tight_layout(rect=[0, 0, 1, 0.95])\n",
286+
"fig.tight_layout(rect=[0, 0, 0.88, 0.95])\n",
287+
"cbar_ax = fig.add_axes([0.90, 0.15, 0.02, 0.7])\n",
288+
"fig.colorbar(im2, cax=cbar_ax)\n",
288289
"plt.show()"
289290
]
290291
},
@@ -338,13 +339,18 @@
338339
" status = \"FAKE\" if prob > 0.5 else \"REAL\"\n",
339340
" print(f\"{token:<12} | {prob:<10.4f} | {status}\")\n",
340341
"\n",
341-
"# Example 1: A natural sentence\n",
342-
"detect_fake_tokens(\"The chef cooked a delicious meal for the guests.\")\n",
342+
"# Example 1: A natural sentence (all tokens should be marked REAL)\n",
343+
"detect_fake_tokens(\"The doctor examined the patient carefully.\")\n",
343344
"\n",
344345
"print(\"\\n\" + \"=\"*40 + \"\\n\")\n",
345346
"\n",
346-
"# Example 2: A sentence with a 'fake' token (replaced 'cooked' with 'ate')\n",
347-
"detect_fake_tokens(\"The chef ate a delicious meal for the guests.\")"
347+
"# Example 2: Same sentence but 'examined' → 'watched' (plausible but wrong in context)\n",
348+
"detect_fake_tokens(\"The doctor watched the patient carefully.\")\n",
349+
"\n",
350+
"print(\"\\n\" + \"=\"*40 + \"\\n\")\n",
351+
"\n",
352+
"# Example 3: 'doctor' → 'musician' (semantically odd with 'patient')\n",
353+
"detect_fake_tokens(\"The musician examined the patient carefully.\")"
348354
]
349355
},
350356
{
@@ -354,21 +360,24 @@
354360
"outputs": [],
355361
"source": [
356362
"# Let's visualize the discriminator's confidence\n",
357-
"sentence = \"The computer programmed the human to write better code.\"\n",
363+
"# 'published' has been swapped in for 'presented' — plausible but detectable\n",
364+
"sentence = \"The researcher published her findings at the annual conference.\"\n",
358365
"inputs = tokenizer(sentence, return_tensors=\"pt\")\n",
359366
"tokens = tokenizer.convert_ids_to_tokens(inputs[\"input_ids\"][0])[1:-1] # Remove CLS/SEP\n",
360367
"\n",
361368
"with torch.no_grad():\n",
362369
" logits = model(**inputs).logits[0][1:-1]\n",
363370
" probs = torch.sigmoid(logits).numpy()\n",
364371
"\n",
365-
"plt.figure(figsize=(10, 5))\n",
372+
"plt.figure(figsize=(12, 5))\n",
366373
"colors = ['#9d162e' if p > 0.5 else '#00693e' for p in probs] # Red for fake, Green for real\n",
367374
"plt.bar(tokens, probs, color=colors)\n",
368-
"plt.axhline(y=0.5, color='gray', linestyle='--')\n",
375+
"plt.axhline(y=0.5, color='gray', linestyle='--', label='Decision boundary')\n",
369376
"plt.ylabel(\"Probability of being 'FAKE'\", color='#9d162e')\n",
370377
"plt.title(\"ELECTRA Discriminator: Identifying 'Fake' Tokens\", fontsize=14)\n",
371378
"plt.ylim(0, 1)\n",
379+
"plt.legend()\n",
380+
"plt.tight_layout()\n",
372381
"plt.show()"
373382
]
374383
},
@@ -378,10 +387,10 @@
378387
"source": [
379388
"### \ud83d\udca1 Discussion\n",
380389
"\n",
381-
"- In the second example, did ELECTRA correctly identify \"ate\" as the fake token? Why might it be suspicious of that word in that context?\n",
382-
"- How is this task different from BERT's masked language modeling? Why might it be more efficient?\n",
383-
"- Try a sentence where you replace a word with a synonym. Does ELECTRA still flag it as fake?\n",
384-
"- What happens if you give it a completely nonsensical sentence?"
390+
"- In Example 2, did ELECTRA flag \"watched\" as suspicious? Doctors *examine* patients — \"watched\" is grammatical but semantically odd in a clinical context.\n",
391+
"- In Example 3, did swapping \"doctor\"\"musician\" get detected? Why might that be easier or harder to detect than swapping the verb?\n",
392+
"- How is this task different from BERT's masked language modeling? Why might it be more efficient? (Hint: how many tokens does each approach learn from per sentence?)\n",
393+
"- Try replacing a word with a close synonym (e.g., \"quickly\"\"rapidly\"). Does ELECTRA flag it?"
385394
]
386395
},
387396
{

0 commit comments

Comments
 (0)