Package: datgan
DATGANDirected Acyclic Tabular GAN (DATGAN) for integrating expert knowledge in synthetic tabular data generation.License:GPLv3Development Status:AlphaHomepage:https://github.com/glederrey/DATGANOverviewTheDATGANis a synthesizer for tabular data. It uses LSTM cells to generate synthetic data for continuous and
categorical variable types. In addition, a Directed Acyclic Graph (DAG) can be provided to represent the structure
between the variables and help the model perform better. This model integrates two types of conditionality: rejection by
sampling and conditional inputs.RequirementsThe current version of theDATGANworks with Python>=3.7 and Tensorflow==2.8.0. Previous versions of Python have not
been tested and are thus blocked. Therefore, we recommend the user set up
avirtualenv. An installation guide is provided to help with the installation
of the library.InstallationWe provide acomplete installation guideusing conda and
setting up a virtualenv. Please follow this guide to properly set up everything and ensure that you can use the DATGAN
as intended.Testing the DATGANOnce you have followed the installation guide, you can clone this repository and use the notebooks provided in the
folderexampleto train theDATGANand use the evaluation
metrics provided in this repository.Data FormatInputTheDATGANuses tabular data loaded with thepandaslibrary. This table must follow these rules:has no missing valueshas columns of typesint,float,strorbool.each column contains data of only one type.NOTE: It is crucial to identify continuous and categorical variables/columns. A general rule of thumb is that any
discrete distributions with a large number of categories should be considered continuous and then rounded.OutputThe output of theDATGANis a table of synthetic data with the same columns as the input table and as many rows as
requested.TutorialThis short tutorial will guide you through a series of steps that will help you get started with the most basic usage
ofDATGANto generate samples from a given dataset.While all the methods are explained below, you can use the built-in helper function to get a reminder of the different
functions used in this library. Just run the following lines:importdatganhelp(datgan)1. Load the data and provide info about itThe first step is to load the data, which we will use to fit theDATGAN. We provide theCMAP(Chicago
Metropolitan Agency for Planning) dataset in the example folder. It is a travel survey dataset for the city of Chicago.
The dataset has already been cleaned compared to the original dataset. It contains 15 columns and 8'929 rows.You can load this dataset usingpandas.You also need to provide the data type for each column. For the moment, only
two possibilities are implemented:continuousandcategorical.Forcontinuouscolumns, you can pass more
information to the model, such as:discrete[mandatory]: Boolean value to indicate if the synthetic value has to be rounded when samplingbounds[optional]: Values of the bounds. While sampling synthetic data, all values outside the bounds will be
discarded.enforce_bounds[optional]: Instead of removing synthetic data generated outside the bounds, we will clip the
values to the bounds.   (This can be useful in the case of a mixed distribution with a peak of values close to the
lower or the upper bound.)apply_func[optional]: You can provide a lambda function that will be applied before the encoding step and when
sampling the final values. This can help train models on distributions that are more difficult to represent by GMMs, *
e.g.* exponential distributions.importpandasaspdimportnumpyasnpdf=pd.read_csv('./data/CMAP.csv',index_col=False)data_info={'distance':{'type':'continuous','bounds':[0.0,np.infty],'discrete':False,'apply_func':(lambdax:np.log(x+1)),},'age':{'type':'continuous','bounds':[0,100],'enforce_bounds':True,'discrete':True},'departure_time':{'type':'continuous','bounds':[0,23.999],'discrete':False}}# For the categorical columns, we can simply add them using a for loopforcindf.columns:ifcnotindata_info.keys():data_info[c]={'type':'categorical'}WARNING:While using a lambda function in theapply_funcparameters, we use thepynverselibrary to compute the inverse of the given function. Therefore, with more complex functions, it is possible to get Warnings while sampling the final synthetic data. This is a normal behavior whenpynverseis having trouble computing the inverse of some values.2. Create a DAGThe second step consists in creating the DAG for the DATGAN. The DAG is created using the librarynetworkxfrom
Python. The only constraint while creating the DAG is that it should not contain any cycle (by definition). In the
article, we give more details on creating the DAG. However, this process can still be cumbersome and requires trials and
errors to obtain a good DAG. (We highly recommend using a "visualization" tool to create the DAG.)Below, we provide an example of a DAG for the CMAP dataset. First, the picture shows a visual representation of the DAG.
The colors of the nodes show the categories of variables: purple corresponds to individuals, blue to households, and red
to trips. The image shows that nodes can be the source and the target of multiple edges. While the direction of the
edges do not play an important role, we recommend having as few sink nodes as possible. TheDATGANarticleprovides more details on building the DAG.The above DAG is represented in thenetworkxlibrary as the following way:importnetworkxasnxgraph=nx.DiGraph()graph.add_edges_from([("age","license"),("age","education_level"),("gender","work_status"),("education_level","work_status"),("education_level","hh_income"),("work_status","hh_income"),("hh_income","hh_descr"),("hh_income","hh_size"),("hh_size","hh_vehicles"),("hh_size","hh_bikes"),("work_status","trip_purpose"),("trip_purpose","departure_time"),("trip_purpose","distance"),("travel_dow","choice"),("distance","choice"),("departure_time","choice"),("hh_vehicles","choice"),("hh_bikes","choice"),("license","choice"),("education_level","hh_size"),("work_status","hh_descr"),("work_status","hh_size"),("hh_income","hh_bikes"),("hh_income","hh_vehicles"),("trip_purpose","choice")])If your DAG contains node that are not connected to any other nodes, you still need to add them. To do this, you can
use theadd_nodemethod of theDiGraphobject.graph.add_node("single_node")If you do not know how to create your DAG, it is possible not to provide any DAG to the model. However, in this case,
the model will define alinearDAG,i.e.each variable in the dataset is linked to the next one following the order
of the columns. It can be helpful to test the model quickly. However, it will reduce the model's performance, as shown
in the article.To help build the DAG and know which edge should be added, we implemented a function namedadvisethat computes
Pearson and Spearman correlations and then suggests some links that could be added to the DAG. The suggested links are
the ones with the highest correlations.importdatgandatgan.advise(df,graph,plot_graphs=True)Results (without the graphs) should look like this:You might want to add the following edges in your DAG (direction not given here):
  - license <-> education_level
  - license <-> work_status
  - hh_vehicles <-> hh_descr
  - hh_bikes <-> age
  - hh_size <-> age3. Create a DATGAN instanceThe next step is to importDATGANand create an instance of the model. There are no required parameters for the
model. However, we advise you to set up the basic parameters such as the output folder (output), batch
size (batch_size), and the number of epochs (num_epochs).output_folder='./output/'batch_size=1116fromdatganimportDATGANdatgan=DATGAN(output=output_folder,batch_size=batch_size,num_epochs=1000)NOTE:Setting up a suitable batch size is really important. A batch size too big will make the model crash due to memory error, while one that is too small will make the model slower to train. Trials and errors are required depending on your hardware. In addition, it is good to find a batch size such thatlen(df) % batch_sizeis as small as possible since the last batch of data is dropped if it is smaller than the batch size.4. Preprocess the data (optional)The fourth step consists in preprocessing the data. This step is optional since it is automatically done in the next
step if skipped right now. We propose to do the preprocessing in advance because it usually takes a bit of time. And if
you want to try multiple parameters with theDATGAN, you do not have to preprocess the data every time. Therefore,
it is possible to do it before fitting the model and saving it somewhere.datgan.preprocess(df,data_info,preprocessed_data_path='./encoded_data')NOTE 1:If you decide to change any values in the metadata dictionarydata_info, you must preprocess the data again. However, since the preprocessing of the continuous variable is random, you will not be able to sample a model trained on a previous preprocessed dataset with the current one. Therefore, we advise not to delete the previous preprocessed dataset.NOTE 2:In the folderpreprocessed_data_path/continuous, you will find PNG files of the continuous distributions. These pictures can be used to make sure that the Variational Gaussian Mixtures (VGM) were correctly trained for each continuous distribution. It can be especially useful if the user is trying to use a lambda function.5. Fit the modelOnce you have aDATGANinstance, you can call the methodfitand pass the following parameters:data: the original DataFramegraph: thenetworkxDAGcontinuous_columns: the list of continuous columnspreprocessed_data_path: the path to the preprocessed data if done in Step 4 or the path where to save them.datgan.fit(df,data_info,graph,preprocessed_data_path='./encoded_data')NOTE:Depending on the verbose level, the model will show different types of information. The standard level is 1, displaying a progress bar for the whole training process. With level 2, the model will display information at each epoch. With level 0, the model will not print any information.6. Sample new dataOnce the model has been fitted, you can generate new synthetic data by calling the functionsample. You have to
provide the desired number of samples.samples=datgan.sample(len(df))samples.to_csv('./data/synthetic.csv',index=False)7. Save and load a model (optional)Fitting the model can take a substantial amount of time. It is, thus, better to avoid fitting a new model every time we
need to generate samples. We advise to save checkpoints of the model while it is training. However, if you do not want
to do that, the model will always keep the latest checkpoint once it has finished training. You can, thus, load it at
any time afterward.You can simply call the functionloadwith the parameters used while fitting the model to load the model. To save
memory, we only save the parameters of the Generator and Discriminator. Therefore, more information is required to load
the model.new_datgan=datgan.load(df,graph,preprocessed_data_path='./encoded_data')NOTE:This can be replaced by using the functionfitonce again. Since the model has already been trained, it will just load it.ConditionalityConditionality can be used in two different ways. The first one is done after training theDATGANmodel. You can
provide a dictionary of desired values and passes it to the sampling method. TheDATGANmodel uses rejection
sampling to provide the desired output. The second method consists in training theDATGANusing conditional inputs.
To use the second conditionality, the model has to be trained again.Rejection samplingIn thesamplefunction of theDATGAN, you must provide a dictionary as follows:categorical variables:string or list of strings for the desired categories.continuous variables:lambda function returning a boolean value.For example, suppose you want only to generate synthetic data about men under 30 years old. In that case, you can
provide the following dictionary to thesamplefunction.cond_dict={'age':lambdax:x<30,'gender':'0'}samples=datgan.sample(len(df),cond_dict=cond_dict)samples.to_csv('./data/synthetic.csv',index=False)WARNING:Since theDATGANis using rejection sampling to provide the desired values, you cannot provide a conditional dictionary that is too restrictive. This will significantly slow the sampling process and even make it fail. In some specific cases, you can set the parametertimeouttoFalseif you do not want the sampling process to be stopped.Conditional inputsThe conditional inputs are columns present in the original dataset. To train theDATGANmodel, you need to provide
the list of columns you want to consider as conditional inputs. For example, suppose you want the age, gender, and trip
purpose to be conditional inputs. In that case, you can initialize theDATGANmodel this way:datgan=DATGAN(output=output_folder,batch_size=batch_size,num_epochs=1000,conditional_inputs=['age','gender','trip_purpose'])Suppose the model is initialized in such a way. In that case, it will automatically transform the DAG. Each of the
conditional variables is considered a source node in the DAG. It will then train the generator using the values for
these variables as inputs. Finally, to sample the synthetic data, you need to provide apandas.DataFrameor adictwith columns/keys corresponding to the conditional inputs defined in the initialization. You do not need to care about
the size of the DataFrame since the model automatically samples values from it. You can, thus, provide any values you
want (a subset of the original dataset, unique values, or values generated by another model on another dataset.)In addition, the rejection sampling conditionality can be used alongside the conditional inputs. You have to provide the
dictionary of conditionals alongside the new input data.Model parametersSuppose you want to change the default behavior ofDATGAN, such as using a different batch size or the total number of
epochs. In that case, you can do so by passing additional arguments when creating theDATGANinstance or using its
two main functions.Initialize the model (DATGAN.__init__)NameTypeDefaultExplanationloss_functionstrNoneName of the loss function to be used. If not specified, it will choose between'WGAN'and'WGGP'depending on the ratio of continuous and categorical columns. Only accepts the values'SGAN','WGAN', and'WGGP'.label_smoothingstr'TS'Type of label smoothing. Only accepts the values'TS','OS', and'NO'.outputstr'./output'Path to store the model and its artifacts.gpuintNoneModel will automatically try to use GPU if tensorflow can use CUDA. However, this parameter allows you to choose which GPU you want to use.num_epochsint100Number of epochs to use during training.batch_sizeint500Size of the batch to feed the model at each step.save_checkpointsboolTrueWhether to store checkpoints of the model after each training epoch.restore_sessionboolTrueWhether continue training from the last checkpoint.learning_ratefloatNoneLearning rate. The value will be set according to the chosen loss function if set to None.g_periodintNoneEveryg_periodsteps, train the generator once. (Used to train the discriminator more than the generator) By default, it will choose values according to the chosen loss function.l2_regboolNoneTell the model to use L2 regularization while training both NNs. By default, it only applies the L2 regularization when using the SGAN loss function.z_dimint200Dimension of the noise vector used as an input to the generator.num_gen_rnnint100Size of the hidden units in the LSTM cell.num_gen_hiddenint50Size of the hidden layer used on the generator's output to act as a convolution.num_dis_layersint1Number of layers for the discriminator.num_dis_hiddenint100Size of the hidden layers in the discriminator.noisefloat0.2Upper bound to the gaussian noise added to the label smoothing. (only used iflabel_smoothingis set to'TS'or'OS')conditional_inputslist[str]NoneList of variables in the dataset that are used as conditional inputs to the model.verboseint1Level of verbose. 0 means no print, 1 means that some details will be printed, 2 is mostly used for debugging purposes.Training the model (DATGAN.fit)NameTypeDefaultExplanationdatapandas.DataFramOriginal datasetmetadatadictNoneDictionary containing information about the data in the DataFrame.dagnetworkx.DiGraphNoneDirected Acyclic Graph representing the relations between the variables. If no dag is provided, the algorithm will create a linear DAG.preprocess_data_pathstrNonePath to an existing preprocessor. If None is given, the model will preprocess the data and save it underself.output + '/encoded_data'.Sampling synthetic data (DATGAN.sample)NameTypeDefaultExplanationnum_samplesintNumber of rows in the generated synthetic dataset.inputsdictorpandas.DataFrameNoneOptional input data.Mustbe provided if the model was training with some conditional inputs in the variableconditional_inputs.cond_dictdictNoneDictionary for the rejection sampling conditionality.samplingstrSSSampling strategy. Only accepts the following values:'SS','SA','AS', and'AA'.timeoutboolTrueTimeout to stop sampling if the model cannot generated the required data incond_dict.When sampling the synthetic data, you can choose between multiple sampling strategies.Smeans we are using simulation
to sample the data,Aindicates that the model uses argmax. The first letter corresponds to continuous variables and
the second to categorical variables. Therefore,SAmeans we're using simulation for continuous variables and argmax
for categorical variables.Tips and tricksWhile theDATGANmodel will automatically choose its parameters if none are provided, we highly recommend the user
to "play" with them. The most important ones are the following:loss_functionGenerally, theWGANloss function works better on datasets with more categorical columns than
continuous. It is the contrary for theWGGPloss. TheSGANloss seems to perform a bit less good than the other
two in the specific cases. However, it seems to perform ok in any case.g_periodThis parameter is critical when using either theWGANor theWGGPloss. Generally, the latter requires
a lot more discriminator training than the previous. However, it might be interesting to test different values to see
which leads to the best results.l2_regThe L2 regularization is "mandatory" for theSGANloss and "forbidden" for theWGANloss. The user can
test these other configurations, but it will lead to worse results. However, for theWGGPloss, there are no
specific rules on whether to apply it or not. Therefore, it might be interesting to test this parameter as well.learning_rateIt has been fixed depending on the loss function. However, as for any optimization problem, playing
with the learning rate to find the optimal value is always important.ContributingIf you want to participate in the development of this model, you can write a Github issue and give us your suggestions
or directly prepare a Pull Request. (However, it would be better first to discuss the content of the PR.)AcknowledgementsWe want to thank the authors of theTGANmodel, the authors of theCTGANmodel, and the authors of theCTAB-GANmodel. These three models have greatly inspired theDATGANmodel.CitationIf you use DATGAN or its evaluation metrics for any of your projects, please cite the following work:"Lederrey G., Hillel T., Bierlaire M.,DATGAN: Integrating Expert Knowledge into Deep Learning for Synthetic Tabular Data, arXiv preprint arXiv:2203.03489 (2022)"The original code for this article can be found in this Github repository:https://github.com/glederrey/SynthPop.