How to Generate Synthetic Data?
Last Updated on January 6, 2023 by Editorial Team
Last Updated on November 15, 2020 by Editorial Team
Author(s): Fabiana Clemente
Data Science
A synthetic data generation dedicated repository
This is a sentence that is getting to common, but itβs still true and reflects the market's trend, Data is the new oil. Some of the biggest players in the market already have the strongest hold on that currency. When it comes to Machine Learning, definitely data is a pre-requisite, and although the entry barrier to the world of algorithms is nowadays lower than before, there are still a lot of barriers in what concerns the data used in real-world problemsβββsometimes access is restricted, others thereβs not enough data to get good results, the variability is not enough for modelβs generalization, and the list goesΒ on.
For those reasons, synthetic data generation is one of the new must-have-skills for data scientists!
What is synthetic data afterΒ all?
Synthetic data can be defined as any data that was not collected from real-world events, meaning, is generated by a system with the aim to mimic real data in terms of essential characteristics. There are specific algorithms that are designed and able to generate realistic synthetic data that can be used as a training dataset. Synthetic data provides several benefits: privacy, as all the personal information has been removed, and the data is not possible to be traced back to being less costly and faster to collect when compared to collecting real-world data.
The algorithms you must know andΒ need!
There are several different methods to generate synthetic data, some of them very familiar to data science teams, such as SMOTE or ADYSIN. Nevertheless, when it comes to generating realistic synthetic data, we shall have a look into other familiar algorithmsβββDeep Generative Networks. The repository Iβll be covering in this blog post is a compilation of different generative algorithms to generate synthetic data.
Generating synthetic data withΒ WGAN
The Wasserstein GAN is considered to be an extension of the Generative Adversarial network introduced by Ian Goodfellow. WGAN was introduced by Martin Arjovsky in 2017 and promises to improve both the stability when training the model as well as introduces a loss function that is able to correlate with the quality of the generated events. Sounds good right? But what are the core differences introduced withΒ WGAN?
Whatβs new inΒ WGAN?
Instead of using the concept of βDiscriminatorβ to classify or predict the probability of a certain generated event as being real or fake, this GAN introduces the concept of a βCriticβ that in a nutshell, scores the realness or fakeness of a given event. This change is mainly due to the fact that while training a generator, theoretically we should seek the minimization of the distance between the distribution of the data observed in the training dataset and the distribution observed in the generated examples.
We can summarize the major differences introduced by WGAN as the following:
- Use a new loss function derived from the Wasserstein distance.
- After every gradient update on the critic function, clamp the weights to a small fixed range, [βc,c]. This allows for the enforcement of the Lipschitz constraint.
- An alternative proposed to the DiscriminatorβββtheΒ Critic.
- Use of a linear activation function as the output linear of the CriticΒ network.
- A different number of updates for the Generator and Critic Networks.
The benefits
The changes mentioned before and introduced with WGAN brings a series of benefits while training these networks:
- The training of a WGAN is more stable when compared to, for example, the original proposedΒ GAN.
- It is less sensitive to model architecture selection (Generator and CriticΒ choice)
- Also, less sensitive and impacted by the hyperparameters choiceβββalthough this is still very important to achieve goodΒ results.
- Finally, we are able to correlate the loss of the critic with the overall quality of the generated events.
Implementation with Tensorflow 2.0
Now what weβve completed the imports letβs go for the networks: the Generator and theΒ Critic.
Similarly to the Generator, Iβve decided to go for a simple Network for the Critic. Here Iβve a 4 Dense layers network with also Relu activation. But, I want to emphasize a bit here in the last code line. Different from Vanilla GAN where we add we usually add this as the last layer of theΒ network:
x = Dense(1, activation=βsigmoidβ)(x))
It uses the sigmoid function in the output layer of the discriminator, which means that it predicts the likelihood of a given event to be real. When it comes to WGAN, the critic model requires a linear activation, in order to predict the score of the βrealnessβ for a givenΒ event.
x = Dense(1)(x)
or
x = Dense(1, activation=βlinearβ)(x)
As Iβve mentioned, the main contribution of the WGAN model is the use of a new loss functionβββThe Wasserstein loss. In this case, we can implement the Wasserstein loss as a custom function in Keras, which calculates the average score for the real and generated events.
The score is maximizing the real events and minimizing the generated ones. Below the implementation of Wasserstein loss:
Another important change is the introduction of the weight clipping for the Critic Network. In this case, Iβve decided to defined to extend the Keras constraint class, with the belowΒ method:
Now that weβve covered the major changes, you can find the full implementation of WGAN in this open GitHub repository, supported by YData.he challenges withΒ WGAN
Although WGAN brings a lot of benefits to the area of data generation, it still has some challenges that need to beΒ solved:
- Still suffers from unstable training, although being more stable when compared to other architectures
- Slow convergence after weight clippingβββwhen the clipping window is tooΒ large
- Vanishing gradientsβββwhen clipping window is tooΒ small
Since its publication, and based on the fact that WGAN major issues are related to the chosen method for weights clipping, some have been the suggested improvements, being one of the most promising and used the gradient penaltiesβββWGAN-GPΒ article.
Tabular data generation
Now that weβve covered the most theoretical bits about WGAN as well as its implementation, letβs jump into its use to generate synthetic tabularΒ data.
For the purpose of this exercise, Iβll use the implementation of WGAN from the repository that Iβve mentioned previously in this blogΒ post.
The dataset that Iβll be using for this purpose is one pretty familiar among the Data Science communityβββthe Credit FraudΒ dataset.
For the purpose of demonstration, Iβve decided to go for a smaller sample of this dataset. In this case, Iβve decided to only synthesize fraudulent events.
After a few preprocessing steps on the data, we are ready to feed our data intoΒ WGAN.
Update the Critic more times than the Generator
In other GAN architectures, such as DCGAN, both the generator and the discriminator model must be updated in an equal amount of time. But this is not entirely true for the WGAN. In this case, the critic model must be updated more times than the generator model.
Thatβs why we have an input parameter, that Iβve called the n_criticβββthis parameter controls the number of times the critic gets an update from every batch of the generator. In this case, Iβve set it to 3 times. But you can set for others and check the impacts in the endΒ results.
When compared to the training process of other GAN architectures, while using the same dataset, it is possible to perceive that indeed WGAN training was less prone to instability and producing results that are closer to the real dataset distribution, although notΒ perfect.
Iβve also decided to reduce the dimensionality of the dataset, by leveraging both PCA and TSNE algorithms with the choice of 2 components, in order to ease the visualization of the data. Below you can find the plots, where I compare the results of both PCA and TSNE for the WGAN generated data and the original one. It is clear, that WGAN failed to fit some of the behavior captured in the original data, nevertheless, the results are quite promising.
Conclusions
The results shown in this blog are still very simple, in comparison with what can be done and achieved with generative algorithms to generate synthetic data with real-value that can be used as training data for Machine LearningΒ tasks.
For those who want to know more about generating synthetic data and want to have a try, have a look into this GitHub repository. Weβll be updating it with new generative algorithms as well as new dataset examples, and we invite you to collaborate!
Fabiana Clemente is CDO atΒ YData.
Improved and synthetic data forΒ AI.
YData provides the first dataset experimentation platform for Data Scientists.
How to Generate Synthetic Data? was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.
Published via Towards AI