@@ -21,15 +21,19 @@ def model_expressions(img_shape):
21
21
sigma = 0.001
22
22
n_in = np .prod (img_shape )
23
23
n_encoder = 1024
24
- n_hidden = 64
25
- n_generator = 1024
26
- n_discriminator = 1024
24
+ n_hidden = 32
25
+ n_generator = 2048
26
+ n_discriminator = 2048
27
27
28
28
encoder = cond_vaegan .ConditionalSequential ([
29
29
expr .Concatenate (axis = 1 ),
30
30
affine (n_encoder , gain ),
31
+ expr .nnet .BatchNormalization (),
31
32
expr .nnet .ReLU (),
33
+ expr .nnet .Dropout (0.5 ),
34
+ expr .Concatenate (axis = 1 ),
32
35
affine (n_encoder , gain ),
36
+ expr .nnet .BatchNormalization (),
33
37
expr .nnet .ReLU (),
34
38
])
35
39
sampler = vaegan .NormalSampler (
@@ -46,18 +50,21 @@ def model_expressions(img_shape):
46
50
affine (n_generator , gain ),
47
51
expr .nnet .BatchNormalization (),
48
52
expr .nnet .ReLU (),
53
+ expr .Concatenate (axis = 1 ),
49
54
affine (n_in , gain ),
50
55
expr .nnet .Sigmoid (),
51
56
])
52
57
discriminator = cond_vaegan .ConditionalSequential ([
53
- expr .nnet .Dropout (0.5 ),
54
58
expr .Concatenate (axis = 1 ),
55
59
affine (n_discriminator , gain ),
56
60
expr .nnet .ReLU (),
57
- expr .nnet .Dropout (0.5 ),
61
+ expr .nnet .Dropout (0.25 ),
58
62
expr .Concatenate (axis = 1 ),
59
63
affine (n_discriminator , gain ),
64
+ expr .nnet .BatchNormalization (),
60
65
expr .nnet .ReLU (),
66
+ expr .nnet .Dropout (0.25 ),
67
+ expr .Concatenate (axis = 1 ),
61
68
affine (1 , gain ),
62
69
expr .nnet .Sigmoid (),
63
70
@@ -71,9 +78,10 @@ def to_b01c(imgs_flat, img_shape):
71
78
72
79
73
80
def run ():
74
- mode = 'gan'
75
- experiment_name = mode
76
- filename = 'savestates/mnist_cond_' + experiment_name + '.pickle'
81
+ mode = 'vaegan'
82
+ vae_grad_scale = 0.025
83
+ experiment_name = mode + 'scale_%.5f' % vae_grad_scale
84
+ filename = 'savestates/mnist_' + experiment_name + '.pickle'
77
85
in_filename = filename
78
86
in_filename = None
79
87
print ('experiment_name' , experiment_name )
@@ -95,6 +103,7 @@ def run():
95
103
x_train = np .reshape (x_train , (x_train .shape [0 ], - 1 ))
96
104
x_test = np .reshape (x_test , (x_test .shape [0 ], - 1 ))
97
105
106
+
98
107
# Setup network
99
108
if in_filename is None :
100
109
print ('Creating new model' )
@@ -111,11 +120,12 @@ def run():
111
120
generator = generator ,
112
121
discriminator = discriminator ,
113
122
mode = mode ,
114
- reconstruct_error = expr .nnet .BinaryCrossEntropy ()
123
+ reconstruct_error = expr .nnet .BinaryCrossEntropy (),
124
+ vae_grad_scale = vae_grad_scale ,
115
125
)
116
126
117
127
# Prepare network inputs
118
- batch_size = 64
128
+ batch_size = 128
119
129
train_input = dp .SupervisedInput (x_train , y_train , batch_size = batch_size ,
120
130
epoch_size = 250 )
121
131
@@ -136,6 +146,7 @@ def run():
136
146
137
147
def plot ():
138
148
model .phase = 'test'
149
+ model .sampler .batch_size = 100
139
150
examples_z = model .embed (examples , examples_y )
140
151
examples_recon = model .reconstruct (examples_z , examples_y )
141
152
recon_video .append (img_tile (to_b01c (examples_recon , img_shape )))
@@ -144,12 +155,13 @@ def plot():
144
155
model .setup (** train_input .shapes )
145
156
model .phase = 'train'
146
157
158
+
147
159
# Train network
148
160
runs = [
149
- (50 , dp .RMSProp (learn_rate = 0.3 )),
150
- (150 , dp .RMSProp (learn_rate = 0.1 )),
151
- (5 , dp .RMSProp (learn_rate = 0.05 )),
161
+ (75 , dp .RMSProp (learn_rate = 0.075 )),
162
+ (25 , dp .RMSProp (learn_rate = 0.05 )),
152
163
(5 , dp .RMSProp (learn_rate = 0.01 )),
164
+ (5 , dp .RMSProp (learn_rate = 0.005 )),
153
165
]
154
166
try :
155
167
for n_epochs , learn_rule in runs :
@@ -167,12 +179,71 @@ def plot():
167
179
expressions = encoder , sampler , generator , discriminator
168
180
pickle .dump (expressions , f )
169
181
182
+ model .phase = 'test'
183
+ batch_size = 128
184
+ model .sampler .batch_size = 128
185
+ z = []
186
+ i = 0
187
+ z = model .embed (x_train , y_train )
188
+ print (z .shape )
189
+ z_mean = np .mean (z , axis = 0 )
190
+ z_std = np .std (z , axis = 0 )
191
+ z_cov = np .cov (z .T )
192
+ print (np .mean (z_mean ), np .std (z_mean ))
193
+ print (np .mean (z_std ), np .std (z_std ))
194
+ print (z_mean .shape , z_std .shape , z_cov .shape )
195
+
196
+
197
+ raw_input ('\n \n generate latent space video?\n ' )
170
198
print ('Generating latent space video' )
171
199
walk_video = Video ('plots/mnist_' + experiment_name + '_walk.mp4' )
172
- for z in random_walk (samples_z , 500 , step_std = 0.15 ):
200
+ for z in random_walk (samples_z , 500 , n_dir_steps = 10 , mean = z_mean , std = z_cov ):
173
201
samples = model .reconstruct (z , samples_y )
174
202
walk_video .append (img_tile (to_b01c (samples , img_shape )))
175
203
176
204
205
+
206
+ print ('Generating AdversarialMNIST dataset' )
207
+ _ , y_train , _ , y_test = dataset .arrays (dp_dtypes = True )
208
+ n = 0
209
+ batch_size = 512
210
+ advmnist_size = 1e6
211
+ x_advmnist = np .empty ((advmnist_size , 28 * 28 ))
212
+ y_advmnist = np .empty ((advmnist_size ,))
213
+ while n < advmnist_size :
214
+ samples_z = np .random .multivariate_normal (mean = z_mean , cov = z_cov ,
215
+ size = batch_size )
216
+ samples_z = samples_z .astype (dp .float_ )
217
+ start_idx = n % len (y_train )
218
+ stop_idx = (n + batch_size ) % len (y_train )
219
+ if start_idx > stop_idx :
220
+ samples_y = np .concatenate ([y_train [start_idx :], y_train [:stop_idx ]])
221
+ else :
222
+ samples_y = y_train [start_idx :stop_idx ]
223
+ y_advmnist [n :n + batch_size ] = samples_y [:advmnist_size - n ]
224
+ samples_y = one_hot (samples_y , n_classes ).astype (dp .float_ )
225
+ samples = model .reconstruct (samples_z , samples_y )
226
+ x_advmnist [n :n + batch_size ] = samples [:advmnist_size - n ]
227
+ n += batch_size
228
+
229
+
230
+ x_train = x_advmnist
231
+ y_train = y_advmnist
232
+ import sklearn .neighbors
233
+ clf = sklearn .neighbors .KNeighborsClassifier (n_neighbors = 1 , algorithm = 'brute' , n_jobs = - 1 )
234
+ clf .fit (x_train , y_train )
235
+ print ('KNN predict' )
236
+ step = 2500
237
+ errors = []
238
+ i = 0
239
+ while i < len (x_test ):
240
+ print (i )
241
+ errors .append (clf .predict (x_test [i :i + step ]) != y_test [i :i + step ])
242
+ i += step
243
+ error = np .mean (errors )
244
+ print ('Test error rate: %.4f' % error )
245
+
246
+ print ('DONE ' + experiment_name )
247
+
177
248
if __name__ == '__main__' :
178
249
run ()
0 commit comments