Latent spaces: VAEs, GANs and diffusion
A generative model maps simple random noise to data. Explore what a latent space is and where it stops being realistic, then see how VAEs, GANs and diffusion models attack the same problem.
What runs in CodeSplash
- Synthetic data — Gaussian mixture Verified in Python and R
You will be able to
- Describe a generative model as a map from noise to data
- Explain why some points in a latent space decode to nonsense
- Compare VAEs, GANs and diffusion models by how they train and fail
- Say which generators CodeSplash includes, and judge any generator with the same scores
Read first
From noise to data
ReadA generative modelGenerative modelA model of how data arises, which you can sample to get new examples rather than predictions about existing ones. is a machine for turning simple randomness into data. Every one you've met in this track works this way.
A mixture draws a group label, then a normal sample. A copula draws normal scores, then maps them through quantiles. Neural generators do the same with far more flexible maps: start with noise, and push it through a network until it looks like an image, a sentence or a table row.
The space where the noise lives is the latent spaceLatent spaceA compact set of coordinates from which a model can rebuild data; every point decodes to something, realistic or not..
What's at the center?
PredictPredict first
Train on a set of curves, each with one narrow bump at a different place. Find the center of a simple linear latent space, and decode it. What do you get?
Reveal
A blurry average: a broad, low hump. The average of many narrow bumps looks like none of them. Being in the center doesn't make a point typical.
Walk around a latent space
ExploreEvery point in this space decodes to a curve. Move the two coordinates and compare the decoded curve with the nearest training curve.
DExplorable · A toy latent space
If every point in a latent space decodes to something, does every point decode to something real?
- Decoded from your point
- Nearest training curve
- Distance to nearest
- 0.093
- Peaks
- 1
- Variance captured
- 0.81
- Puzzle: Find a point inside the plotted range that decodes to something unlike the training data.
The training data are 150 bump curves of different centers and widths. Two coordinates capture 81% of their variance, and this point decodes to a curve with 1 peak, 0.093 from the nearest training curve. It looks like the data.
Find a point that decodes to something unlike the training data. Then press fall off the data. The decoder still returns an answer, with the same confidence. It has no way of saying "I've never seen this".
Three ways to close the holes
Read| Model | How it trains | How it tends to fail |
|---|---|---|
| Variational autoencoderVariational autoencoderAn autoencoder trained so its latent space is smooth and close to a normal distribution, which lets you sample from it. | Rebuild each example, while keeping the latent space close to a normal | Blurry output |
| GANGenerative adversarial networkA generator learns to fool a discriminator that is trained to tell its output from real data. | A generator tries to fool a discriminator | Unstable training; drops rare kinds of example |
| Diffusion modelDiffusion modelLearns to reverse a gradual noising process, then generates by starting from pure noise and denoising in steps. | Learn to undo noise, a small step at a time | Slow to sample; can memorize examples |
Each is a way to make most of the latent space decode to something realistic. None of them removes the risk completely.
What's in the app
ReadCodeSplash includes three table generators: the copula, sequential trees and the Gaussian mixture. Two neural generators sit beside them in the Synthetic studio with an Experimental badge: a WGAN-GP and a TabDDPM-style diffusion model. Discover's VAE builds a map of your data and can also sample new rows from its prior. There are no generators for images, audio or video.
A mixture is a small latent-variable model, with the group label as its latent variable. On iris, it uses = 3 components and draws 120 rows.
120 synthetic rows over 4 columns from 120 training rows with a 3-component Gaussian mixture (full covariances). Numeric columns differ from the data by a mean Kolmogorov–Smirnov distance of 0.119.
- Synthetic rows
- 120
- Mean KS distance
- 0.1187
- Mean TV distance
- —
- Rows copied from the data
- 0
Plots
View data
| Density | Source | sepal_length |
|---|---|---|
| 0.2206 | real | 4.357 |
| 0.0735 | real | 4.470 |
| 0.2206 | real | 4.583 |
| 0.1471 | real | 4.697 |
| 0.2941 | real | 4.810 |
| 0.3676 | real | 4.923 |
| 0.5147 | real | 5.037 |
| 0.7353 | real | 5.150 |
| 0.0735 | real | 5.263 |
| 0.2941 | real | 5.377 |
| 0.4412 | real | 5.490 |
| 0.2941 | real | 5.603 |
| 0.5147 | real | 5.717 |
| 0.5147 | real | 5.830 |
| 0.2206 | real | 5.943 |
| 0.6618 | real | 6.057 |
| 0.2941 | real | 6.170 |
| 0.5882 | real | 6.283 |
| 0.5147 | real | 6.397 |
| 0.2941 | real | 6.510 |
| 0.0735 | real | 6.623 |
| 0.5147 | real | 6.737 |
| 0.3676 | real | 6.850 |
| 0 | real | 6.963 |
| 0.0735 | real | 7.077 |
| 0.0735 | real | 7.190 |
| 0.0735 | real | 7.303 |
| 0.0735 | real | 7.417 |
| 0 | real | 7.530 |
| 0.2941 | real | 7.643 |
| 0.1471 | synthetic | 4.357 |
| 0.0735 | synthetic | 4.470 |
| 0.0735 | synthetic | 4.583 |
| 0.2941 | synthetic | 4.697 |
| 0.2206 | synthetic | 4.810 |
| 0.3676 | synthetic | 4.923 |
| 0.5882 | synthetic | 5.037 |
| 1.103 | synthetic | 5.150 |
| 0.3676 | synthetic | 5.263 |
| 0.3676 | synthetic | 5.377 |
| 0.5147 | synthetic | 5.490 |
| 0.5147 | synthetic | 5.603 |
| 0.5882 | synthetic | 5.717 |
| 0.1471 | synthetic | 5.830 |
| 0.4412 | synthetic | 5.943 |
| 0.4412 | synthetic | 6.057 |
| 0.2941 | synthetic | 6.170 |
| 0.2941 | synthetic | 6.283 |
| 0.4412 | synthetic | 6.397 |
| 0.5882 | synthetic | 6.510 |
| 0.0735 | synthetic | 6.623 |
| 0.0735 | synthetic | 6.737 |
| 0.1471 | synthetic | 6.850 |
| 0.1471 | synthetic | 6.963 |
| 0 | synthetic | 7.077 |
| 0.2941 | synthetic | 7.190 |
| 0.0735 | synthetic | 7.303 |
| 0.0735 | synthetic | 7.417 |
| 0 | synthetic | 7.530 |
| 0.0735 | synthetic | 7.643 |
View data
| Density | Source | sepal_width |
|---|---|---|
| 0.1042 | real | 2.040 |
| 0 | real | 2.120 |
| 0.3125 | real | 2.200 |
| 0.2083 | real | 2.280 |
| 0.2083 | real | 2.360 |
| 0 | real | 2.440 |
| 0.8333 | real | 2.520 |
| 0.5208 | real | 2.600 |
| 0.6250 | real | 2.680 |
| 1.354 | real | 2.760 |
| 0 | real | 2.840 |
| 0.7292 | real | 2.920 |
| 1.979 | real | 3 |
| 1.042 | real | 3.080 |
| 0 | real | 3.160 |
| 1.146 | real | 3.240 |
| 0.4167 | real | 3.320 |
| 1.146 | real | 3.400 |
| 0.6250 | real | 3.480 |
| 0.2083 | real | 3.560 |
| 0 | real | 3.640 |
| 0.1042 | real | 3.720 |
| 0.5208 | real | 3.800 |
| 0.1042 | real | 3.880 |
| 0.1042 | real | 3.960 |
| 0 | real | 4.040 |
| 0 | real | 4.120 |
| 0.1042 | real | 4.200 |
| 0 | real | 4.280 |
| 0.1042 | real | 4.360 |
| 0 | synthetic | 2.040 |
| 0.2083 | synthetic | 2.120 |
| 0.2083 | synthetic | 2.200 |
| 0 | synthetic | 2.280 |
| 0.3125 | synthetic | 2.360 |
| 0.1042 | synthetic | 2.440 |
| 0.5208 | synthetic | 2.520 |
| 0.4167 | synthetic | 2.600 |
| 0.9375 | synthetic | 2.680 |
| 0.5208 | synthetic | 2.760 |
| 0.9375 | synthetic | 2.840 |
| 1.250 | synthetic | 2.920 |
| 0.9375 | synthetic | 3 |
| 0.8333 | synthetic | 3.080 |
| 0.6250 | synthetic | 3.160 |
| 0.5208 | synthetic | 3.240 |
| 0.3125 | synthetic | 3.320 |
| 1.250 | synthetic | 3.400 |
| 0.7292 | synthetic | 3.480 |
| 0.4167 | synthetic | 3.560 |
| 0.6250 | synthetic | 3.640 |
| 0.2083 | synthetic | 3.720 |
| 0 | synthetic | 3.800 |
| 0.2083 | synthetic | 3.880 |
| 0.2083 | synthetic | 3.960 |
| 0.1042 | synthetic | 4.040 |
| 0 | synthetic | 4.120 |
| 0 | synthetic | 4.200 |
| 0.1042 | synthetic | 4.280 |
| 0 | synthetic | 4.360 |
View data
| Density | Source | petal_length |
|---|---|---|
| 0.1293 | real | 1.197 |
| 0.7759 | real | 1.390 |
| 0.6466 | real | 1.583 |
| 0.1293 | real | 1.777 |
| 0.0862 | real | 1.970 |
| 0 | real | 2.163 |
| 0 | real | 2.357 |
| 0 | real | 2.550 |
| 0 | real | 2.743 |
| 0.0431 | real | 2.937 |
| 0 | real | 3.130 |
| 0 | real | 3.323 |
| 0.1293 | real | 3.517 |
| 0.0862 | real | 3.710 |
| 0.2586 | real | 3.903 |
| 0.0862 | real | 4.097 |
| 0.2155 | real | 4.290 |
| 0.4310 | real | 4.483 |
| 0.2155 | real | 4.677 |
| 0.3448 | real | 4.870 |
| 0.4741 | real | 5.063 |
| 0.1724 | real | 5.257 |
| 0.1724 | real | 5.450 |
| 0.3448 | real | 5.643 |
| 0.0862 | real | 5.837 |
| 0.1724 | real | 6.030 |
| 0.0431 | real | 6.223 |
| 0 | real | 6.417 |
| 0.0862 | real | 6.610 |
| 0.0431 | real | 6.803 |
| 0.2155 | synthetic | 1.197 |
| 0.8190 | synthetic | 1.390 |
| 0.7328 | synthetic | 1.583 |
| 0.1724 | synthetic | 1.777 |
| 0 | synthetic | 1.970 |
| 0 | synthetic | 2.163 |
| 0 | synthetic | 2.357 |
| 0 | synthetic | 2.550 |
| 0.0431 | synthetic | 2.743 |
| 0 | synthetic | 2.937 |
| 0.1293 | synthetic | 3.130 |
| 0 | synthetic | 3.323 |
| 0.0431 | synthetic | 3.517 |
| 0.1293 | synthetic | 3.710 |
| 0.3017 | synthetic | 3.903 |
| 0.1293 | synthetic | 4.097 |
| 0.1724 | synthetic | 4.290 |
| 0.2586 | synthetic | 4.483 |
| 0.1724 | synthetic | 4.677 |
| 0.1724 | synthetic | 4.870 |
| 0.3448 | synthetic | 5.063 |
| 0.3448 | synthetic | 5.257 |
| 0.3879 | synthetic | 5.450 |
| 0.2586 | synthetic | 5.643 |
| 0.2155 | synthetic | 5.837 |
| 0.0431 | synthetic | 6.030 |
| 0.0431 | synthetic | 6.223 |
| 0.0431 | synthetic | 6.417 |
| 0 | synthetic | 6.610 |
| 0 | synthetic | 6.803 |
View data
| Density | Source | petal_width |
|---|---|---|
| 0.4167 | real | 0.1400 |
| 2.396 | real | 0.2200 |
| 0.7292 | real | 0.3000 |
| 0.5208 | real | 0.3800 |
| 0 | real | 0.4600 |
| 0.1042 | real | 0.5400 |
| 0.1042 | real | 0.6200 |
| 0 | real | 0.7000 |
| 0 | real | 0.7800 |
| 0 | real | 0.8600 |
| 0 | real | 0.9400 |
| 0.5208 | real | 1.020 |
| 0.3125 | real | 1.100 |
| 0.5208 | real | 1.180 |
| 0 | real | 1.260 |
| 0.9375 | real | 1.340 |
| 0.6250 | real | 1.420 |
| 1.042 | real | 1.500 |
| 0.1042 | real | 1.580 |
| 0.2083 | real | 1.660 |
| 0 | real | 1.740 |
| 1.146 | real | 1.820 |
| 0.5208 | real | 1.900 |
| 0.5208 | real | 1.980 |
| 0 | real | 2.060 |
| 0.4167 | real | 2.140 |
| 0.2083 | real | 2.220 |
| 0.7292 | real | 2.300 |
| 0.2083 | real | 2.380 |
| 0.2083 | real | 2.460 |
| 1.042 | synthetic | 0.1400 |
| 1.250 | synthetic | 0.2200 |
| 1.979 | synthetic | 0.3000 |
| 0.3125 | synthetic | 0.3800 |
| 0.1042 | synthetic | 0.4600 |
| 0 | synthetic | 0.5400 |
| 0 | synthetic | 0.6200 |
| 0 | synthetic | 0.7000 |
| 0 | synthetic | 0.7800 |
| 0 | synthetic | 0.8600 |
| 0.3125 | synthetic | 0.9400 |
| 0.3125 | synthetic | 1.020 |
| 0.5208 | synthetic | 1.100 |
| 0.6250 | synthetic | 1.180 |
| 0.4167 | synthetic | 1.260 |
| 0.4167 | synthetic | 1.340 |
| 0.5208 | synthetic | 1.420 |
| 0.2083 | synthetic | 1.500 |
| 0.7292 | synthetic | 1.580 |
| 0 | synthetic | 1.660 |
| 0.1042 | synthetic | 1.740 |
| 0.5208 | synthetic | 1.820 |
| 0.4167 | synthetic | 1.900 |
| 0.5208 | synthetic | 1.980 |
| 0.5208 | synthetic | 2.060 |
| 0.1042 | synthetic | 2.140 |
| 0.1042 | synthetic | 2.220 |
| 0.7292 | synthetic | 2.300 |
| 0.3125 | synthetic | 2.380 |
| 0.4167 | synthetic | 2.460 |
View data
| Column | Column | Difference |
|---|---|---|
| sepal_length | sepal_length | 0 |
| sepal_width | sepal_length | 0.0329 |
| petal_length | sepal_length | 0.0274 |
| petal_width | sepal_length | 0.0206 |
| sepal_length | sepal_width | 0.0329 |
| sepal_width | sepal_width | 0 |
| petal_length | sepal_width | 0.0487 |
| petal_width | sepal_width | 0.0209 |
| sepal_length | petal_length | 0.0274 |
| sepal_width | petal_length | 0.0487 |
| petal_length | petal_length | 0 |
| petal_width | petal_length | −0.0002 |
| sepal_length | petal_width | 0.0206 |
| sepal_width | petal_width | 0.0209 |
| petal_length | petal_width | −0.0002 |
| petal_width | petal_width | 0 |
Real vs synthetic, column by column
| Column | Kind | Real | Synthetic | Distance | Measure |
|---|---|---|---|---|---|
| sepal_length | numeric | 5.828 ± 0.8 | 5.714 ± 0.724 | 0.1250 | Kolmogorov–Smirnov D |
| sepal_width | numeric | 3.042 ± 0.431 | 3.076 ± 0.433 | 0.1250 | Kolmogorov–Smirnov D |
| petal_length | numeric | 3.725 ± 1.76 | 3.567 ± 1.75 | 0.0917 | Kolmogorov–Smirnov D |
| petal_width | numeric | 1.188 ± 0.763 | 1.146 ± 0.794 | 0.1333 | Kolmogorov–Smirnov D |
- 30 of 150 rows (20%) were held out of training so privacy and utility can be checked against rows the generator never saw.
The exact R script · R R version 4.5.1 (2025-06-13)
# Synthetic data — Gaussian mixture
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method synth.mixture 1.0.1 · spec e39203d9d91acae3
# Dataset: ml_iris (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr)
# %% Data
COLUMNS <- c("sepal_length", "sepal_width", "petal_length", "petal_width")
KINDS <- list(
sepal_length = "numeric",
sepal_width = "numeric",
petal_length = "numeric",
petal_width = "numeric"
)
LEVELS <- list(
)
SEED <- 42
loaded <- cs_synth_load("tests/parity/fixtures/ml_iris.parquet", COLUMNS, KINDS, LEVELS)
cols <- loaded$cols; n_input <- loaded$n
for (c in cols) if (c$kind == "numeric") cs_check_finite(c$values, c$name) # infinite values stop the run (registry/errors.yaml)
rng <- cs_rng(SEED) # shared stream: the same holdout and the same synthetic rows in Python and R
sp <- cs_synth_split(n_input, 0.2, rng)
train_rows <- sp$train; holdout_rows <- sp$holdout
train <- lapply(cols, cs_synth_take, rows = train_rows)
N_OUT <- length(train_rows)
warnings <- list()
fit_values <- list()
extra_tables <- list()
model_note <- ""
if (length(holdout_rows)) {
warnings <- c(warnings, sprintf("%s of %s rows (%.0f%%) were held out of training so privacy and utility can be checked against rows the generator never saw.",
format(length(holdout_rows), big.mark = ","), format(n_input, big.mark = ","), 100 * length(holdout_rows) / n_input))
}
for (c in train) if (mean(cs_synth_missing(c)) == 1) cs_error("CS_E_ALL_MISSING", column = c$name) # no values in the training rows
# %% Generate
# Gaussian mixture: the cs_ml_gmm_em twin of scikit-learn's EM from host-drawn starts, then
# shared-stream sampling from the fitted components.
fit <- cs_synth_mixture(train, N_OUT, rng, k = 3L, covariance = "full", n_init = 3L,
tol = 1e-8, max_iter = 500L, select_k = FALSE,
clip = TRUE)
synth <- fit$cols
fit_values$k <- fit$facts$k
fit_values$bic <- fit$facts$bic
for (j in seq_along(fit$facts$weights)) fit_values[[sprintf("weight[%d]", j)]] <- fit$facts$weights[j]
model_note <- sprintf(" with a %d-component Gaussian mixture (%s covariances)", fit$facts$k, "full")
synth <- cs_synth_add_missing(synth, train, rng, "keep_rate") # each column's missing share, at random
# %% Write and compare
out <- cs_ml_out_dir("target/academy-parity/run-953/synth.mixture/iris_full/out-r")
path <- cs_synth_write(file.path(out, "synthetic.parquet"), synth)
split <- cs_synth_write_split(file.path(out, "split.parquet"), n_input, train_rows, holdout_rows)
sm <- cs_synth_summarize(train, synth, length(train_rows), n_input, length(holdout_rows), fit_values)
values <- sm$values
summary <- sprintf("%s synthetic rows over %d column%s from %s training rows%s.",
format(values$n_rows, big.mark = ","), length(COLUMNS), if (length(COLUMNS) != 1) "s" else "",
format(length(train_rows), big.mark = ","), model_note)
if (!is.nan(values$fidelity_ks_mean)) {
summary <- paste0(summary, sprintf(" Numeric columns differ from the data by a mean Kolmogorov–Smirnov distance of %.3f", values$fidelity_ks_mean),
if (!is.nan(values$fidelity_tv_mean)) sprintf(" and categories by a mean total variation of %.3f.", values$fidelity_tv_mean) else ".")
} else if (!is.nan(values$fidelity_tv_mean)) {
summary <- paste0(summary, sprintf(" Categories differ from the data by a mean total variation of %.3f.", values$fidelity_tv_mean))
}
if (values$exact_matches > 0) {
summary <- paste0(summary, sprintf(" %s synthetic row%s identical to a training row.", format(values$exact_matches, big.mark = ","),
if (values$exact_matches != 1) "s are" else " is"))
}
cs_synth_publish(
"Synthetic data — Gaussian mixture",
cs_engine_info("stats", "nanoparquet", "nanoparquet"),
summary, values, sm$table, sm$charts, warnings,
list(list(key = "synthetic", path = path, format = "parquet", title = "Synthetic rows"),
list(key = "split", path = split, format = "parquet", title = "Training and holdout rows")),
list(kind = "generator", columns = I(COLUMNS), params = list(
n_rows = NULL,
k = 3,
select_k = FALSE,
covariance = "full",
n_init = 3,
max_iter = 500,
tol = 1e-8,
clip = TRUE,
missing = "keep_rate",
holdout = 0.2
)),
extra_tables
)
120 synthetic rows over 4 columns from 120 training rows with a 3-component Gaussian mixture (full covariances). Numeric columns differ from the data by a mean Kolmogorov–Smirnov distance of 0.119.
- Synthetic rows
- 120
- Mean KS distance
- 0.1187
- Mean TV distance
- —
- Rows copied from the data
- 0
To keep this page light, the plots are shown once, under the R tab.
Real vs synthetic, column by column
| Column | Kind | Real | Synthetic | Distance | Measure |
|---|---|---|---|---|---|
| sepal_length | numeric | 5.828 ± 0.8 | 5.714 ± 0.724 | 0.1250 | Kolmogorov–Smirnov D |
| sepal_width | numeric | 3.042 ± 0.431 | 3.076 ± 0.433 | 0.1250 | Kolmogorov–Smirnov D |
| petal_length | numeric | 3.725 ± 1.76 | 3.567 ± 1.75 | 0.0917 | Kolmogorov–Smirnov D |
| petal_width | numeric | 1.188 ± 0.763 | 1.146 ± 0.794 | 0.1333 | Kolmogorov–Smirnov D |
- 30 of 150 rows (20%) were held out of training so privacy and utility can be checked against rows the generator never saw.
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Synthetic data — Gaussian mixture
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method synth.mixture 1.0.1 · spec e39203d9d91acae3
# Dataset: ml_iris (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import os
import numpy as np
import codesplash as cs
from codesplash.errors import check_finite
from codesplash.ml.run import out_dir
from codesplash.stats.rng import Rng
from codesplash.synth import common as sy
from codesplash.synth import mixture
# %% Data
COLUMNS = ["sepal_length", "sepal_width", "petal_length", "petal_width"]
KINDS = {
"sepal_length": "numeric",
"sepal_width": "numeric",
"petal_length": "numeric",
"petal_width": "numeric",
}
LEVELS = {
}
SEED = 42
cols, n_input = sy.load("tests/parity/fixtures/ml_iris.parquet", COLUMNS, KINDS, LEVELS)
for c in cols:
if c.kind == "numeric":
check_finite(c.values, c.name) # infinite values stop the run (registry/errors.yaml)
rng = Rng(SEED) # shared stream: the same holdout and the same synthetic rows in Python and R
train_rows, holdout_rows = sy.split_holdout(n_input, 0.2, rng)
train = [c.take(train_rows) for c in cols]
N_OUT = None or len(train_rows)
warnings = []
fit_values = {}
extra_tables = []
model_note = ""
if len(holdout_rows):
warnings.append(f"{len(holdout_rows):,} of {n_input:,} rows ({len(holdout_rows) / n_input:.0%}) were held out of training so privacy and utility can be checked against rows the generator never saw.")
for c in train:
share = float(np.mean(c.missing()))
if share == 1.0:
cs.cs_error("CS_E_ALL_MISSING", column=c.name) # no values in the training rows
# %% Generate
# Gaussian mixture: scikit-learn's GaussianMixture from host-drawn starts (R: the cs_ml_gmm_em twin),
# then shared-stream sampling from the fitted components.
synth, facts = mixture.synthesize(
train, N_OUT, rng, k=3, covariance="full", n_init=3, tol=1e-8,
max_iter=500, select_k=False, clip=True, seed=SEED,
)
fit_values["k"] = facts["k"]
fit_values["bic"] = facts["bic"]
for j, w in enumerate(facts["weights"]):
fit_values[f"weight[{j + 1}]"] = w
model_note = f" with a {facts['k']}-component Gaussian mixture (full covariances)"
sy.add_missing(synth, train, rng, "keep_rate") # each column's missing share, at random
# %% Write and compare
out = out_dir("target/academy-parity/run-953/synth.mixture/iris_full/out-python")
path = sy.write(os.path.join(out, "synthetic.parquet"), synth)
split = sy.write_split(os.path.join(out, "split.parquet"), n_input, train_rows, holdout_rows)
values, table, charts = sy.summarize(train, synth, len(train_rows), n_input, len(holdout_rows), fit_values)
summary = (
f"{values['n_rows']:,} synthetic rows over {len(COLUMNS)} column{'s' if len(COLUMNS) != 1 else ''} from "
f"{len(train_rows):,} training rows{model_note}."
)
if not np.isnan(values["fidelity_ks_mean"]):
summary += f" Numeric columns differ from the data by a mean Kolmogorov–Smirnov distance of {values['fidelity_ks_mean']:.3f}"
summary += f" and categories by a mean total variation of {values['fidelity_tv_mean']:.3f}." if not np.isnan(values["fidelity_tv_mean"]) else "."
elif not np.isnan(values["fidelity_tv_mean"]):
summary += f" Categories differ from the data by a mean total variation of {values['fidelity_tv_mean']:.3f}."
if values["exact_matches"]:
summary += f" {values['exact_matches']:,} synthetic row{'s are' if values['exact_matches'] != 1 else ' is'} identical to a training row."
sy.publish(
"Synthetic data — Gaussian mixture",
cs.engine_info("scikit-learn", "pyarrow", "numpy", "pyarrow"),
summary, values, table, charts, warnings,
[{"key": "synthetic", "path": path, "format": "parquet", "title": "Synthetic rows"},
{"key": "split", "path": split, "format": "parquet", "title": "Training and holdout rows"}],
{"kind": "generator", "columns": COLUMNS, "params": {
"n_rows": None,
"k": 3,
"select_k": False,
"covariance": "full",
"n_init": 3,
"max_iter": 500,
"tol": 1e-8,
"clip": True,
"missing": "keep_rate",
"holdout": 0.2,
}},
extra_tables,
)
Which would you try first?
PredictPredict first
You have a table of 400 customers, with numbers and categories, and you want a shareable copy. Which would you try first?
Reveal
A copula or sequential trees. A neural generator has many parameters to learn from 400 rows, and it has more room to memorize them. Start with the simple generators and let the scorecard show you what they miss. If a stronger generator is ever justified, you'll have numbers to compare it with.
The same scores apply
ReflectWherever a generator comes from, the same questions apply: does it look like the data, is it too close to real rows, and can it do the job?
Suppose a vendor offers you a tool built on a deep generator, with a claim that it's "private by design". Write down three things you'd ask to see before you'd share its output.
Check your understanding
CheckpointCheck yourself
You decode a point far from any training data. What does the decoder do?
Show the answer
Answer: a
A decoder is a function, and it gives an answer for any input. Staying near the data is the job of the sampling procedure.
Check yourself
Which one trains by having two networks compete?
Show the answer
Answer: a
The generator tries to fool the discriminator, which tries to spot fakes. That contest is also why training can be unstable.
Check yourself
In a Gaussian mixture, what plays the part of the latent variable?
Show the answer
Answer: a
To generate a row, you draw the component label first, then draw from that component. You never observe the label, which is what makes it latent.
Do it on your own data
In the appSampling is the easy part once a model is fitted. Here you fit a mixture and draw new rows from it.
import pandas as pd
from sklearn.mixture import GaussianMixture
df = pd.read_csv("iris.csv")[["sepal_length", "sepal_width", "petal_length", "petal_width"]]
gm = GaussianMixture(n_components=3, n_init=3, random_state=1).fit(df)
rows, component = gm.sample(120)
synth = pd.DataFrame(rows, columns=df.columns)
print(synth.describe().round(2))
print(pd.Series(component).value_counts().sort_index())library(mclust)
df <- read.csv("iris.csv")[, c("sepal_length", "sepal_width", "petal_length", "petal_width")]
fit <- Mclust(df, G = 3, modelNames = "VVV")
set.seed(1)
drawn <- sim(fit$modelName, fit$parameters, n = 120)
synth <- as.data.frame(drawn[, -1])
names(synth) <- names(df)
summary(synth)
table(drawn[, 1])Recap
Recap- A generative model maps simple noise to data, through a latent space.
- Every latent point decodes to something, so distance from the data matters.
- VAEs, GANs and diffusion models differ in how they train and how they fail.
- CodeSplash's generators are the copula, trees and mixture, plus Lab GAN and diffusion generators and the VAE's sampler. Judge any generator with the same scores.
Concepts in this lesson