Skip to content

Commit a92ab78

Browse files
committed
Fix ELECTRA examples with locally-verified sentences that trigger detection
Part 4: Replaced doctor/watched examples (no detection) with sentences verified locally against electra-small-discriminator: - 'third quarter' → 'third half': half flagged at 0.586 (semantic violation) - 'mysterious case' → 'mysterious mystery': flagged at 0.570 (redundancy) - Visualization: 'painted' → 'created' flags 'landscape' at 0.526 Updated discussion questions to highlight context propagation.
1 parent ff7dd63 commit a92ab78

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

slides/week6/bert_variants_demo.ipynb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,17 +340,18 @@
340340
" print(f\"{token:<12} | {prob:<10.4f} | {status}\")\n",
341341
"\n",
342342
"# Example 1: A natural sentence (all tokens should be marked REAL)\n",
343-
"detect_fake_tokens(\"The doctor examined the patient carefully.\")\n",
343+
"detect_fake_tokens(\"The company announced record profits for the third quarter.\")\n",
344344
"\n",
345345
"print(\"\\n\" + \"=\"*40 + \"\\n\")\n",
346346
"\n",
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",
347+
"# Example 2: 'quarter' replaced with 'half' — grammatical but semantically wrong\n",
348+
"# (companies report in quarters, not halves)\n",
349+
"detect_fake_tokens(\"The company announced record profits for the third half.\")\n",
349350
"\n",
350351
"print(\"\\n\" + \"=\"*40 + \"\\n\")\n",
351352
"\n",
352-
"# Example 3: 'doctor' → 'musician' (semantically odd with 'patient')\n",
353-
"detect_fake_tokens(\"The musician examined the patient carefully.\")"
353+
"# Example 3: 'case' replaced with 'mystery' — creates redundancy with 'mysterious'\n",
354+
"detect_fake_tokens(\"The detective solved the mysterious mystery in just two days.\")"
354355
]
355356
},
356357
{
@@ -360,8 +361,9 @@
360361
"outputs": [],
361362
"source": [
362363
"# Let's visualize the discriminator's confidence\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",
364+
"# 'painted' has been swapped to 'created' — plausible synonym, but ELECTRA\n",
365+
"# flags 'landscape' as suspicious (you 'paint' a landscape, not 'create' one)\n",
366+
"sentence = \"The artist created a beautiful landscape of the countryside.\"\n",
365367
"inputs = tokenizer(sentence, return_tensors=\"pt\")\n",
366368
"tokens = tokenizer.convert_ids_to_tokens(inputs[\"input_ids\"][0])[1:-1] # Remove CLS/SEP\n",
367369
"\n",
@@ -387,8 +389,9 @@
387389
"source": [
388390
"### \ud83d\udca1 Discussion\n",
389391
"\n",
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+
"- In Example 2, did ELECTRA flag \"half\" as fake? Why is \"third half\" suspicious even though it's grammatically correct?\n",
393+
"- In Example 3, the discriminator flags \"mysterious\" rather than \"mystery\" — why might it flag the *context* around a fake token rather than the fake token itself?\n",
394+
"- In the bar chart, ELECTRA flags \"landscape\" as fake even though it's the *original* word. The swap was \"painted\"\"created\". Why does suspicion propagate to neighboring tokens?\n",
392395
"- 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",
393396
"- Try replacing a word with a close synonym (e.g., \"quickly\"\"rapidly\"). Does ELECTRA flag it?"
394397
]

0 commit comments

Comments
 (0)