Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Gretelai Gretel synthetics ACTGANSynthesizer Sample

From Leeroopedia
Knowledge Sources
Domains Synthetic_Data, GAN, Tabular_Data
Last Updated 2026-02-14 19:00 GMT

Overview

Concrete tool for generating synthetic tabular data from a trained ACTGAN model, with optional conditional generation support, provided by the gretel-synthetics library.

Description

The ACTGANSynthesizer.sample(n, conditions) method generates n rows of synthetic data by running the trained generator in inference mode. It constructs conditional vectors based on the provided conditions and conditional vector type, generates data in batches, applies per-column activation functions, concatenates all batches, trims to exactly n rows, and inverse-transforms the encoded output back to the original data representation using DataTransformer.inverse_transform().

The method handles four distinct conditional generation scenarios:

  1. SINGLE_DISCRETE without user conditions: Samples conditional vectors from the learned discrete column distribution each batch.
  2. SINGLE_DISCRETE with user conditions: Sets fixed_cond_vec_torch=None and relies on SDV's rejection sampling to enforce conditions.
  3. ANYWAY without user conditions: Uses a zero conditional vector (fully unconditional).
  4. ANYWAY with user conditions: Encodes the conditions into a fixed conditional vector and repeats it for the entire batch.

The DataTransformer.inverse_transform() method reverses the encoding by iterating over each column's transform info, extracting the relevant slice of the encoded data, applying the reverse transformation (BayesianGMM reverse for continuous, OHE/Binary reverse for discrete), and reconstructing a DataFrame with original column names and dtypes.

Usage

Call model.sample(n) or model.sample(n, conditions={"col": value}) after fitting the ACTGAN model.

Code Reference

Source Location

  • Repository: gretel-synthetics
  • File: src/gretel_synthetics/actgan/actgan.py (lines 839-935), src/gretel_synthetics/actgan/data_transformer.py (lines 298-330)

Signature

# ACTGANSynthesizer.sample
@random_state
def sample(
    self,
    n: int,
    conditions: Optional[dict] = None,
) -> pd.DataFrame:

# DataTransformer.inverse_transform
def inverse_transform(self, data: DFLike, sigmas=None) -> DFLike:

Import

from gretel_synthetics.actgan.actgan_wrapper import ACTGAN
# sample is called through the ACTGAN wrapper:
# ACTGAN.sample() -> _ACTGANModel._sample() -> ACTGANSynthesizer.sample()

I/O Contract

Inputs

Name Type Required Description
n int Yes Number of rows of synthetic data to generate.
conditions Optional[dict] No Dictionary mapping column names to desired values. When provided, the generator is biased toward producing rows matching these conditions. For SINGLE_DISCRETE mode, conditions are handled by SDV rejection sampling. For ANYWAY mode, conditions are encoded into the conditional vector. Column names for numeric columns must have '.value' appended if calling ACTGANSynthesizer directly.

Outputs

Name Type Description
(return value) pd.DataFrame A DataFrame with n rows of synthetic data in the same format and column names as the original training data. Column dtypes are restored to match the original data.

Internal Sampling Flow

# Simplified pseudocode of the sample method
def sample(self, n, conditions=None):
    # 1. Prepare conditional vector
    if conditions is not None:
        if ANYWAY mode:
            cond_vec = self._transformer.convert_conditions(conditions)
            fixed_cond_vec_torch = repeat(cond_vec, batch_size)
        else:
            fixed_cond_vec_torch = None  # rely on rejection sampling
    else:
        if ANYWAY mode:
            fixed_cond_vec_torch = zeros(batch_size, cond_vec_dim)
        else:
            fixed_cond_vec_torch = None

    # 2. Switch generator to eval mode
    self._generator.eval()

    # 3. Generate in batches
    steps = ceil(n / batch_size)
    data = []
    for _ in range(steps):
        if fixed_cond_vec_torch is None:
            cond_vec = sample_original_condvec(batch_size)  # SINGLE_DISCRETE
        else:
            cond_vec = fixed_cond_vec_torch

        noise = Normal(0, 1).sample([batch_size, embedding_dim])
        _, fakeact = self._apply_generator(noise, cond_vec)
        data.append(fakeact.detach().cpu().numpy())

    # 4. Switch back to train mode
    self._generator.train()

    # 5. Concatenate and trim
    data = concatenate(data)[:n]

    # 6. Inverse transform to original representation
    original_data = self._transformer.inverse_transform(data)

    # 7. Optionally force condition values
    if force_conditioning and conditions:
        for col, val in conditions.items():
            original_data[col] = val

    return original_data

Usage Examples

Basic Example

import pandas as pd
from gretel_synthetics.actgan.actgan_wrapper import ACTGAN

# Assume model is already trained
data = pd.read_csv("training_data.csv")
model = ACTGAN(epochs=100)
model.fit(data)

# Generate 1000 synthetic rows
synthetic_data = model.sample(1000)
print(synthetic_data.head())

Conditional Generation Example

from gretel_synthetics.actgan.actgan_wrapper import ACTGAN
from gretel_synthetics.actgan.structures import ConditionalVectorType

model = ACTGAN(
    epochs=100,
    conditional_vector_type=ConditionalVectorType.ANYWAY,
    conditional_select_mean_columns=1.0,
)
model.fit(data)

# Generate rows where "city" column is "New York"
synthetic = model.sample(500, conditions={"city": "New York"})

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment