Sub topic 14
Generating Synthetic Training Text
This chapter assumes you know Python, supervised text classification, precision, recall, and F1. You should also understand that weak labels are noisy signals rather than verified truth.
Synthetic text is text created by a program or language model instead of collected directly from users. It can increase the size of a training set after labeling functions identify useful classes. For example, a small set of weakly labeled support messages can guide the creation of more messages about refunds, damaged products, or positive experiences.
Start with the task, not the generator
Synthetic examples should represent cases that your classifier must recognize. Define the class, the required meaning, and the allowed variation before generating text.
A useful record contains:
- The generated text.
- Its intended label.
- The source template, prompt, or labeling rule.
- A generation version.
- Optional confidence or review status.
Templates are simple and reproducible. They are useful when the task has a controlled vocabulary. This program creates positive and negative product reviews:
import random
random.seed(7)
templates = {
"positive": [
"The {product} has a clear display and a reliable battery.",
"I love the {product}; it works exactly as expected.",
"The {product} is excellent for daily use.",
],
"negative": [
"The {product} arrived broken and needs a refund.",
"I am disappointed because the {product} stopped working.",
"The {product} has a terrible battery and poor construction.",
],
}
products = ["phone", "tablet", "headset"]
examples = []
for label, patterns in templates.items():
for pattern in patterns:
for product in products:
examples.append({
"text": pattern.format(product=product),
"label": label,
"source": "template-v1",
})
for example in examples:
print(example)This approach gives control over class balance and vocabulary. However, the generated sentences may be repetitive. A classifier can learn template fragments instead of the underlying concept.
Use language models carefully
A language model can create more varied text. Give it a strict task definition, a target label, and constraints such as length, tone, and required evidence. Ask for structured output, such as JSON, so that malformed responses can be rejected.
Generate several examples for each class, including difficult cases. A positive review might mention one minor problem, while a negative review might contain polite language. These cases reduce the risk that the classifier treats individual words as labels.
Keep the generator separate from the classifier. Do not ask the generator to produce examples and then evaluate them with the same model without independent checks. The generator can repeat its own mistakes.
Validate before training
Treat every synthetic example as untrusted until it passes checks:
- Remove duplicates and near-duplicates.
- Verify that the text matches the requested label.
- Reject empty, malformed, or excessively short text.
- Check class balance and vocabulary overlap.
- Review a random sample by hand.
- Compare performance on real, hand-labeled data.
A gold set must contain real examples that were not used to design the generator. Use it to measure whether synthetic data improves precision, recall, or F1. Keep the gold set unchanged across experiments.
Synthetic generation works best as an augmentation step, not as a replacement for real labeled data. Generate examples for rare or ambiguous cases, then let real examples determine whether the added data helps.
Exercise
Why should synthetic examples be evaluated on a separate real gold set?
Show answer
Because synthetic examples may share the generator's vocabulary and errors. A real gold set tests whether the classifier generalizes to the data it will actually receive.