forked from gianlucatruda/TableDiffusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_diff_gan.py
More file actions
218 lines (193 loc) · 5.91 KB
/
Copy pathrun_diff_gan.py
File metadata and controls
218 lines (193 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from pathlib import Path
import os
import argparse
from datetime import datetime
import random
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from tqdm import tqdm
import mlflow
import sys
import time
import argparse
# Have to add path to the top level module, because the code doesn't seem to use relative imports...
REPO_DIR = os.path.dirname(os.path.realpath(__file__))
SRCDIR = os.path.join(REPO_DIR, 'tablediffusion')
sys.path.append(SRCDIR)
# TableDiffusion code.
from tablediffusion.models import *
import tablediffusion.utilities.utils as utils
import tablediffusion.utilities.data_utils as data_utils
import tablediffusion.config.configs as configs
import tablediffusion.config as config
from tablediffusion.utilities import run_synthesisers
GAN_EPOCHS = 1
DIFF_EPOCHS = 1
DIFFUSION_STEPS = 3
SYNTHESIZERS = {
"DPWGAN": (
WGAN_Synthesiser,
{
"batch_size": 1024,
'gen_lr': 0.005,
'dis_lr': 0.001,
"latent_dim": 128,
'n_critic': 2,
"epoch_target": GAN_EPOCHS,
"mlflow_logging": False,
'gen_dims': (512, 512),
'dis_dims': (512, 512)
},
{
"n_epochs": GAN_EPOCHS,
},
{
"use_raw_data": False,
},
),
"DPautoGAN": (
DPautoGAN_Synthesiser,
{
'batch_size': 1024,
'latent_dim': 64,
"gen_dims": (256, 256, 256),
"dis_dims": (256, 256, 256),
'gen_lr': 0.0001,
'dis_lr': 0.0007,
"ae_lr": 0.02,
"ae_compress_dim": 16,
"ae_eps_frac": 0.3,
'epoch_target': GAN_EPOCHS,
'mlflow_logging': True,
},
{
"n_epochs": GAN_EPOCHS,
},
{
"use_raw_data": False,
},
),
"PATEGAN": (
PATEGAN_Synthesiser,
{
'batch_size': 1024,
"gen_dims": (512, 512, 512),
"dis_dims": (512, 512, 512),
'gen_lr': 0.005,
'dis_lr': 0.001,
'latent_dim': 128,
'num_teachers': 30,
'teacher_iters': 8,
'student_iters': 5,
'epoch_target': GAN_EPOCHS,
'mlflow_logging': True,
},
{
'n_epochs': GAN_EPOCHS,
'noise_multiplier': 0.0048,
},
{
"use_raw_data": False,
},
),
"DPDiffusion": (
TableDiffusion_Synthesiser,
{
"batch_size": 1024,
"lr": 0.005,
"dims": (128, 128),
"mlflow_logging": False,
"epoch_target": DIFF_EPOCHS,
"diffusion_steps": DIFFUSION_STEPS,
"predict_noise": True,
},
{
"n_epochs": DIFF_EPOCHS,
"verbose": True,
},
{
"use_raw_data": True,
},
),
"DPAttentionGAN": (
DPattentionGAN_Synthesiser,
{
'batch_size': 1024,
'latent_dim': 64,
"gen_dims": (256, 256),
"dis_dims": (256, 256),
'gen_lr': 0.0001,
'dis_lr': 0.0007,
"ae_lr": 0.02,
"ae_compress_dim": 16,
"ae_eps_frac": 0.3,
'epoch_target': GAN_EPOCHS,
'mlflow_logging': True,
},
{
"n_epochs": GAN_EPOCHS,
},
{
"use_raw_data": False,
},
),
}
def do_things(input_dataset, output_dir="/home/azureuser/drive1/syn"):
DIR = Path("stuff")
HOMEDIR = os.getenv('HOME')
DATADIR = Path(os.path.join(HOMEDIR, "TableDiffusion2/data"))
RESULTDIR = DIR / "results"
sys.path.append(str(SRCDIR))
DEVICE = torch.device("cuda:0" if (torch.cuda.is_available()) else "cpu")
SEED = 900
for p in [SRCDIR, DIR, DATADIR, RESULTDIR]:
if not os.path.exists(p):
print(f"{p} does not exist")
# Set up the models to run
model_configs = {model_name: SYNTHESIZERS[model_name] for model_name in args.models}
dset_name = input_dataset
datasets = {dset_name: config.datasets[dset_name]}
exp_hash = datetime.now().strftime("%y%m%d_%H%M%S")
EXP_NAME = f"exp_{exp_hash}"
# Make directories for experiment EXP_NAME
EXP_PATH = RESULTDIR / EXP_NAME
# FAKE_DSET_PATH = EXP_PATH / "fake_datasets"
FAKE_DSET_PATH = Path(output_dir)
if not os.path.exists(FAKE_DSET_PATH):
os.makedirs(FAKE_DSET_PATH)
exp_id = mlflow.create_experiment(f"{EXP_NAME}")
print(f"\n\nRunning experiment: {EXP_NAME}\n\n")
start = time.time()
run_synthesisers(
datasets=datasets,
synthesisers=model_configs,
exp_name=EXP_NAME,
exp_id=exp_id,
datadir=DATADIR,
repodir="./",
epsilon_values=[2.0],
repeats=1,
metaseed=SEED,
generate_fakes=True,
fake_sample_path=EXP_PATH / "samples",
fake_data_path=FAKE_DSET_PATH,
cuda=True,
)
mlflow.end_run()
end = time.time()
print(f'Time Elapsed: {(end - start) / 60} min')
if __name__ =='__main__':
parser = argparse.ArgumentParser(description='Run DP baselines for WGAN and Diffusion.')
parser.add_argument('--input_dataset', required=True, help='Name of dataset. Must be defined in configs.py')
parser.add_argument('--output_dir', default="/home/azureuser/drive1/syn", help='Directory to put generated synthetic data.')
parser.add_argument('--models', nargs='+', default=["DPWGAN", "DPDiffusion"], help='List of models to run. Options are DPWGAN, DPautoGAN, PATEGAN, DPDiffusion')
args = parser.parse_args()
do_things(args.input_dataset, args.output_dir)