CTGANDataSampler<T>
Models & Types · AiDotNet.NeuralNetworks.SyntheticData
Handles conditional vector generation and training-by-sampling for CTGAN. Implements the sampling strategy from the CTGAN paper that ensures all categories are represented equally during training.
For Beginners
Real data often has imbalanced categories. For example, in a “car color” column, 80% might be “white” and only 2% “yellow”. Without special handling, the generator would mostly learn to produce white cars.
The data sampler fixes this by:
- Picking a random categorical column
- Picking a random category (so “yellow” gets equal chance as “white”)
- Finding a real row with that category to use as training data
- Creating a “conditional vector” that tells the generator what category to produce
This ensures the generator learns to produce all categories well.
How It Works
The CTGAN paper introduces “training-by-sampling” to handle imbalanced categorical columns:
- Randomly pick a discrete/categorical column
- Randomly pick a category value from that column (with equal probability)
- Construct a conditional vector indicating the selected category
- Sample a real row that has the selected category value
This ensures that rare categories are sampled proportionally during training, preventing the generator from ignoring minority classes.
Constructors
| Constructor | Summary |
|---|---|
CTGANDataSampler(Random random) |
Initializes a new CTGANDataSampler. |
Properties
| Property | Summary |
|---|---|
int ConditionalVectorWidth { get; } |
Gets the width of the conditional vector. |
int NumDiscreteColumns { get; } |
Gets the number of discrete/categorical columns used for conditioning. |
Methods
| Method | Summary |
|---|---|
ValueTuple<Vector<T>, Vector<T>, int, int, int> SampleCondVecWithMask() |
Full training-by-sampling draw (Xu 2019 §4.3) exposing everything the generator’s conditional cross-entropy loss needs: the one-hot conditional vector, a MASK that is 1 only over the selected column’s category block, the selected discrete-c… |
ValueTuple<Vector<T>, int> SampleConditionAndRow() |
Samples a conditional vector and a corresponding real row index for training. |
Vector<T> CreateConditionVector(int discreteColIndex, int categoryIndex) |
Generates a conditional vector for a specific category in generation mode. |
Vector<T> SampleRandomConditionVector() |
Generates a random conditional vector for unconditional generation (picks random category). |
void Fit(Matrix<T> data, IReadOnlyList<ColumnMetadata> columns) |
Builds the category-to-row-index tables from the training data. |