forked from VincentGranville/Main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWGAN.py
494 lines (362 loc) · 15.2 KB
/
WGAN.py
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# https://machinelearningmastery.com/how-to-code-a-wasserstein-generative-adversarial-network-wgan-from-scratch/
# -*- coding: utf-8 -*-
"""Shakti__telecom_wcgan-gp.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1ZoMl4TM8Ell_2ClnJAifPEr-aCdBHVUg
"""
# !pip install table-evaluator
# from table_evaluator import TableEvaluator
import tensorflow as tf
import keras
from functools import partial
import itertools
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.layers import Input, Dense, Flatten, Dropout, Embedding, multiply, LeakyReLU
from keras.models import Sequential, Model
# from keras.optimizers.legacy import Adam
from keras.optimizers import Adam
import keras.backend as K
from tensorflow.python.framework.ops import disable_eager_execution
disable_eager_execution()
import warnings
warnings.simplefilter("ignore")
# import random as python_random
# from numpy.random import randn
# from tensorflow import random
import random
random.seed(101)
seed = 101 #108 # to make results replicable (much better than 102, 103)
np.random.seed(seed) # for numpy
tf.random.set_seed(seed) # for tensorflow
keras.utils.set_random_seed(seed) # keras
# python_random.seed(seed) # for python
optimizers = Adam(learning_rate=0.0004, beta_1=0.05, beta_2=0.9)
#Check how many GPUs there is avaliable for training:
# import tensorflow as tf
# gpus = tf.config.list_physical_devices('GPU')
# logical_gpus = tf.config.experimental.list_logical_devices('GPU')
# print("Number of GPUs & Logical GPUs available: ", len(gpus), len(logical_gpus))
#--- [1] Read and transform data
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler, Normalizer, LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA
df = pd.read_csv("https://raw.githubusercontent.com/shakti2594/Shared_dataset/main/Telco-Customer-Churn.csv")
# keep minority group only (Churn = 'Yes')
df.drop(df[(df['Churn'] == 'No')].index, inplace=True)
#- [1.1] transforming TotalCharges to TotalChargeResidues, add to dataframe
df['TotalCharges'] = pd.to_numeric(df['TotalCharges'], errors='coerce')
print(df.isna().sum().sum())
df = df.dropna()
print(df.shape)
arr1 = df['tenure'].to_numpy()
arr2 = df['TotalCharges'].to_numpy()
arr2 = arr2.astype(float)
residues = arr2 - arr1 * np.sum(arr2) / np.sum(arr1) # also try arr2/arr1
df['TotalChargeResidues'] = residues
df = df[['tenure', 'MonthlyCharges', 'TotalChargeResidues', 'Churn']]
#- [1.2] adding labels to data, to boost GAN
le = LabelEncoder()
df['Churn'] = le.fit_transform(df['Churn'])
encoded_labels = {num:label for (num, label) in zip(range(15), le.classes_)}
encoded_labels
x_train, x_test, y_train, y_test = train_test_split(df.iloc[:,:-1],
df['Churn'],
test_size=1/7.0,
random_state=0)
x_train = np.array(x_train)
x_test = np.array(x_test)
#y_train = y_train.values.reshape(-1,1)
#y_test = y_test.values.reshape(-1,1)
y_train = np.array(y_train)
y_test = np.array(y_test)
#- [1.3] potential data transforms
ss = StandardScaler().fit(x_train)
x_train = ss.transform(x_train)
x_test = ss.transform(x_test)
#print(x_train.shape, x_test.shape)
pca = PCA(.99).fit(x_train)
x_train = pca.transform(x_train)
x_test = pca.transform(x_test)
#print(x_train.shape, x_test.shape)
norm = Normalizer().fit(x_train)
x_train = norm.transform(x_train)
x_test = norm.transform(x_test)
#print(x_train.shape, x_test.shape)
print(x_train.shape, x_test.shape)
print(x_train)
#--- [2] Model Definition"""
class RandomWeightedAverage(tf.keras.layers.Layer):
# Provides a (random) weighted average between real and generated samples
def __init__(self, batch_size):
super().__init__()
self.batch_size = batch_size
def call(self, inputs, **kwargs):
alpha = tf.random.uniform((self.batch_size, 1))
return (alpha * inputs[0]) + ((1 - alpha) * inputs[1])
def compute_output_shape(self, input_shape):
return input_shape[0]
class WCGANGP():
def __init__(self,
x_train,
y_train,
latent_dim: int,
batch_size: int,
n_critic: int):
"""Implement WCGAN with Gradient Penalty."""
self.x_train = x_train
self.y_train = y_train
self.num_classes = len(np.unique(y_train))
self.data_dim = x_train.shape[1]
self.latent_dim = latent_dim
self.batch_size = batch_size
self.n_critic = n_critic
# Log training progress.
self.losslog = []
# Adam optimizer, suggested by original paper.
optimizer = optimizers
# Build the generator and critic
self.generator = self.build_generator()
self.critic = self.build_critic()
# Freeze generator's layers while training critic.
self.generator.trainable = False
# Data input (real sample).
real_data = Input(shape=self.data_dim)
# Noise input (z).
noise = Input(shape=(self.latent_dim,))
# Label input.
label = Input(shape=(1,))
# Generate data based of noise (fake sample)
fake_data = self.generator([noise, label])
# Critic (discriminator) determines validity of the real and fake images.
fake = self.critic([fake_data, label])
valid = self.critic([real_data, label])
# Construct weighted average between real and fake images.
interpolated_data = RandomWeightedAverage(self.batch_size)([real_data, fake_data])
# Determine validity of weighted sample.
validity_interpolated = self.critic([interpolated_data, label])
# Use Python partial to provide loss function with additional
# 'averaged_samples' argument.
partial_gp_loss = partial(self.gradient_penalty_loss,
averaged_samples=interpolated_data)
# Keras requires function names.
partial_gp_loss.__name__ = 'gradient_penalty'
self.critic_model = Model(inputs=[real_data, label, noise],
outputs=[valid, fake, validity_interpolated])
self.critic_model.compile(loss=[self.wasserstein_loss,
self.wasserstein_loss,
partial_gp_loss],
optimizer=optimizer,
loss_weights=[1, 1, 10])
# For the generator we freeze the critic's layers.
self.critic.trainable = False
self.generator.trainable = True
# Sampled noise for input to generator.
noise = Input(shape=(self.latent_dim,))
# Add label to input.
label = Input(shape=(1,))
# Generate data based of noise.
fake_data = self.generator([noise, label])
# Discriminator determines validity.
valid = self.critic([fake_data, label])
# Define generator model.
self.generator_model = Model([noise, label], valid)
self.generator_model.compile(loss=self.wasserstein_loss,
optimizer=optimizer)
def gradient_penalty_loss(self, y_true, y_pred, averaged_samples):
## Computes gradient penalty based on prediction and weighted real / fake samples
gradients = K.gradients(y_pred, averaged_samples)[0]
# compute the euclidean norm by squaring ...
gradients_sqr = K.square(gradients)
# ... summing over the rows ...
gradients_sqr_sum = K.sum(gradients_sqr,
axis=np.arange(1, len(gradients_sqr.shape)))
# ... and sqrt
gradient_l2_norm = K.sqrt(gradients_sqr_sum)
# compute lambda * (1 - ||grad||)^2 still for each single sample
gradient_penalty = K.square(1 - abs(gradient_l2_norm))
# return the mean as loss over all the batch samples
return K.mean(gradient_penalty)
def wasserstein_loss(self, y_true, y_pred):
# Computes Wasserstein loss from real and fake predictions.
return K.mean(y_true * y_pred)
def build_generator(self):
model = Sequential(name="Generator")
# First hidden layer.
model.add(Dense(256, input_dim=self.latent_dim))
model.add(LeakyReLU(alpha=0.2))
model.add(Dropout(0.3))
# Second hidden layer.
model.add(Dense(512))
model.add(LeakyReLU(alpha=0.2))
model.add(Dropout(0.3))
# Third hidden layer.
model.add(Dense(1024))
model.add(LeakyReLU(alpha=0.2))
model.add(Dropout(0.3))
# Output layer.
model.add(Dense(self.data_dim, activation="tanh"))
model.summary()
# Noise and label input layers.
noise = Input(shape=(self.latent_dim,))
label = Input(shape=(1,), dtype="int32")
# Embed labels into onehot encoded vectors.
label_embedding = Flatten()(Embedding(self.num_classes, self.latent_dim)(label))
# Multiply noise and embedded labels to be used as model input.
model_input = multiply([noise, label_embedding])
generated_data = model(model_input)
return Model([noise, label], generated_data, name="Generator")
def build_critic(self):
model = Sequential(name="Critic")
# First hidden layer.
model.add(Dense(1024, input_dim=self.data_dim))
model.add(LeakyReLU(alpha=0.2))
# Second hidden layer.
model.add(Dense(512))
model.add(LeakyReLU(alpha=0.2))
# Third hidden layer.
model.add(Dense(256))
model.add(LeakyReLU(alpha=0.2))
# Output layer with linear activation.
model.add(Dense(1))
model.summary()
# Artificial data input.
generated_sample = Input(shape=self.data_dim)
# Label input.
label = Input(shape=(1,), dtype="int32")
# Embedd label as onehot vector.
label_embedding = Flatten()(Embedding(self.num_classes, self.data_dim)(label))
# Multiply fake data sample with label embedding to get critic input.
model_input = multiply([generated_sample, label_embedding])
validity = model(model_input)
return Model([generated_sample, label], validity, name="Critic")
#--- [3] Main part: training GAN (epoch iteration)
def train(self, epochs):
# Adversarial ground truths.
valid = -(np.ones((self.batch_size, 1)))
fake = np.ones((self.batch_size, 1))
dummy = np.zeros((self.batch_size, 1))
# Number of batches.
self.n_batches = math.floor(self.x_train.shape[0] / self.batch_size)
overhead = self.x_train.shape[0] % self.batch_size
for epoch in range(epochs):
# Reset training set.
self.x_train = x_train.copy()
self.y_train = y_train.copy()
# Select random overhead rows that do not fit into batches.
rand_overhead_idx = np.random.choice(range(self.x_train.shape[0]), overhead, replace=False)
# Remove random overhead rows.
self.x_train = np.delete(self.x_train, rand_overhead_idx, axis=0)
self.y_train = np.delete(self.y_train, rand_overhead_idx, axis=0)
# Split training data into batches.
x_batches = np.split(self.x_train, self.n_batches)
y_batches = np.split(self.y_train, self.n_batches)
for x_batch, y_batch, i in zip(x_batches, y_batches, range(self.n_batches)):
for _ in range(self.n_critic):
# ---------------------
# Train Critic
# ---------------------
# Generate random noise.
noise = np.random.normal(0, 1, (self.batch_size, self.latent_dim))
# Train the critic.
d_loss = self.critic_model.train_on_batch(
[x_batch, y_batch, noise],
[valid, fake, dummy])
# ---------------------
# Train Generator
# ---------------------
# Generate sample of artificial labels.
generated_labels = np.random.randint(0, self.num_classes, self.batch_size).reshape(-1, 1)
# Train generator.
g_loss = self.generator_model.train_on_batch([noise, generated_labels], valid)
# ---------------------
# Logging
# ---------------------
self.losslog.append([d_loss[0], g_loss])
DLOSS = "%.4f" % d_loss[0]
GLOSS = "%.4f" % g_loss
if i % 100 == 0:
print(f"{epoch} - {i}/{self.n_batches} \t [D loss: {DLOSS}] [G loss: {GLOSS}]")
#- Model training: core of the algorithm
wcgan = WCGANGP(
x_train,
y_train,
latent_dim=32,
batch_size=128,
n_critic=5)
n_epochs = 500
wcgan.train(epochs=n_epochs)
#--- [4] Synthetic data generation
label_ratios = { label:len(y_train[y_train == label])/y_train.shape[0] for label in np.unique(y_train) }
label_ratios
def generate_samples(n: int, latent_dim: int):
"""Use WCGAN to generate new data."""
noise = np.random.normal(0, 1, (n, latent_dim))
# Create sampled labels.
sampled_labels = [
np.full(round(ratio*n), label).tolist()
for label, ratio in label_ratios.items()
]
# Convert list to numpy array.
sampled_labels = np.array((list(itertools.chain(*sampled_labels))))
# Use CGAN to generate aritficial data.
return wcgan.generator.predict([noise, sampled_labels])
generated_samples = generate_samples(x_train.shape[0], wcgan.latent_dim)
# real_samples = pd.DataFrame(x_train)
df_real = pd.DataFrame(x_train, columns = ['tenure', 'MonthlyCharges', 'TotalChargeResidues'])
df_synth = pd.DataFrame(generated_samples, columns = ['tenure', 'MonthlyCharges', 'TotalChargeResidues'])
df_real.to_csv('WGAN_telecom_real.csv')
df_synth.to_csv('WGAN_telecom_synth.csv')
print(df_synth)
#--- [5] Synthetic data evaluation
# table_evaluator = TableEvaluator(
# real_samples,
# pd.DataFrame(generated_samples)
# )
# table_evaluator.visual_evaluation()
plt.figure(figsize=(10,5))
#plt.ylim(-6, 3)
plt.plot(wcgan.losslog)
plt.title("WCGANGP Losses")
plt.xlabel("Batches")
plt.ylabel("Loss")
plt.legend(['Critic loss', 'Generator loss'])
plt.show()
"""
plt.figure(figsize=(10,5))
# plt.ylim(-6, 2)
plt.plot(wcgan.losslog)
plt.title("WCGANGP Losses")
plt.xlabel("Batches")
plt.ylabel("Loss")
plt.legend(['Critic loss', 'Generator loss'])
plt.figure(figsize=(10,5))
# plt.ylim(-6, 2)
plt.plot(wcgan.losslog)
plt.title("WCGANGP Losses")
plt.xlabel("Batches")
plt.ylabel("Loss")
plt.legend(['Critic loss', 'Generator loss'])
plt.figure(figsize=(10,5))
# plt.ylim(-6, 2)
plt.plot(wcgan.losslog)
plt.title("WCGANGP Losses")
plt.xlabel("Batches")
plt.ylabel("Loss")
plt.legend(['Critic loss', 'Generator loss'])
plt.figure(figsize=(10,5))
plt.ylim(-6, 2)
plt.plot(wcgan.losslog)
plt.title("WCGANGP Losses")
plt.xlabel("Batches")
plt.ylabel("Loss")
plt.legend(['Critic loss', 'Generator loss'])
"""
"""**Saving models**"""
# wcgan.generator.save("models/generator.h5")
# wcgan.critic.save("models/critic.h5")