Topic 2 of 5 · 5 min read
Scrubbing PII from fine-tuning datasets
Goal: Clean a fine-tuning dataset of direct identifiers and duplicates and record what was removed and why.
The cheapest privacy control for a model is to keep personal data out of the training set in the first place: detect it, remove or replace it, drop duplicates, and write down what you did. Once a record is in the weights, you cannot cleanly take it out (see the unlearning topic).
Check yourself · 1 of 3
What should happen before technical scrubbing of a fine-tuning dataset?
Why it matters
Memorization, the previous topic, only matters for what was in the data. Every direct identifier you remove before training is one fewer thing the model can leak, and one fewer record you may have to answer for in an erasure request.
How to do it
- Check the right to use the data first. Was it collected for this purpose? Does the privacy notice or contract allow model training? Is there a consent flag or an opt-out? Exclude records without a lawful basis. This check is not technical, and no scrubber replaces it.
- Detect. Combine tools: regular expressions for emails, phones, card numbers and ids; a named-entity recognizer for names, places and organizations; and a custom list for your own id formats. No single detector finds everything, so measure recall on a sample you labeled by hand.
- Replace, do not just delete. Swap values for typed placeholders such as
[EMAIL]or[PERSON_1]. The text stays readable and the model learns the shape, not the person. Keep the same placeholder for the same person inside one record. - Deduplicate. Remove exact copies and near copies. Duplicates are the main driver of memorization, and removing them also tends to improve the model.
- Log it. Record counts per category, the tool and version, the sample recall, and what was dropped for consent. Do not store the removed values in the log.
Before and after
import re
EMAIL = re.compile(r"[\w.+-]+@[\w-]+\.[\w.]+")
def scrub(text: str) -> str:
return EMAIL.sub("[EMAIL]", text)
rows = {scrub(r["text"]) for r in rows if r["consent"]}
The weak version trained on raw rows. This one drops rows without consent, masks emails and removes exact duplicates after masking. It still misses names and free-text addresses, so it is a start, not a finish.
What goes wrong
- Scrubbing only the obvious fields. Names and details inside free text slip through.
- Masking before deduplicating. Two near-identical rows differ only by an email and both survive.
- Re-identification. A job title, a rare city and a date together can point to one person even with no name.
Examples
A scrub step with a consent filter
clean = []
seen = set()
for row in rows:
if not row["training_consent"]:
dropped["no_consent"] += 1
continue
text = mask_pii(row["text"]) # regex + NER
if text in seen:
dropped["duplicate"] += 1
continue
seen.add(text)
clean.append({"text": text})
The loop counts why each row was dropped. The counts go to the dataset record; the original values do not.
What the regex missed
A team masks emails and phone numbers, then reviews 200 random rows by hand. Nine still hold a full name in free text, such as "my neighbour Tomas Reyes said". They add an entity recognizer, re-measure on the same sample and keep the sample as a regression set for the next dataset.
Key points
- Decide whether you may use each record for training before any technical cleaning.
- Combine regexes, an entity recognizer and custom rules, and measure recall on a hand-labeled sample.
- Replace identifiers with typed placeholders so the text stays useful and consistent within a record.
- Deduplicate exact and near-duplicate records, because repetition drives memorization.
- Keep a log of counts and tools, never of the removed values.
Common mistakes
- Treating consent and purpose as someone else's problem and scrubbing only the text.
- Trusting one regex pass without measuring what it missed.
- Deleting whole values so sentences become unreadable and lose training value.
- Logging the removed personal data in the audit trail.
Check yourself · 2 of 3
Why replace an email with [EMAIL] instead of deleting the whole sentence?
Check yourself · 3 of 3
Why remove duplicate records before fine-tuning?
Practice items, not counted towards your mastery.
Start here
- Extracting Training Data from Large Language Models (Carlini et al.) (opens in a new tab)PaperFree
Shows that a language model can emit verbatim training sequences, including personal details, to a plain attacker.
- The Secret Sharer: measuring unintended memorization (Carlini et al.) (opens in a new tab)PaperFree
Introduces canary insertion and the exposure metric used to test whether a model memorized a planted secret.
- Quantifying Memorization Across Neural Language Models (opens in a new tab)PaperFree
Measures how memorization grows with model size, repeated data and prompt length.
More for this topic
- Deduplicating Training Data Makes Language Models Better (Lee et al.) (opens in a new tab)PaperFree
Shows that removing duplicate text reduces verbatim emission of training data.
Where to go in your country
- EDPB Opinion 28/2024 on data protection aspects of AI models (opens in a new tab)DocsFreeEU
The EU regulators' view on when a trained model counts as anonymous and what that means for erasure.