The Algorithms logoThe Algorithms
AboutDonate

A-Simple-GAN

G.A.N

Generative Adversial Network:

  • The main focus for GAN is to generate data from scratch.
  • It brings us closer to understanding intelligence.
  • It trains two deep networks, called Generator and Discriminator, that compete and cooperate with each other. In the course of training, both networks eventually learn how to perform their tasks.
    • The generator never actually sees examples from the domain and is adapted based on how well the discriminator performs.
# The Import Statements:
from matplotlib import pyplot
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.utils.vis_utils import plot_model
Using TensorFlow backend.

Defining a 1 D function:

y=f(x)

def function_1D(x):
    return x*x
inputs=np.arange(-0.5,0.6,0.1)
outputs=[function_1D(x) for x in inputs]
# plot the result
pyplot.plot(inputs, outputs)
pyplot.show()
#Defining random values
def generate_samples(n=100):
    x1=np.random.rand(n)-0.5
    x2=x1*x1
    x1=x1.reshape(n,1)
    x2=x2.reshape(n,1)
    return np.hstack((x1,x2))
# generate samples and plotting them
data = generate_samples()

pyplot.scatter(data[:, 0], data[:, 1])
pyplot.show()

Note:

a sample is comprised of a vector with two elements, one for the input and one for the output of our one-dimensional function.

The Discriminator

  • The difference from a typical CNN is the absence of max-pooling in between layers.
  • will have 1 hidden layer with 25 nodes.
  • will use the ReLU activation function
  • The output layer will have 1 node for the binary classification using the sigmoid activation function.
  • Loss Function: Binary Cross Entropy
  • Optimizer : Adam version of stochastic Gradient Descent
#Code for the Discriminator Unit:
def define_discriminator(n_inputs=2):
	model = Sequential()
	model.add(Dense(25, activation='relu', kernel_initializer='he_uniform', input_dim=n_inputs))
	model.add(Dense(1, activation='sigmoid'))
	# compile model
	model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
	return model
# define the discriminator model
model = define_discriminator()
# summarize the model
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 25)                75        
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 26        
=================================================================
Total params: 101
Trainable params: 101
Non-trainable params: 0
_________________________________________________________________
def generate_real_samples(n):
    x1=np.random.rand(n)-0.5
    x2=x1*x1
    x1=x1.reshape(n,1)
    x2=x2.reshape(n,1)
    X= np.hstack((x1,x2))
    y=np.ones((n,1))
    return X,y
def generate_fake_samples(n):
	# generate inputs in [-1, 1]
	X1 = -1 + np.random.rand(n) * 2
	# generate outputs in [-1, 1]
	X2 = -1 + np.random.rand(n) * 2
	# stack arrays
	X1 = X1.reshape(n, 1)
	X2 = X2.reshape(n, 1)
	X = np.hstack((X1, X2))
	# generate class labels
	y = np.zeros((n, 1))
	return X, y
#training the discriminator model
def train_discriminator(model, n_epochs=1000, n_batch=128):
	half_batch = int(n_batch / 2)
	# run epochs manually
	for i in range(n_epochs):
		# generate real examples
		X_real, y_real = generate_real_samples(half_batch)
		# update model
		model.train_on_batch(X_real, y_real)
		# generate fake examples
		X_fake, y_fake = generate_fake_samples(half_batch)
		# update model
		model.train_on_batch(X_fake, y_fake)
		# evaluate the model
		_, acc_real = model.evaluate(X_real, y_real, verbose=0)
		_, acc_fake = model.evaluate(X_fake, y_fake, verbose=0)
		print(i, acc_real, acc_fake)
# define the discriminator model
model = define_discriminator()
# fit the model
train_discriminator(model)
0 0.5 0.15625
1 0.546875 0.25
2 0.578125 0.21875
3 0.5625 0.21875
4 0.640625 0.234375
5 0.515625 0.40625
6 0.515625 0.46875
7 0.59375 0.390625
8 0.5625 0.46875
9 0.53125 0.4375
10 0.46875 0.484375
11 0.421875 0.53125
12 0.46875 0.484375
13 0.515625 0.5625
14 0.46875 0.6875
15 0.5625 0.515625
16 0.515625 0.53125
17 0.53125 0.5
18 0.484375 0.609375
19 0.40625 0.5
20 0.46875 0.671875
21 0.484375 0.59375
22 0.546875 0.609375
23 0.375 0.53125
24 0.421875 0.53125
25 0.34375 0.65625
26 0.421875 0.484375
27 0.421875 0.578125
28 0.328125 0.546875
29 0.359375 0.640625
30 0.4375 0.609375
31 0.296875 0.484375
32 0.296875 0.53125
33 0.515625 0.625
34 0.359375 0.59375
35 0.40625 0.65625
36 0.359375 0.6875
37 0.28125 0.578125
38 0.328125 0.578125
39 0.328125 0.671875
40 0.40625 0.671875
41 0.296875 0.515625
42 0.296875 0.578125
43 0.296875 0.625
44 0.34375 0.625
45 0.390625 0.671875
46 0.375 0.609375
47 0.34375 0.59375
48 0.359375 0.578125
49 0.359375 0.6875
50 0.34375 0.65625
51 0.375 0.515625
52 0.390625 0.609375
53 0.234375 0.625
54 0.359375 0.625
55 0.234375 0.59375
56 0.40625 0.6875
57 0.34375 0.765625
58 0.296875 0.609375
59 0.265625 0.59375
60 0.25 0.65625
61 0.390625 0.65625
62 0.265625 0.65625
63 0.46875 0.703125
64 0.40625 0.59375
65 0.359375 0.625
66 0.28125 0.625
67 0.3125 0.625
68 0.328125 0.640625
69 0.390625 0.625
70 0.28125 0.640625
71 0.328125 0.609375
72 0.296875 0.59375
73 0.125 0.703125
74 0.359375 0.765625
75 0.359375 0.65625
76 0.34375 0.5625
77 0.234375 0.671875
78 0.34375 0.6875
79 0.21875 0.734375
80 0.3125 0.78125
81 0.28125 0.6875
82 0.34375 0.640625
83 0.375 0.765625
84 0.34375 0.640625
85 0.328125 0.703125
86 0.21875 0.75
87 0.25 0.65625
88 0.28125 0.640625
89 0.3125 0.78125
90 0.234375 0.71875
91 0.328125 0.71875
92 0.25 0.671875
93 0.25 0.703125
94 0.265625 0.546875
95 0.296875 0.546875
96 0.328125 0.625
97 0.265625 0.75
98 0.296875 0.671875
99 0.265625 0.703125
100 0.15625 0.734375
101 0.234375 0.796875
102 0.25 0.71875
103 0.21875 0.828125
104 0.21875 0.828125
105 0.328125 0.765625
106 0.21875 0.671875
107 0.28125 0.84375
108 0.21875 0.796875
109 0.21875 0.78125
110 0.25 0.65625
111 0.25 0.8125
112 0.21875 0.859375
113 0.28125 0.796875
114 0.234375 0.625
115 0.21875 0.671875
116 0.234375 0.859375
117 0.265625 0.71875
118 0.21875 0.765625
119 0.234375 0.765625
120 0.234375 0.6875
121 0.234375 0.8125
122 0.296875 0.828125
123 0.25 0.796875
124 0.234375 0.890625
125 0.359375 0.765625
126 0.203125 0.75
127 0.328125 0.703125
128 0.203125 0.78125
129 0.265625 0.796875
130 0.3125 0.828125
131 0.21875 0.828125
132 0.25 0.828125
133 0.296875 0.84375
134 0.203125 0.875
135 0.34375 0.734375
136 0.421875 0.765625
137 0.15625 0.796875
138 0.359375 0.75
139 0.234375 0.75
140 0.421875 0.71875
141 0.28125 0.703125
142 0.1875 0.6875
143 0.21875 0.828125
144 0.25 0.890625
145 0.265625 0.9375
146 0.171875 0.8125
147 0.265625 0.84375
148 0.296875 0.90625
149 0.171875 0.875
150 0.21875 0.765625
151 0.25 0.875
152 0.25 0.90625
153 0.25 0.90625
154 0.328125 0.796875
155 0.296875 0.921875
156 0.234375 0.84375
157 0.25 0.875
158 0.1875 0.9375
159 0.234375 0.859375
160 0.234375 0.890625
161 0.28125 0.984375
162 0.21875 0.90625
163 0.21875 0.90625
164 0.265625 0.953125
165 0.28125 0.953125
166 0.328125 0.984375
167 0.28125 0.96875
168 0.25 0.953125
169 0.359375 0.921875
170 0.34375 1.0
171 0.328125 0.953125
172 0.296875 0.96875
173 0.359375 0.921875
174 0.28125 0.9375
175 0.375 0.984375
176 0.359375 0.96875
177 0.265625 0.984375
178 0.21875 0.96875
179 0.28125 0.9375
180 0.359375 1.0
181 0.34375 0.984375
182 0.40625 0.984375
183 0.296875 0.953125
184 0.34375 0.984375
185 0.3125 0.984375
186 0.359375 0.96875
187 0.375 0.984375
188 0.53125 0.953125
189 0.40625 0.921875
190 0.453125 0.953125
191 0.375 0.96875
192 0.609375 0.9375
193 0.40625 0.96875
194 0.4375 0.96875
195 0.390625 0.96875
196 0.40625 0.984375
197 0.4375 1.0
198 0.546875 0.953125
199 0.328125 1.0
200 0.328125 0.984375
201 0.328125 0.984375
202 0.453125 0.96875
203 0.34375 0.96875
204 0.34375 0.984375
205 0.359375 0.96875
206 0.359375 0.984375
207 0.421875 0.96875
208 0.5 0.984375
209 0.40625 1.0
210 0.390625 0.96875
211 0.546875 0.96875
212 0.390625 0.984375
213 0.5 0.921875
214 0.578125 0.96875
215 0.671875 0.9375
216 0.484375 0.984375
217 0.4375 0.90625
218 0.453125 0.96875
219 0.421875 0.953125
220 0.625 0.9375
221 0.5 0.953125
222 0.578125 0.984375
223 0.53125 0.9375
224 0.5625 0.984375
225 0.546875 0.984375
226 0.5625 0.96875
227 0.578125 0.984375
228 0.5625 0.953125
229 0.546875 0.90625
230 0.546875 0.921875
231 0.75 1.0
232 0.546875 1.0
233 0.671875 1.0
234 0.5625 0.96875
235 0.640625 0.984375
236 0.6875 0.921875
237 0.59375 0.90625
238 0.71875 0.921875
239 0.609375 1.0
240 0.59375 0.953125
241 0.65625 0.953125
242 0.640625 0.984375
243 0.6875 0.984375
244 0.546875 0.9375
245 0.71875 1.0
246 0.71875 0.953125
247 0.609375 1.0
248 0.671875 0.96875
249 0.625 0.953125
250 0.53125 0.921875
251 0.6875 0.984375
252 0.625 0.921875
253 0.65625 0.984375
254 0.625 0.9375
255 0.75 0.96875
256 0.609375 0.984375
257 0.640625 0.921875
258 0.6875 0.984375
259 0.671875 1.0
260 0.625 0.9375
261 0.65625 0.984375
262 0.703125 0.96875
263 0.734375 1.0
264 0.65625 0.984375
265 0.578125 0.9375
266 0.59375 0.96875
267 0.703125 0.921875
268 0.734375 1.0
269 0.734375 1.0
270 0.703125 0.984375
271 0.734375 0.984375
272 0.6875 0.953125
273 0.703125 0.96875
274 0.71875 1.0
275 0.65625 0.953125
276 0.75 0.921875
277 0.609375 0.984375
278 0.6875 1.0
279 0.6875 0.953125
280 0.765625 1.0
281 0.796875 1.0
282 0.734375 0.96875
283 0.671875 0.984375
284 0.578125 1.0
285 0.671875 0.953125
286 0.671875 0.984375
287 0.75 0.9375
288 0.734375 0.984375
289 0.71875 0.96875
290 0.75 0.96875
291 0.6875 0.984375
292 0.65625 0.9375
293 0.734375 0.984375
294 0.65625 0.953125
295 0.703125 0.96875
296 0.6875 0.984375
297 0.71875 0.984375
298 0.734375 0.953125
299 0.71875 0.9375
300 0.734375 0.984375
301 0.78125 0.953125
302 0.75 0.9375
303 0.75 0.953125
304 0.796875 0.96875
305 0.734375 1.0
306 0.71875 0.921875
307 0.796875 0.953125
308 0.84375 0.96875
309 0.75 0.96875
310 0.71875 1.0
311 0.828125 0.96875
312 0.8125 0.984375
313 0.78125 0.9375
314 0.84375 0.96875
315 0.6875 0.984375
316 0.84375 0.953125
317 0.8125 0.984375
318 0.765625 0.9375
319 0.71875 0.9375
320 0.703125 0.9375
321 0.671875 0.96875
322 0.703125 0.921875
323 0.765625 0.96875
324 0.703125 0.953125
325 0.828125 0.96875
326 0.828125 0.96875
327 0.78125 0.984375
328 0.8125 0.953125
329 0.796875 0.984375
330 0.765625 0.9375
331 0.890625 0.90625
332 0.8125 0.90625
333 0.8125 1.0
334 0.78125 0.96875
335 0.875 0.96875
336 0.84375 0.984375
337 0.796875 1.0
338 0.8125 0.984375
339 0.78125 0.9375
340 0.875 0.953125
341 0.8125 0.921875
342 0.796875 0.90625
343 0.796875 0.953125
344 0.8125 0.984375
345 0.890625 0.9375
346 0.828125 0.9375
347 0.78125 0.96875
348 0.90625 0.953125
349 0.828125 0.96875
350 0.796875 0.953125
351 0.734375 0.921875
352 0.796875 0.984375
353 0.765625 0.921875
354 0.8125 0.90625
355 0.765625 0.96875
356 0.84375 0.96875
357 0.875 0.953125
358 0.8125 0.90625
359 0.828125 0.96875
360 0.828125 0.953125
361 0.84375 0.984375
362 0.859375 0.9375
363 0.859375 0.953125
364 0.859375 0.9375
365 0.875 0.96875
366 0.875 0.953125
367 0.8125 0.953125
368 0.90625 0.9375
369 0.875 0.984375
370 0.9375 0.890625
371 0.875 0.9375
372 0.796875 0.921875
373 0.828125 0.921875
374 0.90625 0.921875
375 0.8125 0.9375
376 0.8125 0.90625
377 0.90625 0.921875
378 0.765625 1.0
379 0.78125 0.953125
380 0.78125 0.953125
381 0.875 0.953125
382 0.875 0.9375
383 0.84375 0.890625
384 0.796875 0.9375
385 0.859375 0.96875
386 0.8125 0.96875
387 0.90625 0.953125
388 0.921875 0.9375
389 0.890625 0.953125
390 0.96875 0.921875
391 0.84375 0.875
392 0.875 0.921875
393 0.828125 0.9375
394 0.921875 0.9375
395 0.890625 0.9375
396 0.921875 0.921875
397 0.890625 1.0
398 0.875 0.984375
399 0.921875 0.9375
400 0.828125 0.953125
401 0.828125 0.90625
402 0.9375 0.953125
403 0.8125 0.890625
404 0.953125 0.953125
405 0.9375 0.890625
406 0.90625 0.96875
407 0.890625 0.984375
408 0.875 0.9375
409 0.921875 0.984375
410 0.859375 0.96875
411 0.890625 0.953125
412 0.921875 0.96875
413 0.921875 1.0
414 0.921875 0.96875
415 0.875 0.90625
416 0.96875 0.96875
417 0.890625 0.921875
418 0.90625 0.953125
419 0.875 0.9375
420 0.875 0.921875
421 0.96875 0.953125
422 0.921875 0.96875
423 0.890625 0.875
424 0.84375 0.90625
425 0.921875 0.984375
426 0.953125 0.890625
427 1.0 0.90625
428 0.890625 0.9375
429 0.921875 0.9375
430 0.9375 0.953125
431 0.96875 0.953125
432 0.90625 0.9375
433 0.953125 0.875
434 0.953125 0.890625
435 0.96875 0.90625
436 0.9375 0.953125
437 0.96875 0.953125
438 0.953125 0.890625
439 0.921875 0.953125
440 0.953125 0.90625
441 0.90625 0.984375
442 0.96875 0.96875
443 0.984375 0.9375
444 0.921875 0.9375
445 0.96875 0.90625
446 0.921875 0.96875
447 0.890625 0.875
448 0.953125 0.953125
449 0.984375 0.953125
450 0.921875 0.953125
451 1.0 0.90625
452 0.984375 0.96875
453 1.0 0.90625
454 0.953125 0.953125
455 0.96875 0.9375
456 0.953125 0.90625
457 1.0 0.921875
458 0.984375 0.96875
459 0.984375 0.90625
460 0.984375 0.984375
461 1.0 0.921875
462 0.984375 0.953125
463 0.96875 0.921875
464 1.0 0.921875
465 0.984375 0.859375
466 1.0 0.90625
467 1.0 0.890625
468 1.0 0.90625
469 1.0 0.875
470 1.0 0.84375
471 1.0 0.875
472 0.984375 0.859375
473 1.0 0.859375
474 1.0 0.921875
475 0.984375 0.96875
476 1.0 0.875
477 1.0 0.890625
478 1.0 0.921875
479 1.0 0.890625
480 1.0 0.953125
481 1.0 0.90625
482 1.0 0.890625
483 1.0 0.90625
484 1.0 0.90625
485 1.0 0.890625
486 1.0 0.921875
487 1.0 0.96875
488 1.0 0.9375
489 1.0 0.921875
490 1.0 0.890625
491 1.0 0.890625
492 1.0 0.9375
493 1.0 0.90625
494 0.984375 0.9375
495 1.0 0.875
496 1.0 0.890625
497 1.0 0.9375
498 1.0 0.921875
499 0.984375 0.921875
500 1.0 0.921875
501 1.0 0.875
502 1.0 0.90625
503 1.0 0.953125
504 1.0 0.921875
505 1.0 0.96875
506 1.0 0.953125
507 1.0 0.921875
508 1.0 0.921875
509 1.0 0.890625
510 1.0 0.96875
511 1.0 0.921875
512 1.0 0.921875
513 1.0 0.859375
514 1.0 0.875
515 1.0 0.921875
516 1.0 0.9375
517 1.0 0.890625
518 1.0 0.90625
519 1.0 0.875
520 1.0 0.90625
521 1.0 0.9375
522 1.0 0.921875
523 1.0 0.875
524 1.0 0.875
525 1.0 0.921875
526 1.0 0.953125
527 1.0 0.90625
528 1.0 0.90625
529 1.0 0.90625
530 1.0 0.890625
531 1.0 0.90625
532 1.0 0.921875
533 1.0 0.921875
534 1.0 0.953125
535 1.0 0.875
536 1.0 0.90625
537 1.0 0.859375
538 1.0 0.890625
539 1.0 0.9375
540 1.0 0.9375
541 1.0 0.953125
542 1.0 0.890625
543 1.0 0.90625
544 1.0 0.84375
545 1.0 0.890625
546 1.0 0.890625
547 1.0 0.953125
548 1.0 0.9375
549 1.0 0.9375
550 1.0 0.890625
551 1.0 0.96875
552 1.0 0.921875
553 1.0 0.890625
554 1.0 0.90625
555 1.0 0.96875
556 1.0 0.921875
557 1.0 0.953125
558 1.0 0.890625
559 1.0 0.9375
560 1.0 0.9375
561 1.0 0.90625
562 1.0 0.9375
563 1.0 0.953125
564 1.0 0.875
565 1.0 0.90625
566 1.0 0.859375
567 1.0 0.875
568 1.0 0.921875
569 1.0 0.84375
570 1.0 0.890625
571 1.0 0.890625
572 1.0 0.953125
573 1.0 0.90625
574 1.0 0.921875
575 1.0 0.921875
576 1.0 0.921875
577 1.0 0.859375
578 1.0 0.9375
579 1.0 0.953125
580 1.0 0.921875
581 1.0 0.875
582 1.0 0.875
583 1.0 0.875
584 1.0 0.921875
585 1.0 0.890625
586 1.0 0.890625
587 1.0 0.890625
588 1.0 0.90625
589 1.0 0.875
590 1.0 0.90625
591 1.0 0.859375
592 1.0 0.90625
593 1.0 0.84375
594 1.0 0.859375
595 1.0 0.890625
596 1.0 0.90625
597 1.0 0.875
598 1.0 0.953125
599 1.0 0.875
600 1.0 0.859375
601 1.0 0.9375
602 1.0 0.875
603 1.0 0.859375
604 1.0 0.90625
605 1.0 0.90625
606 1.0 0.78125
607 1.0 0.828125
608 1.0 0.9375
609 1.0 0.921875
610 1.0 0.90625
611 1.0 0.953125
612 1.0 0.890625
613 1.0 0.9375
614 1.0 0.921875
615 1.0 0.796875
616 1.0 0.890625
617 1.0 0.921875
618 1.0 0.90625
619 1.0 0.890625
620 1.0 0.859375
621 1.0 0.921875
622 1.0 0.828125
623 1.0 0.875
624 1.0 0.953125
625 1.0 0.84375
626 1.0 0.9375
627 1.0 0.9375
628 1.0 0.859375
629 1.0 0.9375
630 1.0 0.90625
631 1.0 0.9375
632 1.0 0.921875
633 1.0 0.90625
634 1.0 0.90625
635 1.0 0.890625
636 1.0 0.78125
637 1.0 0.90625
638 1.0 0.890625
639 1.0 0.921875
640 1.0 0.75
641 1.0 0.875
642 1.0 0.875
643 1.0 0.90625
644 1.0 0.953125
645 1.0 0.921875
646 1.0 0.890625
647 1.0 0.859375
648 1.0 0.84375
649 1.0 0.90625
650 1.0 0.84375
651 1.0 0.84375
652 1.0 0.921875
653 1.0 0.90625
654 1.0 0.921875
655 1.0 0.921875
656 1.0 0.921875
657 1.0 0.84375
658 1.0 0.84375
659 1.0 0.90625
660 1.0 0.953125
661 1.0 0.890625
662 1.0 0.875
663 1.0 0.84375
664 1.0 0.84375
665 1.0 0.8125
666 1.0 0.859375
667 1.0 0.90625
668 1.0 0.90625
669 1.0 0.875
670 1.0 0.921875
671 1.0 0.84375
672 1.0 0.953125
673 1.0 0.84375
674 1.0 0.8125
675 1.0 0.84375
676 1.0 0.921875
677 1.0 0.953125
678 1.0 0.84375
679 1.0 0.859375
680 1.0 0.859375
681 1.0 0.90625
682 1.0 0.90625
683 1.0 0.90625
684 1.0 0.828125
685 1.0 0.875
686 1.0 0.9375
687 1.0 0.859375
688 1.0 0.859375
689 1.0 0.84375
690 1.0 0.953125
691 1.0 0.84375
692 1.0 0.859375
693 1.0 0.9375
694 1.0 0.890625
695 1.0 0.859375
696 1.0 0.875
697 1.0 0.875
698 1.0 0.9375
699 1.0 0.890625
700 1.0 0.921875
701 1.0 0.828125
702 1.0 0.84375
703 1.0 0.90625
704 1.0 0.859375
705 1.0 0.90625
706 1.0 0.921875
707 1.0 0.9375
708 1.0 0.84375
709 1.0 0.875
710 1.0 0.90625
711 1.0 0.90625
712 1.0 0.890625
713 1.0 0.921875
714 1.0 0.90625
715 1.0 0.890625
716 1.0 0.8125
717 1.0 0.875
718 1.0 0.890625
719 1.0 0.890625
720 1.0 0.90625
721 1.0 0.875
722 1.0 0.84375
723 1.0 0.9375
724 1.0 0.90625
725 1.0 0.84375
726 1.0 0.9375
727 1.0 0.875
728 1.0 0.8125
729 1.0 0.84375
730 1.0 0.9375
731 1.0 0.84375
732 1.0 0.875
733 1.0 0.921875
734 1.0 0.78125
735 1.0 0.78125
736 1.0 0.859375
737 1.0 0.84375
738 1.0 0.90625
739 1.0 0.859375
740 1.0 0.875
741 1.0 0.859375
742 1.0 0.890625
743 1.0 0.875
744 1.0 0.890625
745 1.0 0.859375
746 1.0 0.8125
747 1.0 0.859375
748 1.0 0.953125
749 1.0 0.84375
750 1.0 0.890625
751 1.0 0.796875
752 1.0 0.890625
753 1.0 0.796875
754 1.0 0.84375
755 1.0 0.890625
756 1.0 0.875
757 1.0 0.890625
758 1.0 0.921875
759 1.0 0.84375
760 1.0 0.875
761 1.0 0.859375
762 1.0 0.953125
763 1.0 0.890625
764 1.0 0.859375
765 1.0 0.84375
766 1.0 0.828125
767 1.0 0.8125
768 1.0 0.84375
769 1.0 0.859375
770 1.0 0.875
771 1.0 0.890625
772 1.0 0.90625
773 1.0 0.90625
774 1.0 0.953125
775 1.0 0.90625
776 1.0 0.890625
777 1.0 0.890625
778 1.0 0.921875
779 1.0 0.90625
780 1.0 0.90625
781 1.0 0.90625
782 1.0 0.796875
783 1.0 0.765625
784 1.0 0.921875
785 1.0 0.875
786 1.0 0.890625
787 1.0 0.90625
788 1.0 0.90625
789 1.0 0.890625
790 1.0 0.859375
791 1.0 0.890625
792 1.0 0.875
793 1.0 0.796875
794 1.0 0.859375
795 1.0 0.828125
796 1.0 0.890625
797 1.0 0.875
798 1.0 0.890625
799 1.0 0.8125
800 1.0 0.875
801 1.0 0.921875
802 1.0 0.90625
803 1.0 0.890625
804 1.0 0.90625
805 1.0 0.890625
806 1.0 0.90625
807 1.0 0.9375
808 1.0 0.90625
809 1.0 0.90625
810 1.0 0.859375
811 1.0 0.859375
812 1.0 0.890625
813 1.0 0.875
814 1.0 0.8125
815 1.0 0.875
816 1.0 0.921875
817 1.0 0.9375
818 1.0 0.890625
819 1.0 0.90625
820 1.0 0.921875
821 1.0 0.875
822 1.0 0.9375
823 1.0 0.90625
824 1.0 0.875
825 1.0 0.84375
826 1.0 0.875
827 1.0 0.8125
828 1.0 0.8125
829 1.0 0.890625
830 1.0 0.78125
831 1.0 0.890625
832 1.0 0.890625
833 1.0 0.84375
834 1.0 0.859375
835 1.0 0.875
836 1.0 0.890625
837 1.0 0.9375
838 1.0 0.953125
839 1.0 0.875
840 1.0 0.890625
841 1.0 0.78125
842 1.0 0.921875
843 1.0 0.875
844 1.0 0.84375
845 1.0 0.859375
846 1.0 0.828125
847 1.0 0.8125
848 1.0 0.921875
849 1.0 0.890625
850 1.0 0.84375
851 1.0 0.96875
852 1.0 0.90625
853 1.0 0.9375
854 1.0 0.875
855 1.0 0.890625
856 1.0 0.859375
857 1.0 0.890625
858 1.0 0.8125
859 1.0 0.875
860 1.0 0.875
861 1.0 0.875
862 1.0 0.859375
863 1.0 0.875
864 1.0 0.9375
865 1.0 0.796875
866 1.0 0.84375
867 1.0 0.90625
868 1.0 0.875
869 1.0 0.890625
870 1.0 0.953125
871 1.0 0.890625
872 1.0 0.90625
873 1.0 0.828125
874 1.0 0.96875
875 1.0 0.90625
876 1.0 0.84375
877 1.0 0.765625
878 1.0 0.96875
879 1.0 0.890625
880 1.0 0.890625
881 1.0 0.84375
882 1.0 0.890625
883 1.0 0.84375
884 1.0 0.859375
885 1.0 0.84375
886 1.0 0.859375
887 1.0 0.875
888 1.0 0.921875
889 1.0 0.90625
890 1.0 0.90625
891 1.0 0.859375
892 1.0 0.859375
893 1.0 0.9375
894 1.0 0.84375
895 1.0 0.859375
896 1.0 0.90625
897 1.0 0.9375
898 1.0 0.84375
899 1.0 0.890625
900 1.0 0.828125
901 1.0 0.90625
902 1.0 0.90625
903 1.0 0.859375
904 1.0 0.875
905 1.0 0.84375
906 1.0 0.875
907 1.0 0.875
908 1.0 0.859375
909 1.0 0.875
910 1.0 0.9375
911 1.0 0.796875
912 1.0 0.90625
913 1.0 0.859375
914 1.0 0.9375
915 1.0 0.875
916 1.0 0.859375
917 1.0 0.890625
918 1.0 0.890625
919 1.0 0.875
920 1.0 0.953125
921 1.0 0.921875
922 1.0 0.921875
923 1.0 0.890625
924 1.0 0.9375
925 1.0 0.859375
926 1.0 0.90625
927 1.0 0.921875
928 1.0 0.859375
929 1.0 0.859375
930 1.0 0.875
931 1.0 0.890625
932 1.0 0.890625
933 1.0 0.859375
934 1.0 0.859375
935 1.0 0.828125
936 1.0 0.96875
937 1.0 0.875
938 1.0 0.75
939 1.0 0.9375
940 1.0 0.828125
941 1.0 0.890625
942 1.0 0.828125
943 1.0 0.859375
944 1.0 0.875
945 1.0 0.921875
946 1.0 0.890625
947 1.0 0.921875
948 1.0 0.9375
949 1.0 0.953125
950 1.0 0.875
951 1.0 0.859375
952 1.0 0.890625
953 1.0 0.84375
954 1.0 0.875
955 1.0 0.875
956 1.0 0.90625
957 1.0 0.84375
958 1.0 0.828125
959 1.0 0.75
960 1.0 0.890625
961 1.0 0.875
962 1.0 0.921875
963 1.0 0.796875
964 1.0 0.875
965 1.0 0.90625
966 1.0 0.921875
967 1.0 0.796875
968 1.0 0.875
969 1.0 0.921875
970 1.0 0.828125
971 1.0 0.9375
972 1.0 0.890625
973 1.0 0.921875
974 1.0 0.875
975 1.0 0.9375
976 1.0 0.90625
977 1.0 0.953125
978 1.0 0.859375
979 1.0 0.828125
980 1.0 0.9375
981 1.0 0.953125
982 1.0 0.90625
983 1.0 0.828125
984 1.0 0.78125
985 1.0 0.90625
986 1.0 0.921875
987 1.0 0.90625
988 1.0 0.921875
989 1.0 0.890625
990 1.0 0.90625
991 1.0 0.890625
992 1.0 0.78125
993 1.0 0.859375
994 1.0 0.765625
995 1.0 0.8125
996 1.0 0.890625
997 1.0 0.890625
998 1.0 0.875
999 1.0 0.84375

"The goal is to train a generator model, not a discriminator model, and that is where the complexity of GANs truly lies."

The Generator Model

We will define a small latent space of five dimensions and use the standard approach in the GAN literature of using a Gaussian distribution for each variable in the latent space. We will generate new inputs by drawing random numbers from a standard Gaussian distribution, i.e. mean of zero and a standard deviation of one.

  • Specs:
    • Single Hidden Layer with 5 nodes
    • ReLU activation Function
    • He weight initialization
    • Output layer will have 2 nodes+ will use linear activation function
# define the generator model unit
def define_generator(latent_dim, n_outputs=2):
	model = Sequential()
	model.add(Dense(15, activation='relu', kernel_initializer='he_uniform', input_dim=latent_dim))
	model.add(Dense(n_outputs, activation='linear'))
	return model
# define the discriminator model
model = define_generator(5)
# summarize the model
model.summary()
Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_5 (Dense)              (None, 15)                90        
_________________________________________________________________
dense_6 (Dense)              (None, 2)                 32        
=================================================================
Total params: 122
Trainable params: 122
Non-trainable params: 0
_________________________________________________________________
# generate points in latent space as input for the generator
def generate_latent_points(latent_dim, n):
	# generate points in the latent space
	x_input = np.random.randn(latent_dim * n)
	# reshape into a batch of inputs for the network
	x_input = x_input.reshape(n, latent_dim)
	return x_input
# use the generator to generate n fake examples and plot the results
def generate_fake_samples(generator, latent_dim, n):
	# generate points in latent space
	x_input = generate_latent_points(latent_dim, n)
	# predict outputs
	X = generator.predict(x_input)
    # create class labels
	y = np.zeros((n, 1))
	return X, y

When the discriminator is good at detecting fake samples, the generator is updated more, and when the discriminator model is relatively poor or confused when detecting fake samples, the generator model is updated less.

# define the combined generator and discriminator model, for updating the generator
def define_gan(generator, discriminator):
	# make weights in the discriminator not trainable
	discriminator.trainable = False
	# connect them
	model = Sequential()
	# add generator
	model.add(generator)
	# add the discriminator
	model.add(discriminator)
	# compile model
	model.compile(loss='binary_crossentropy', optimizer='adam')
	return model
# train the generator and discriminator
def train(g_model, d_model, gan_model, latent_dim, n_epochs=10000, n_batch=128, n_eval=2000):
	# determine half the size of one batch, for updating the discriminator
	half_batch = int(n_batch / 2)
	# manually enumerate epochs
	for i in range(n_epochs):
		# prepare real samples
		x_real, y_real = generate_real_samples(half_batch)
		# prepare fake examples
		x_fake, y_fake = generate_fake_samples(g_model, latent_dim, half_batch)
		# update discriminator
		d_model.train_on_batch(x_real, y_real)
		d_model.train_on_batch(x_fake, y_fake)
		# prepare points in latent space as input for the generator
		x_gan = generate_latent_points(latent_dim, n_batch)
		# create inverted labels for the fake samples
		y_gan = np.ones((n_batch, 1))
		# update the generator via the discriminator's error
		gan_model.train_on_batch(x_gan, y_gan)
		# evaluate the model every n_eval epochs
		if (i+1) % n_eval == 0:
			summarize_performance(i, g_model, d_model, latent_dim)
# evaluate the discriminator and plot real and fake points
def summarize_performance(epoch, generator, discriminator, latent_dim, n=100):
	# prepare real samples
	x_real, y_real = generate_real_samples(n)
	# evaluate discriminator on real examples
	_, acc_real = discriminator.evaluate(x_real, y_real, verbose=0)
	# prepare fake examples
	x_fake, y_fake = generate_fake_samples(generator, latent_dim, n)
	# evaluate discriminator on fake examples
	_, acc_fake = discriminator.evaluate(x_fake, y_fake, verbose=0)
	# summarize discriminator performance
	print(epoch, acc_real, acc_fake)
	# scatter plot real and fake data points
	pyplot.scatter(x_real[:, 0], x_real[:, 1], color='red')
	pyplot.scatter(x_fake[:, 0], x_fake[:, 1], color='blue')
	pyplot.show()
# size of the latent space
latent_dim = 5
# create the discriminator
discriminator = define_discriminator()
# create the generator
generator = define_generator(latent_dim)
# create the gan
gan_model = define_gan(generator, discriminator)
# train model
train(generator, discriminator, gan_model, latent_dim)
1999 0.7400000095367432 0.4399999976158142