Concepts / Rule-Based Data Synthesis

Rule-Based Data Synthesis

A template is a predefined structure with placeholder fields, and it does not contain real data by itself.

  • Programming

Designing the Shape Before the Data

Imagine you need thousands of realistic records to test or train a system, but you cannot use real customer records to do it. One practical answer is to stop thinking about individual records and instead design the shape a record must have. Once that shape is fixed, you can produce as many instances of it as you like. This article follows that idea from the simplest case, a fill-in-the-blank form, through to the way transformed copies of existing data can expand a dataset.

Definition

A template is a predefined structure with placeholder fields, and it does not contain real data by itself. Rule-based data synthesis is the practice of creating data by filling such a structure according to defined rules.

Watching a Generator Fill a Template

The source gives a concrete case: to create a credit card transaction, you start with a transaction template and use a random generator, such as Faker, to populate each field in that template. The template is the fixed part. The random generator is the source of variation. Each field in the template is a slot, and the generator decides what goes into each slot.

Work through a small case. The transaction template has three slots: a transaction identifier, a date, and a time. Before any generation happens, the template is only a description of what a transaction must look like. Nothing in it is a real transaction yet. The template also states the expected format of each slot, so the generator must produce values in those formats.

Transactiontemplatethree empty slotstransaction_id slotidentifier formatRandom generatorfor example FakerSynthetic recordall slots filleddate slotdate formattime slottime formatvisitthenthengenerator picks valuesassembles
What happens next, from the blank template to the finished record, as the generator visits each placeholder and picks a value?

Notice the order of events in this pipeline. The structure of the output is decided by the template before generation starts. The variety of the output comes from the generator, one field at a time. This split is what makes the method predictable in shape and varied in content.

Where Structure and Variety Come From

A random generator fills each template field, so the structure stays fixed while the values vary. You can think of the template as a contract about layout and format, and the generator as the component that produces many different acceptable values under that contract. Changing the generator changes the values; changing the template changes the shape of every record produced from it.

Template-based generation separates structure from variety. The template fixes the fields and their expected formats, and the random generator supplies the values. Because the structure is decided first, every output has the same fields in the same order, even though no two outputs need to contain the same values.

Tracing Two Generated Records

Now run the same template twice. The field names stay exactly the same in both records, while the values assigned to those fields differ. The map below shows which value each field receives in each generated record.

transaction_idTX-1042transaction_idTX-9317date2024-03-07date2024-11-19time14:25time08:03
Which field gets which value, and how do the same field names carry different values across several generated records?

Each column in the map has the same keys, which is the structure the template guarantees. The values in each column come from the generator and are different from one another. A learner reading only one record cannot tell which values were drawn from the generator and which were fixed by the template, which is why it helps to compare several records side by side.

Templates for Documents and Grammars

Templates are not limited to records with named fields. The source lists two other broad uses. The subsections below describe each one.

Augmentation: Starting From Existing Data

Templates create data from nothing but a structure. Data augmentation works differently: it starts with data you already have and produces new data by applying simple transformations to it. The source describes this as procedurally generating new data from existing data.

For images, the transformations named in the source are random rotation, cropping, scaling, and erasing part of the image. Each transformed copy is a new training example that is derived from an original one. The diagram below shows how one original image becomes several new examples.

original cat photolabel: catflipped cat photolabel: catcropped cat photolabel: catrotated cat photolabel: catflipcroprotate
How does one original example become several new examples after each simple transformation is applied?

The reason this works is that these transformations are expected to preserve meaning. The source gives two examples: a flipped image of a cat should still be a cat, and a slightly cropped image of a soccer game should still be a soccer game. The label stays the same while the pixels change.

Why Synthetic Data Comes First

The main practical motivation appears in the source's discussion of sensitive data. Transaction data is sensitive, so many fraud detection models are first trained on synthetic transaction data generated from templates like the one above. The purpose is to show that the model is feasible before it is given access to real data.

Think of the project timeline in three phases. First, the team designs the template and generates synthetic records, so the pipeline and model can be built and tested. Second, the model is shown to work on data that has the right shape. Third, when access to real data is permitted, the same model is run on that data. Synthetic data fills the gap between the start of the project and the arrival of real data.

The source also points to a large-scale use of this idea. DeepMind trained an Olympiad-level geometry model, AlphaGeometry, using 100 million synthetic examples. That scale shows why generating data from structured templates can be worth the effort when real examples are limited.

Template Versus Filled Record

The two kinds of data are easy to confuse, because both have the same field names. The table sets them side by side, using what the source describes about each.

AspectTemplateFilled synthetic record
Contains real dataNo, it holds placeholder fields onlyYes, each field holds an actual generated value
RoleDefines the structure and expected formatsAn instance that can be used downstream
Source of variationNone, it is fixedThe random generator fills each field
Example contenttransaction_id, date, time slotsTX-1042, 2024-03-07, 14:25

Template and filled record, as described in the source and the worked case above

Mistakes Learners Make With Templates

  • Common mistake

    Treating the template as if it were a real record

    What it looks like

    Reading the bracketed transaction template and assuming it describes an actual purchase.

    Why it fails

    The source states that a template does not contain real data by itself. It only describes what a transaction must look like.

    Do this instead

    Keep the template and the filled record separate in your head: one holds placeholders, the other holds values.

  • Common mistake

    Assuming the generator decides the structure

    What it looks like

    Expecting a generated record to gain a new field because the random values look unusual.

    Why it fails

    The structure is decided by the template before generation happens. The generator only fills existing slots.

    Do this instead

    Change the template if the shape must change; change the generator if only the values should vary.

  • Common mistake

    Applying any transformation to augment data

    What it looks like

    Flipping a photo of a cat and labeling it as something else, or cropping away the only part that shows the subject.

    Why it fails

    Augmentation is expected to preserve the label. A transformation that breaks the meaning of the example produces a wrong training example.

    Do this instead

    Choose transformations that keep the label true, such as the flips and slight crops the source describes.

  • Common mistake

    Using a single generated record to judge the variety of the output

Designing Your Own Template

MEDIUM

Design a template for a simple customer support ticket with four slots: a ticket identifier, a category, a priority, and a short description. For each slot, state the expected format, then describe how a random generator would fill it. Finally, explain which parts of the template would stay the same across every generated ticket.

Hints
  • Start by writing the template with placeholder names in brackets, the way the source's transaction template does.
  • A slot's format tells the generator what kind of value is allowed, such as a fixed-length identifier or a value from a set of categories.
  • Everything the template spells out, including field names and separators, stays fixed across records.

Key Takeaways

  • A template is a predefined structure with placeholder fields and does not contain real data by itself.
  • A random generator fills each template field, so the structure stays fixed while the values vary.
  • Templates can also produce fixed-layout documents and data that follows a grammar or syntax, such as regular expressions and math equations.
  • Data augmentation creates new examples from existing data by applying transformations such as rotation, cropping, scaling, or erasing, which are expected to preserve the label.
  • Synthetic data lets a model be built and shown to be feasible before real, sensitive data is available, as in fraud detection work with synthetic transactions.

Related concepts

More concepts starting with R