|
| 1 | +# CTGAN Single-Table Example |
| 2 | + |
| 3 | +This example will go over training a single-table [CTGAN](https://arxiv.org/pdf/1907.00503) |
| 4 | +model using the [CTGAN](https://github.com/sdv-dev/CTGAN/) library and then synthesizing |
| 5 | +some data afterwards. |
| 6 | + |
| 7 | + |
| 8 | +## Downloading data |
| 9 | + |
| 10 | +First, we need the data. Download it from this |
| 11 | +[Google Drive link](https://drive.google.com/file/d/1J5qDuMHHg4dm9c3ISmb41tcTHSu1SVUC/view?usp=drive_link), |
| 12 | +extract the files and place them in a `/data` folder in within this folder |
| 13 | +(`examples/gan`). |
| 14 | + |
| 15 | +> [!NOTE] |
| 16 | +> If you wish to change the data folder, you can do so by editing the `base_data_dir` attribute |
| 17 | +> of the [`config.yaml`](config.yaml) file. |
| 18 | +
|
| 19 | +Here is a description of the files that have been extracted: |
| 20 | +- `trans.csv`: The training data. It consists of information about bank transactions and it |
| 21 | +contains 20,000 data points. |
| 22 | +- `trans_domain.json`: Metadata about the columns in `trans.csv`, such as data types and sizes. |
| 23 | +- `dataset_meta.json`: Metadata about the relationship between the tables. Since this is a |
| 24 | +single-table example, it will only contain information about the `trans` table. |
| 25 | +- `meta_info.json`: Metadata about the dataset, namely which columns are numerical and |
| 26 | +which ones are categorical, the target column and the task type (e.g. `regression`). |
| 27 | + |
| 28 | + |
| 29 | +## Kicking off training |
| 30 | + |
| 31 | +To kick off training, simply run the command below from the project's root folder: |
| 32 | + |
| 33 | +```bash |
| 34 | +python -m examples.gan.train |
| 35 | +``` |
| 36 | + |
| 37 | + |
| 38 | +## Training results |
| 39 | + |
| 40 | +The result files will be saved inside a `/results` folder within this folder |
| 41 | +(`examples/gan`). |
| 42 | + |
| 43 | +> [!NOTE] |
| 44 | +> If you wish to change the save folder, you can do so by editing the `results_dir` attribute |
| 45 | +> of the [`config.yaml`](config.yaml) file. |
| 46 | +
|
| 47 | +In the `/results` folder, there will be a file called `trained_ctgan_model.pkl`, |
| 48 | +which is a pickle file containing the trained model. You can load it using CTGAN's |
| 49 | +`load` function: |
| 50 | + |
| 51 | +```python |
| 52 | +import pickle |
| 53 | +from ctgan import CTGAN |
| 54 | + |
| 55 | +results_file = Path("examples/gan/results/trained_ctgan_model.pkl") |
| 56 | + |
| 57 | +ctgan = CTGAN.load(results_file) |
| 58 | +``` |
| 59 | + |
| 60 | +## Synthesizing data |
| 61 | + |
| 62 | +To synthesize some data with the trained model, run: |
| 63 | + |
| 64 | +```bash |
| 65 | +python -m examples.gan.synthesize |
| 66 | +``` |
| 67 | + |
| 68 | +If there is already a trained model in the `/results` folder, it will use that model. |
| 69 | +Otherwise it will train one from scratch. At the end of the script, it will save the |
| 70 | +synthesized data to `/results/trans_synthetic.csv`. |
| 71 | + |
| 72 | + |
| 73 | +## Evaluating the quality of the synthetic data |
| 74 | + |
| 75 | +### Alpha Precision |
| 76 | + |
| 77 | +To run a round of evaluation with [Alpha Precision](https://arxiv.org/abs/2301.07573) |
| 78 | +metrics on a set of synthetic data, run the `evaluate.py` script: |
| 79 | + |
| 80 | +```bash |
| 81 | +python -m midst_toolkit.evaluation.quality.scripts.midst_alpha_precision_eval \ |
| 82 | + --synthetic_data_path examples/gan/results/trans_synthetic.csv \ |
| 83 | + --real_data examples/gan/data/trans.csv \ |
| 84 | + --meta_info_path examples/gan/data/meta_info.json \ |
| 85 | + --save_directory examples/gan/results/ |
| 86 | +``` |
| 87 | + |
| 88 | +It will save the evaluation results under the `/results/model.txt` file. |
| 89 | + |
| 90 | +### Additional Metrics |
| 91 | + |
| 92 | +The calculation of additional metrics are set up in the `evaluate.py` file. They are the |
| 93 | +Kolmogorov-Smirnov (KS) test, Total Variation Distance (TVD), Correlation Matrix Difference |
| 94 | +and Mutual Information Difference. |
| 95 | + |
| 96 | +To compute those metrics, you can run the command below. The name of the table should be |
| 97 | +defined in the `dataset_meta.json` file, and the file for synthetic data should be under |
| 98 | +`/data/{table_name}.csv` for the real data and `/results/{table_name}_synthetic.csv` |
| 99 | +for the synthetic data. |
| 100 | + |
| 101 | +```bash |
| 102 | +python -m examples.gan.evaluate |
| 103 | +``` |
| 104 | + |
| 105 | +The results will be saved in the `/results/evaluation.json` file. |
0 commit comments