-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathgan.py
691 lines (582 loc) · 22.6 KB
/
gan.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
import argparse
import numbers
import numpy as np
import tensorflow as tf
def check_random_state(seed):
"""Turn seed into a np.random.RandomState instance.
Parameters
----------
seed : None or int or instance of RandomState
If seed is None, return the RandomState singleton used by np.random.
If seed is an int, return a new RandomState instance seeded with seed.
If seed is already a RandomState instance, return it.
Otherwise raise ValueError.
Notes
-----
This routine is from scikit-learn. See:
http://scikit-learn.org/stable/developers/utilities.html#validation-tools.
"""
if seed is None or seed is np.random:
return np.random.mtrand._rand
if isinstance(seed, (numbers.Integral, np.integer)):
return np.random.RandomState(seed)
if isinstance(seed, np.random.RandomState):
return seed
raise ValueError(
"%r cannot be used to seed a numpy.random.RandomState"
" instance" % seed
)
def init_xavier(fan, constant=1):
"""Xavier initialization of network weights."""
fan_in, fan_out = fan[0], fan[1]
low = -constant * np.sqrt(6.0 / (fan_in + fan_out))
high = constant * np.sqrt(6.0 / (fan_in + fan_out))
return tf.random_uniform(
(fan_in, fan_out), minval=low, maxval=high, dtype=tf.float32
)
def binary_crossentropy(preds, targets, offset=1e-10, name=None):
"""Computes binary cross entropy given `preds`.
For brevity, let `x = preds`, `z = targets`. The logistic loss is
loss(x, z) = - sum_i (x[i] * log(z[i]) + (1 - x[i]) * log(1 - z[i]))
"""
with ops.op_scope([preds, targets], name, "bce_loss") as name:
preds = ops.convert_to_tensor(preds, name="preds")
targets = ops.convert_to_tensor(targets, name="targets")
return tf.reduce_mean(
-(targets * tf.log(preds + offset) +
(1. - targets) * tf.log(1. - preds + offset))
)
def lrelu(X, leak=0.2, name="lrelu"):
"""Leaky rectified linear unit (LReLU)."""
with tf.variable_scope(name):
f1 = 0.5 * (1 + leak)
f2 = 0.5 * (1 - leak)
return f1 * X + f2 * abs(X)
def linear(
input_,
output_size,
scope=None,
stddev=0.5,
bias_start=0.0,
with_w=False,
):
"""Compute the linear dot product with the input and its weights plus bias.
Parameters
----------
input_ : Tensor
Tensor on which to apply dot product.
output_size : int
Number of outputs.
Returns
-------
Tensor
Linear dot product.
"""
shape = input_.get_shape().as_list()
with tf.variable_scope(scope or "Linear"):
matrix = tf.get_variable(
"Matrix",
[shape[1], output_size],
tf.float32,
tf.random_normal_initializer(stddev=stddev),
)
bias = tf.get_variable(
"bias",
[output_size],
initializer=tf.constant_initializer(bias_start),
)
if with_w:
return tf.matmul(input_, matrix) + bias, matrix, bias
else:
return tf.matmul(input_, matrix) + bias
def optimizer(loss, var_list):
"""Pre-training optimizer."""
initial_learning_rate = 0.02
decay = 0.95
num_decay_steps = 150
batch = tf.Variable(0)
learning_rate = tf.train.exponential_decay(
initial_learning_rate,
batch,
num_decay_steps,
decay,
staircase=True,
)
learning_rate = initial_learning_rate
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(
loss,
global_step=batch,
var_list=var_list,
)
return optimizer
class GeneratorDistribution(object):
"""Random noise generator."""
def __init__(self, n_input, random_state=None):
self.n_input = n_input
self.random_state = random_state
def sample(self, N):
s = np.empty([N, self.n_input])
for i in range(self.n_input):
s[:, i] = self.random_state.uniform(low=0.0, high=1.0, size=N)
return s
class GAN(object):
"""Generative Adversarial Network (GAN) implemented using TensorFlow.
The GAN framework uses two iteratively trained adversarial networks to
estimate a generative process. A generative model, G, captures the data
distribution, while a discriminative model, D, estimates the probability
that a sample came from the training data rather than from G, the
generative model [1].
Parameters
----------
num_epochs : int
Passes over the training dataset.
batch_size : int
Size of minibatches for stochastic optimizers.
d_hidden_dim : list
Discriminator number of units per hidden layer.
g_hidden_dim : list
Generator number of units per hidden layer.
n_input : int
Number of inputs to initial layer.
stddev : float
The standard deviation for the initialization noise.
pretrain : bool
Use unsupervised pre-training to initialize the discriminator weights.
d_transfer_fct : object
Discriminator transfer function for hidden layers.
g_transfer_fct : object
Generator transfer function for hidden layers.
W_init_fct : object
Initialization function for weights.
b_init_fct : object
Initialization function for biases.
d_learning_rate : float
Discriminator learning rate schedule for weight updates.
g_learning_rate : float
Generator learning rate schedule for weight updates.
random_state : int or None, optional (default=None)
If int, random_state is the seed used by the random number generator.
If None, the random number generator is the RandomState instance used
by np.random.
log_every : int
Print loss after this many steps.
References
----------
.. [1] I. J. Goodfellow, J. Pouget-Abadie, M. Mirza, B. Xu, D.
Warde-Farley, S. Ozair, A. Courville, and Y. Bengio. "Generative
Adversarial Nets". Advances in Neural Information Processing
Systems 27 (NIPS), 2014.
Notes
-----
Based on related code:
- https://github.com/AYLIEN/gan-intro
- https://github.com/ProofByConstruction/better-explanations
"""
def __init__(
self,
num_epochs,
batch_size,
d_hidden_dim,
g_hidden_dim,
n_input,
stddev=0.5,
pretrain=False,
d_transfer_fct=lrelu,
g_transfer_fct=tf.nn.relu,
W_init_fct=init_xavier,
b_init_fct=tf.zeros,
d_learning_rate=0.01,
g_learning_rate=0.0005,
random_state=None,
log_every=None,
):
self.num_epochs = num_epochs
self.batch_size = batch_size
self.net_arch = {
"d_hidden_dim": d_hidden_dim,
"g_hidden_dim": g_hidden_dim,
"n_input": n_input,
"n_output": n_input,
}
self.stddev = stddev
self.d_pretrain = pretrain
self.d_transfer_fct = d_transfer_fct
self.g_transfer_fct = g_transfer_fct
self.W_init_fct = W_init_fct
self.b_init_fct = b_init_fct
self.d_learning_rate = d_learning_rate
self.g_learning_rate = g_learning_rate
self.random_state = check_random_state(random_state)
tf.set_random_seed(random_state)
self.log_every = log_every
# Initialize generator distribution.
self.gen = GeneratorDistribution(
self.net_arch["n_input"], self.random_state)
# Create discriminator and generator networks.
self._create_networks()
# Define the loss function.
self._create_loss_optimizer()
# Initialize the TensorFlow variables.
init = tf.global_variables_initializer()
# Launch the session.
self.sess = tf.InteractiveSession()
self.sess.run(init)
self.saver = tf.train.Saver(tf.global_variables())
def _generator(
self,
layer_input,
layer_dim,
output_dim,
batch_norm=True,
stddev=0.5,
):
"""Define the generator network.
Parameters
----------
layer_input : Tensor
Input to the initial layer.
layer_dim : list
Number of neurons for each hidden layer of the generator network.
output_dim : int
Number of neurons for the output of the generator network.
Returns the output of the generator network.
"""
for layer_i, n_output in enumerate(layer_dim):
if batch_norm:
layer_input = tf.contrib.layers.batch_norm(
layer_input, scope="G_{0}".format(layer_i))
output = self.g_transfer_fct(
linear(
layer_input,
n_output,
scope="G_{0}".format(layer_i),
stddev=stddev
)
)
layer_input = output
return tf.nn.tanh(
linear(layer_input, output_dim, scope="G_final", stddev=stddev)
)
#return tf.nn.relu(
# linear(layer_input, output_dim, scope="G_final", stddev=stddev)
#)
def _discriminator(self, layer_input, layer_dim, stddev=0.5):
"""Define the discriminator network.
Parameters
----------
layer_input : Tensor
Input to the initial layer.
layer_dim : list
Number of neurons for each hidden layer of the discriminator network.
Returns the output of the discriminator network. The output layer has
one neuron for binary discrimination.
"""
for layer_i, n_output in enumerate(layer_dim):
output = self.d_transfer_fct(
linear(
layer_input,
n_output,
scope="D_{0}".format(layer_i),
stddev=stddev,
)
)
layer_input = output
# return tf.nn.sigmoid(linear(layer_input, 1, scope="D_final"))
return lrelu(linear(layer_input, 1, scope="D_final", stddev=stddev))
def _create_networks(self):
"""Initialize the discriminator and generator networks.
In order to make sure that the discriminator is providing useful gradient
information to the generator from the start, we can pretrain the
discriminator using a maximum likelihood objective. We define the network
for this pretraining step scoped as D_pre.
"""
# Pretrain (optional).
if self.d_pretrain:
with tf.variable_scope("D_pre"):
self.pre_input = tf.placeholder(
tf.float32, shape=(self.batch_size, self.net_arch["n_input"])
)
self.pre_labels = tf.placeholder(
tf.float32, shape=(self.batch_size, self.net_arch["n_input"])
)
D_pre = self._discriminator(
self.pre_input,
self.net_arch["d_hidden_dim"],
stddev=self.stddev
)
self.pre_loss = tf.reduce_mean(
tf.square(D_pre - self.pre_labels))
self.pre_opt = optimizer(self.pre_loss, None)
# Define the generator network.
with tf.variable_scope("G"):
self.z = tf.placeholder(
tf.float32, [None, self.net_arch["n_input"]], name="z"
)
self.G = self._generator(
self.z,
self.net_arch["g_hidden_dim"],
self.net_arch["n_output"],
batch_norm=True,
stddev=self.stddev,
)
# Define the discriminator network.
with tf.variable_scope("D") as scope:
self.x = tf.placeholder(
tf.float32, [None, self.net_arch["n_output"]], name="x"
)
self.D1 = self._discriminator(
self.x, self.net_arch["d_hidden_dim"], stddev=self.stddev
)
scope.reuse_variables()
self.D2 = self._discriminator(
self.G, self.net_arch["d_hidden_dim"], stddev=self.stddev
)
def _create_loss_optimizer(self):
"""Define the cost functions."""
# Define two discriminator losses, based on the fake and real
# discriminator predictions.
self.loss_d_real = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
labels=tf.ones_like(self.D1), logits=self.D1
)
)
self.loss_d_fake = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
labels=tf.zeros_like(self.D2), logits=self.D2
)
)
# Define the loss for the discriminator and generator networks.
self.loss_d = tf.add(self.loss_d_real, self.loss_d_fake)
self.loss_g = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
labels=tf.ones_like(self.D2), logits=self.D2
)
)
t_vars = tf.trainable_variables()
if self.d_pretrain:
self.d_pre_vars = [
var for var in t_vars if var.name.startswith("D_pre/")
]
self.d_vars = [var for var in t_vars if var.name.startswith("D/")]
self.g_vars = [var for var in t_vars if var.name.startswith("G/")]
# Define optimizers for the discriminator and generator networks.
opt_d = tf.train.AdamOptimizer(self.d_learning_rate, beta1=0.5)
opt_g = tf.train.AdamOptimizer(self.g_learning_rate, beta1=0.5)
self.opt_d = opt_d.minimize(self.loss_d, var_list=self.d_vars)
self.opt_g = opt_g.minimize(self.loss_g, var_list=self.g_vars)
def sample(self, n_samples):
"""Generate samples.
Parameters
----------
n_samples : int
Number of samples to generate.
Returns samples.
"""
batch_size = min(self.batch_size, n_samples)
# Generate samples.
zs = np.empty([n_samples, self.net_arch["n_input"]])
for i in range(self.net_arch["n_input"]):
zs[:, i] = self.random_state.uniform(
low=0.0, high=1.0, size=n_samples
)
samples = np.zeros((n_samples, self.z.get_shape()[1]))
for i in range(n_samples // batch_size):
z_batch = np.reshape(
zs[batch_size * i:batch_size * (i+1)],
(batch_size, self.z.get_shape()[1])
)
samples[batch_size * i:batch_size * (i+1)] = self.sess.run(
self.G, feed_dict={self.z: z_batch}
)
return samples
def partial_fit(self, X, Z):
"""Train model based on mini-batch of input data.
Parameters
----------
X : ndarray, shape (n_samples, n_features)
Matrix containing the data to be learned for the discriminator.
Z : ndarray, shape (n_samples, n_features)
Matrix containing the data to be learned for the generator.
Returns cost of mini-batch.
"""
# Update discriminator.
opt_d, cost_d = self.sess.run(
(self.opt_d, self.loss_d), feed_dict={self.z: Z, self.x: X}
)
# Update generator.
opt_g, cost_g = self.sess.run(
(self.opt_g, self.loss_g), feed_dict={self.z: Z}
)
return (cost_d, cost_g)
def fit(self, X, shuffle=True, display_step=5):
"""Training cycle.
Parameters
----------
X : ndarray, shape (n_samples, n_features)
Matrix containing the data to be learned.
Returns
-------
self : object
Returns self.
"""
if display_step is None:
display_step = self.log_every
n_samples = X.shape[0]
# Pretrain the discriminator.
if self.d_pretrain:
num_pretrain_steps = 1000
for step in xrange(num_pretrain_steps):
d = np.empty([self.batch_size, self.net_arch["n_input"]])
for i in range(self.net_arch["n_input"]):
d[:, i] = (self.random_state.random_sample(
self.batch_size) - 0.5
) * 10.0
labels = np.empty([self.batch_size, self.net_arch["n_input"]])
for i in range(self.net_arch["n_input"]):
labels[:, i] = self.random_state.uniform(
low=0.0, high=1.0, size=d.shape[0]
)
pretrain_loss, _ = self.sess.run([self.pre_loss, self.pre_opt], {
self.pre_input: np.reshape(d, (self.batch_size, -1)),
self.pre_labels: np.reshape(
labels, (self.batch_size, self.net_arch["n_input"])
),
})
self.weightsD = self.sess.run(self.d_pre_vars)
# Copy the weights from pre-training over to the new discriminator
# network.
for i, v in enumerate(self.d_vars):
self.sess.run(v.assign(self.weightsD[i]))
for epoch in range(self.num_epochs):
if shuffle:
indices = np.arange(len(X))
self.random_state.shuffle(indices)
# Loop over all batches.
start_idxs = range(
0, len(X) - self.batch_size + 1, self.batch_size)
for start_idx in start_idxs:
if shuffle:
excerpt = indices[start_idx:start_idx + self.batch_size]
else:
excerpt = slice(start_idx, start_idx + self.batch_size)
batch_x = np.array(X[excerpt])
batch_z = self.gen.sample(self.batch_size)
# Fit training using batch data.
cost_d, cost_g = self.partial_fit(batch_x, batch_z)
if len(start_idxs) > 0:
if display_step and epoch % display_step == 0:
print(
"Epoch: {:d}".format(epoch + 1),
"loss_d_real: {:.4f}".format(
self.loss_d_real.eval({self.x: batch_x})
),
"loss_d_fake: {:.4f}".format(
self.loss_d_fake.eval({self.z: batch_z})
),
"loss_g: {:.4f}".format(
self.loss_g.eval({self.z: batch_z}))
)
return self
def close(self):
"""Closes the TensorFlow session."""
self.sess.close()
def main(data, n_samples, args):
tf.reset_default_graph()
model = GAN(
args.num_epochs,
args.batch_size,
args.d_hidden_dim,
args.g_hidden_dim,
args.n_input,
args.stddev,
args.pretrain,
args.d_transfer_fct,
args.g_transfer_fct,
args.W_init_fct,
args.b_init_fct,
args.d_learning_rate,
args.g_learning_rate,
args.random_state,
args.log_every,
)
model.fit(data)
samples = model.sample(n_samples)
model.close()
return samples
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--num-epochs", type=int, default=1000,
help="Passes over the training dataset.")
parser.add_argument("--batch-size", type=int, default=100,
help="Size of minibatches for stochastic optimizers.")
parser.add_argument("--n_d_hidden_dim", type=list, default=(100,),
help="Discriminator number of units per hidden layer.")
parser.add_argument("--n_g_hidden_dim", type=list, default=(100,),
help="Generator number of units per hidden layer.")
parser.add_argument("--n_input", type=int, default=2,
help="Number of inputs to the initial layer.")
parser.add_argument("--stddev", type=int, default=0.5,
help="The standard deviation for the initialization "
"noise.")
parser.add_argument("--pretrain", type=int, default=False,
help="Use unsupervised pre-training to initialize the "
"discriminator weights.")
parser.add_argument("--d_transfer_fct", type=object, default=lrelu,
help="Discriminator transfer function for hidden "
"layers.")
parser.add_argument("--g_transfer_fct", type=object, default=tf.nn.relu,
help="Generator transfer function for hidden layers.")
parser.add_argument("--W_init_fct", type=object, default=init_xavier,
help="Initialization function for weights.")
parser.add_argument("--b_init_fct", type=object, default=tf.zeros,
help="Initialization function for biases.")
parser.add_argument("--d_learning_rate", type=float, default=0.01,
help="Discriminator learning rate schedule for weight "
"updates.")
parser.add_argument("--g_learning_rate", type=float, default=0.0005,
help="Generator learning rate schedule for weight "
"updates.")
parser.add_argument("--random_state", type=int, default=None,
help="The seed used by the random number generator.")
parser.add_argument("--log_every", type=int, default=10,
help="Print loss after this many steps.")
return parser.parse_args()
# Test with MNIST.
def test_mnist():
import matplotlib as mpl
mpl.use("Agg")
import matplotlib.pyplot as plt
mnist = tf.keras.datasets.mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
img_rows, img_cols = 28, 28
n_train = X_train.shape[0]
n_test = X_test.shape[0]
X_train = X_train.reshape((n_train, img_rows*img_cols))
X_test = X_test.reshape((n_test, img_rows*img_cols))
# Standardize.
X_train = X_train / 256.
X_test = X_test / 256.
# One-hot encode.
y_train = np.eye(10)[y_train]
y_test = np.eye(10)[y_test]
gan = GAN(
num_epochs=10,
batch_size=100,
d_hidden_dim=(512, 256),
g_hidden_dim=(512, 256, 64),
n_input=784, # MNIST data input (img shape: 28*28)
stddev=0.01, # standard deviation for initialization noise
pretrain=False,
)
gan.fit(X_train, display_step=1)
samples = gan.sample(400)
gan.close()
fig, ax = plt.subplots(40, 10, figsize=(10, 40))
for i in range(400):
ax[i/10][i%10].imshow(np.reshape(samples[i], (28, 28)), cmap="gray")
ax[i/10][i%10].axis("off")
#plt.show()
plt.savefig("gan_mnist_samples.png")
if __name__ == "__main__":
#main(data, 100, parse_args())
test_mnist()