Skip to content

Commit ae64c02

Browse files
author
Francisco Santos
committed
ActivationInterface integrations in regular models
1 parent 73b52ba commit ae64c02

6 files changed

Lines changed: 56 additions & 31 deletions

File tree

src/ydata_synthetic/synthesizers/regular/cgan/model.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""CGAN implementation"""
22
import os
33
from os import path
4-
from typing import List, Union
4+
from typing import List, Union, Optional, NamedTuple
55

66
import numpy as np
7-
from numpy import array, vstack, zeros, empty, hstack, ndarray
7+
from numpy import array, vstack, empty, hstack, ndarray
88
from numpy.random import normal
99
from pandas import DataFrame
10-
from tensorflow import convert_to_tensor, dtypes, expand_dims, tile, concat, constant
10+
from tensorflow import convert_to_tensor, dtypes, expand_dims, tile
1111
from tensorflow import data as tfdata
1212
from tensorflow.keras import Model
1313
from tensorflow.keras.layers import Dense, Dropout, Input, Flatten, Embedding, multiply
@@ -16,6 +16,7 @@
1616

1717
from ydata_synthetic.synthesizers import TrainParameters
1818
from ydata_synthetic.synthesizers.gan import BaseModel
19+
from ydata_synthetic.utils.gumbel_softmax import ActivationInterface
1920

2021

2122
class CGAN(BaseModel):
@@ -27,9 +28,10 @@ def __init__(self, model_parameters, num_classes):
2728
self.label_col = None
2829
super().__init__(model_parameters)
2930

30-
def define_gan(self):
31+
def define_gan(self, processor_info: Optional[NamedTuple] = None):
3132
self.generator = Generator(self.batch_size, self.num_classes). \
32-
build_model(input_shape=(self.noise_dim,), dim=self.layers_dim, data_dim=self.data_dim)
33+
build_model(input_shape=(self.noise_dim,), dim=self.layers_dim, data_dim=self.data_dim,
34+
processor_info = processor_info)
3335

3436
self.discriminator = Discriminator(self.batch_size, self.num_classes). \
3537
build_model(input_shape=(self.data_dim,), dim=self.layers_dim)
@@ -100,7 +102,7 @@ def train(self, data: Union[DataFrame, array], label_col: str, train_arguments:
100102

101103
processed_data = self.processor.transform(data)
102104
self.data_dim = processed_data.shape[1]
103-
self.define_gan()
105+
self.define_gan(self.processor.col_transform_info if preprocess else None)
104106

105107
# Merging labels with processed data
106108
processed_data = hstack([processed_data, label])
@@ -177,7 +179,7 @@ def __init__(self, batch_size, num_classes):
177179
self.batch_size = batch_size
178180
self.num_classes = num_classes
179181

180-
def build_model(self, input_shape, dim, data_dim):
182+
def build_model(self, input_shape, dim, data_dim, processor_info: Optional[NamedTuple] = None):
181183
noise = Input(shape=input_shape, batch_size=self.batch_size)
182184
label = Input(shape=(1,), batch_size=self.batch_size, dtype='int32')
183185
label_embedding = Flatten()(Embedding(self.num_classes, 1)(label))
@@ -187,6 +189,8 @@ def build_model(self, input_shape, dim, data_dim):
187189
x = Dense(dim * 2, activation='relu')(x)
188190
x = Dense(dim * 4, activation='relu')(x)
189191
x = Dense(data_dim)(x)
192+
if processor_info:
193+
x = ActivationInterface(processor_info).call(x)
190194
return Model(inputs=[noise, label], outputs=x)
191195

192196

src/ydata_synthetic/synthesizers/regular/cramergan/model.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
from os import path
3-
from typing import List
3+
from typing import List, Optional, NamedTuple
44

55
import numpy as np
66
import tensorflow as tf
@@ -12,6 +12,7 @@
1212
from ydata_synthetic.synthesizers import TrainParameters
1313
from ydata_synthetic.synthesizers.gan import BaseModel
1414
from ydata_synthetic.synthesizers.loss import Mode, gradient_penalty
15+
from ydata_synthetic.utils.gumbel_softmax import ActivationInterface
1516

1617

1718
class CRAMERGAN(BaseModel):
@@ -26,9 +27,10 @@ def __init__(self, model_parameters, gradient_penalty_weight=10):
2627
self.gradient_penalty_weight = gradient_penalty_weight
2728
super().__init__(model_parameters)
2829

29-
def define_gan(self):
30+
def define_gan(self, processor_info: Optional[NamedTuple] = None):
3031
self.generator = Generator(self.batch_size). \
31-
build_model(input_shape=(self.noise_dim,), dim=self.layers_dim, data_dim=self.data_dim)
32+
build_model(input_shape=(self.noise_dim,), dim=self.layers_dim, data_dim=self.data_dim,
33+
processor_info=processor_info)
3234

3335
self.critic = Critic(self.batch_size). \
3436
build_model(input_shape=(self.data_dim,), dim=self.layers_dim)
@@ -147,7 +149,7 @@ def train(self, data, train_arguments: TrainParameters, num_cols: List[str],
147149

148150
data = self.processor.transform(data)
149151
self.data_dim = data.shape[1]
150-
self.define_gan()
152+
self.define_gan(self.processor.col_transform_info if preprocess else None)
151153

152154
iterations = int(abs(data.shape[0] / self.batch_size) + 1)
153155

@@ -192,12 +194,14 @@ def __init__(self, batch_size):
192194
"""Simple generator with dense feedforward layers."""
193195
self.batch_size = batch_size
194196

195-
def build_model(self, input_shape, dim, data_dim):
197+
def build_model(self, input_shape, dim, data_dim, processor_info: Optional[NamedTuple] = None):
196198
input_ = Input(shape=input_shape, batch_size=self.batch_size)
197199
x = Dense(dim, activation='relu')(input_)
198200
x = Dense(dim * 2, activation='relu')(x)
199201
x = Dense(dim * 4, activation='relu')(x)
200202
x = Dense(data_dim)(x)
203+
if processor_info:
204+
x = ActivationInterface(processor_info)(x)
201205
return Model(inputs=input_, outputs=x)
202206

203207
class Critic(tf.keras.Model):

src/ydata_synthetic/synthesizers/regular/dragan/model.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
from os import path
33

4+
from typing import Optional, NamedTuple
45
import tensorflow as tf
56
import tqdm
67
from tensorflow.keras import Model, initializers
@@ -9,6 +10,7 @@
910

1011
from ydata_synthetic.synthesizers.gan import BaseModel
1112
from ydata_synthetic.synthesizers.loss import Mode, gradient_penalty
13+
from ydata_synthetic.utils.gumbel_softmax import ActivationInterface
1214

1315

1416
class DRAGAN(BaseModel):
@@ -21,10 +23,11 @@ def __init__(self, model_parameters, n_discriminator, gradient_penalty_weight=10
2123
self.gradient_penalty_weight = gradient_penalty_weight
2224
super().__init__(model_parameters)
2325

24-
def define_gan(self):
26+
def define_gan(self, col_transform_info: Optional[NamedTuple] = None):
2527
# define generator/discriminator
2628
self.generator = Generator(self.batch_size). \
27-
build_model(input_shape=(self.noise_dim,), dim=self.layers_dim, data_dim=self.data_dim)
29+
build_model(input_shape=(self.noise_dim,), dim=self.layers_dim, data_dim=self.data_dim,
30+
processor_info=col_transform_info)
2831

2932
self.discriminator = Discriminator(self.batch_size). \
3033
build_model(input_shape=(self.data_dim,), dim=self.layers_dim)
@@ -118,7 +121,7 @@ def train(self, data, train_arguments, num_cols, cat_cols, preprocess: bool = Tr
118121

119122
processed_data = self.processor.transform(data)
120123
self.data_dim = processed_data.shape[1]
121-
self.define_gan()
124+
self.define_gan(self.processor.col_transform_info if preprocess else None)
122125

123126
train_loader = self.get_data_batch(processed_data, self.batch_size)
124127

@@ -167,10 +170,12 @@ class Generator(Model):
167170
def __init__(self, batch_size):
168171
self.batch_size = batch_size
169172

170-
def build_model(self, input_shape, dim, data_dim):
173+
def build_model(self, input_shape, dim, data_dim, processor_info: NamedTuple = None):
171174
input = Input(shape=input_shape, batch_size = self.batch_size)
172175
x = Dense(dim, kernel_initializer=initializers.TruncatedNormal(mean=0., stddev=0.5), activation='relu')(input)
173176
x = Dense(dim * 2, activation='relu')(x)
174177
x = Dense(dim * 4, activation='relu')(x)
175178
x = Dense(data_dim)(x)
179+
if processor_info:
180+
x = ActivationInterface(processor_info)(x)
176181
return Model(inputs=input, outputs=x)

src/ydata_synthetic/synthesizers/regular/vanillagan/model.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import os
22
from os import path
33
import numpy as np
4-
from typing import List
4+
from typing import List, Optional, NamedTuple
55
from tqdm import trange
66

77
from ydata_synthetic.synthesizers.gan import BaseModel
88
from ydata_synthetic.synthesizers import TrainParameters
9+
from ydata_synthetic.utils.gumbel_softmax import ActivationInterface
910

1011
import tensorflow as tf
1112
from tensorflow.keras.layers import Input, Dense, Dropout
@@ -19,9 +20,10 @@ class VanilllaGAN(BaseModel):
1920
def __init__(self, model_parameters):
2021
super().__init__(model_parameters)
2122

22-
def define_gan(self):
23+
def define_gan(self, processor_info: Optional[NamedTuple]):
2324
self.generator = Generator(self.batch_size).\
24-
build_model(input_shape=(self.noise_dim,), dim=self.layers_dim, data_dim=self.data_dim)
25+
build_model(input_shape=(self.noise_dim,), dim=self.layers_dim, data_dim=self.data_dim,
26+
processor_info = processor_info)
2527

2628
self.discriminator = Discriminator(self.batch_size).\
2729
build_model(input_shape=(self.data_dim,), dim=self.layers_dim)
@@ -77,7 +79,7 @@ def train(self, data, train_arguments: TrainParameters, num_cols: List[str],
7779

7880
processed_data = self.processor.transform(data)
7981
self.data_dim = processed_data.shape[1]
80-
self.define_gan()
82+
self.define_gan(self.processor.col_transform_info if preprocess else None)
8183

8284
iterations = int(abs(data.shape[0]/self.batch_size)+1)
8385

@@ -131,12 +133,14 @@ class Generator(tf.keras.Model):
131133
def __init__(self, batch_size):
132134
self.batch_size=batch_size
133135

134-
def build_model(self, input_shape, dim, data_dim):
136+
def build_model(self, input_shape, dim, data_dim, processor_info: Optional[NamedTuple] = None):
135137
input= Input(shape=input_shape, batch_size=self.batch_size)
136138
x = Dense(dim, activation='relu')(input)
137139
x = Dense(dim * 2, activation='relu')(x)
138140
x = Dense(dim * 4, activation='relu')(x)
139141
x = Dense(data_dim)(x)
142+
if processor_info:
143+
x = ActivationInterface(processor_info)(x)
140144
return Model(inputs=input, outputs=x)
141145

142146
class Discriminator(tf.keras.Model):

src/ydata_synthetic/synthesizers/regular/wgan/model.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from os import mkdir, path
2-
from typing import List
2+
from typing import List, Optional, NamedTuple
33

44
import numpy as np
55
import tensorflow as tf
@@ -11,6 +11,7 @@
1111

1212
from ydata_synthetic.synthesizers import TrainParameters
1313
from ydata_synthetic.synthesizers.gan import BaseModel
14+
from ydata_synthetic.utils.gumbel_softmax import ActivationInterface
1415

1516

1617
#Auxiliary Keras backend class to calculate the Random Weighted average
@@ -41,9 +42,10 @@ def __init__(self, model_parameters, n_critic, clip_value=0.01):
4142
def wasserstein_loss(self, y_true, y_pred):
4243
return K.mean(y_true * y_pred)
4344

44-
def define_gan(self):
45+
def define_gan(self, processor_info: Optional[NamedTuple] = None):
4546
self.generator = Generator(self.batch_size). \
46-
build_model(input_shape=(self.noise_dim,), dim=self.layers_dim, data_dim=self.data_dim)
47+
build_model(input_shape=(self.noise_dim,), dim=self.layers_dim, data_dim=self.data_dim,
48+
processor_info=processor_info)
4749

4850
self.critic = Critic(self.batch_size). \
4951
build_model(input_shape=(self.data_dim,), dim=self.layers_dim)
@@ -97,7 +99,7 @@ def train(self, data, train_arguments: TrainParameters, num_cols: List[str],
9799

98100
processed_data = self.processor.transform(data)
99101
self.data_dim = processed_data.shape[1]
100-
self.define_gan()
102+
self.define_gan(self.processor.col_transform_info if preprocess else None)
101103

102104
#Create a summary file
103105
iterations = int(abs(data.shape[0]/self.batch_size)+1)
@@ -154,12 +156,14 @@ class Generator(tf.keras.Model):
154156
def __init__(self, batch_size):
155157
self.batch_size = batch_size
156158

157-
def build_model(self, input_shape, dim, data_dim):
159+
def build_model(self, input_shape, dim, data_dim, processor_info: Optional[NamedTuple] = None):
158160
input = Input(shape=input_shape, batch_size=self.batch_size)
159161
x = Dense(dim, activation='relu')(input)
160162
x = Dense(dim * 2, activation='relu')(x)
161163
x = Dense(dim * 4, activation='relu')(x)
162164
x = Dense(data_dim)(x)
165+
if processor_info:
166+
x = ActivationInterface(processor_info)(x)
163167
return Model(inputs=input, outputs=x)
164168

165169
class Critic(tf.keras.Model):

src/ydata_synthetic/synthesizers/regular/wgangp/model.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
from os import path
3-
from typing import List
3+
from typing import List, NamedTuple, Optional
44

55
import numpy as np
66
import tensorflow as tf
@@ -11,6 +11,7 @@
1111

1212
from ydata_synthetic.synthesizers import TrainParameters
1313
from ydata_synthetic.synthesizers.gan import BaseModel
14+
from ydata_synthetic.utils.gumbel_softmax import ActivationInterface
1415

1516

1617
class WGAN_GP(BaseModel):
@@ -24,9 +25,10 @@ def __init__(self, model_parameters, n_critic, gradient_penalty_weight=10):
2425
self.gradient_penalty_weight = gradient_penalty_weight
2526
super().__init__(model_parameters)
2627

27-
def define_gan(self):
28+
def define_gan(self, processor_info: Optional[NamedTuple] = None):
2829
self.generator = Generator(self.batch_size). \
29-
build_model(input_shape=(self.noise_dim,), dim=self.layers_dim, data_dim=self.data_dim)
30+
build_model(input_shape=(self.noise_dim,), dim=self.layers_dim, data_dim=self.data_dim,
31+
processor_info=processor_info)
3032

3133
self.critic = Critic(self.batch_size). \
3234
build_model(input_shape=(self.data_dim,), dim=self.layers_dim)
@@ -141,7 +143,7 @@ def train(self, data, train_arguments: TrainParameters, num_cols: List[str],
141143

142144
processed_data = self.processor.transform(data)
143145
self.data_dim = processed_data.shape[1]
144-
self.define_gan()
146+
self.define_gan(self.processor.col_transform_info if preprocess else None)
145147

146148
iterations = int(abs(data.shape[0]/self.batch_size)+1)
147149

@@ -176,12 +178,14 @@ class Generator(tf.keras.Model):
176178
def __init__(self, batch_size):
177179
self.batch_size = batch_size
178180

179-
def build_model(self, input_shape, dim, data_dim):
181+
def build_model(self, input_shape, dim, data_dim, processor_info: Optional[NamedTuple] = None):
180182
input = Input(shape=input_shape, batch_size=self.batch_size)
181183
x = Dense(dim, activation='relu')(input)
182184
x = Dense(dim * 2, activation='relu')(x)
183185
x = Dense(dim * 4, activation='relu')(x)
184186
x = Dense(data_dim)(x)
187+
if processor_info:
188+
x = ActivationInterface(processor_info)(x)
185189
return Model(inputs=input, outputs=x)
186190

187191
class Critic(tf.keras.Model):

0 commit comments

Comments
 (0)