-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from ydataai/feat/feature#11
feat: feature#11 (WGAN-GP)
- Loading branch information
Showing
7 changed files
with
312 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#Install ydata-synthetic lib | ||
#! pip install pip install git+https://github.com/ydataai/ydata-synthetic.git | ||
|
||
import importlib | ||
import sys | ||
|
||
import pandas as pd | ||
import numpy as np | ||
import sklearn.cluster as cluster | ||
import matplotlib.pyplot as plt | ||
|
||
from ydata_synthetic.synthesizers import WGAN_GP | ||
from ydata_synthetic.preprocessing.credit_fraud import * | ||
|
||
model = WGAN_GP | ||
|
||
#Read the original data and have it preprocessed | ||
data = pd.read_csv('data/creditcard.csv', index_col=[0]) | ||
|
||
#Data processing and analysis | ||
data_cols = list(data.columns[ data.columns != 'Class' ]) | ||
label_cols = ['Class'] | ||
|
||
print('Dataset columns: {}'.format(data_cols)) | ||
sorted_cols = ['V14', 'V4', 'V10', 'V17', 'V12', 'V26', 'Amount', 'V21', 'V8', 'V11', 'V7', 'V28', 'V19', 'V3', 'V22', 'V6', 'V20', 'V27', 'V16', 'V13', 'V25', 'V24', 'V18', 'V2', 'V1', 'V5', 'V15', 'V9', 'V23', 'Class'] | ||
processed_data = data[ sorted_cols ].copy() | ||
|
||
#Before training the GAN do not forget to apply the required data transformations | ||
#To ease here we've applied a PowerTransformation | ||
data = transformations(data) | ||
|
||
#For the purpose of this example we will only synthesize the minority class | ||
train_data = data.loc[ data['Class']==1 ].copy() | ||
|
||
print("Dataset info: Number of records - {} Number of variables - {}".format(train_data.shape[0], train_data.shape[1])) | ||
|
||
algorithm = cluster.KMeans | ||
args, kwds = (), {'n_clusters':2, 'random_state':0} | ||
labels = algorithm(*args, **kwds).fit_predict(train_data[ data_cols ]) | ||
|
||
print( pd.DataFrame( [ [np.sum(labels==i)] for i in np.unique(labels) ], columns=['count'], index=np.unique(labels) ) ) | ||
|
||
fraud_w_classes = train_data.copy() | ||
fraud_w_classes['Class'] = labels | ||
|
||
# GAN training | ||
#Define the GAN and training parameters | ||
noise_dim = 32 | ||
dim = 128 | ||
batch_size = 128 | ||
|
||
log_step = 100 | ||
epochs = 200+1 | ||
learning_rate = 5e-4 | ||
beta_1 = 0.5 | ||
beta_2 = 0.9 | ||
models_dir = './cache' | ||
|
||
train_sample = fraud_w_classes.copy().reset_index(drop=True) | ||
train_sample = pd.get_dummies(train_sample, columns=['Class'], prefix='Class', drop_first=True) | ||
label_cols = [ i for i in train_sample.columns if 'Class' in i ] | ||
data_cols = [ i for i in train_sample.columns if i not in label_cols ] | ||
train_sample[ data_cols ] = train_sample[ data_cols ] / 10 # scale to random noise size, one less thing to learn | ||
train_no_label = train_sample[ data_cols ] | ||
|
||
gan_args = [batch_size, learning_rate, beta_1, beta_2, noise_dim, train_sample.shape[1], dim] | ||
train_args = ['', epochs, log_step] | ||
|
||
seed = 17 | ||
test_size = 492 # number of fraud cases | ||
noise_dim = 32 | ||
|
||
#Training the WGAN_GP model | ||
synthesizer = model(gan_args, n_critic=2) | ||
synthesizer.train(train_sample, train_args) | ||
|
||
#WGAN_GP models is now trained | ||
#So we can easily generate a few samples | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
from ydata_synthetic.synthesizers.regular.cgan.model import CGAN | ||
from ydata_synthetic.synthesizers.regular.wgan.model import WGAN | ||
from ydata_synthetic.synthesizers.regular.vanillagan.model import VanilllaGAN | ||
from ydata_synthetic.synthesizers.regular.wgangp.model import WGAN_GP | ||
|
||
__all__ = [ | ||
"VanilllaGAN", | ||
"CGAN", | ||
"WGAN" | ||
"WGAN", | ||
"WGAN_GP" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.