liveBook · Manning

Generative adversarial network

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

A generative adversarial network is a deep learning architecture consisting of a generator and discriminator trained adversarially to produce samples resembling real data.

A generative adversarial network (GAN) uses two deep neural networks: a generator creates samples from random noise, while a discriminator distinguishes them from real examples. They train simultaneously, pushing the generator toward the original data distribution without directly seeing its instances. section 2.3.3

For tabular data, GAN-based generation can expand scarce datasets, strengthen minority classes, create rare edge cases for testing, and produce fictitious records that preserve useful characteristics without reproducing private instances. section 2.3.3

Use synthetic generation when you need to:

  • Increase the number and diversity of available tabular examples. section 2.3.3
  • Generate unusual cases for system testing or support privacy-conscious experimentation. section 2.3.3
Listing 2.5 Generating a synthetic dataset
import pandas as pd
from sdv.metadata import SingleTableMetadata                       #1
from sdv.single_table import TVAESynthesizer                       #2
 
data = pd.read_csv("./german_credit_data.csv", index_col=0)        #3
data = data.reset_index()
print(data.shape)
print(data.head)
 
metadata = SingleTableMetadata()                                   #4
metadata.detect_from_dataframe(data)                               #5
print(metadata.to_dict())                                          #6
metadata.update_column(column_name="Saving accounts", 
                       sdtype="categorical")                       #7
synthesizer = TVAESynthesizer(metadata, epochs=10_000)             #8
synthesizer.fit(data)                                              #9
synthetic_data = synthesizer.sample(num_rows=10_000)               #10
synthetic_data.head()

Shows a related SDV workflow for fitting a tabular synthesizer to German credit data and sampling synthetic rows. section 2.3.3

Listing 2.6 Testing a synthetic dataset using adversarial validation
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import roc_auc_score
 
X = pd.concat([synthetic_data, data])                               #1
categorical_columns = [
    "Sex", "Housing", "Saving accounts",
    "Checking account", "Purpose"]
X_encoded = pd.get_dummies(X, columns=categorical_columns)          #2
X= X.drop("index", axis="columns") 
y = [0] * len(synthetic_data) + [1] * len(data)
 
model = RandomForestClassifier()
cv_preds = cross_val_predict(
    model,                                                          #3
    X_encoded,                                                      #3
    y,                                                              #3
    cv=5,                                                           #3
    n_jobs=-1,                                                      #3
    method="predict_proba"                                          #3
)                                                                   #3
roc_adv_score = roc_auc_score(y_true=y, y_score=cv_preds[:, 1])     #4
print(f"roc auc adv score: {roc_adv_score:0.3f}")

Demonstrates adversarial validation for testing whether synthetic and original datasets remain distinguishable. section 2.3.3

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Defines generator–discriminator interaction and random-noise generation but stops short of mathematical or implementation details.
Examples
Uses tabular scarcity, minority classes, edge cases, privacy-preserving fictitious records, and German credit data generation.
What you can do after
Evaluate/understand how adversarial networks generate synthetic records and how synthetic data can be tested.
How it connects
Connects GANs with deep learning, synthetic data, generator, discriminator, unsupervised learning, and variational autoencoders.

Also explained in

Compare how 12 books explain generative adversarial network →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.