How to do it...

The GAN class will be straightforward to implement—it's essentially the same class we defined back in Chapter 3My First GAN in Under 100 Lines, and Chapter 4, Dreaming of New Outdoor Structures Using DCGAN.

Here are the steps to create it:

  1.  Define all of the necessary packages to import for the GAN class:
#!/usr/bin/env python3
import sys
import numpy as np
from keras.models import Sequential, Model
from keras.optimizers import Adam
from keras.utils import plot_model
  1. Define the class that takes in the discriminator and generator models as input:
class GAN(object):
def __init__(self,discriminator,generator):

  1. After we initialize the class, define the optimizer and our internal variables for the GAN architecture:
self.OPTIMIZER = Adam(lr=0.008, beta_1=0.5)
self.Generator = generator
self.Discriminator = discriminator
self.Discriminator.trainable = True
  1. We'll use the model method (defined in step 5), compile the model, and print a summary:
self.gan_model = self.model()
self.gan_model.compile(loss='binary_crossentropy', optimizer=self.OPTIMIZER)
self.summary()
  1. Let's create the model definition—the generator feeds into the discriminator:
def model(self):
model = Sequential()
model.add(self.Generator)
model.add(self.Discriminator)
return model
  1. For the final step, print a summary to screen with the summary method:
def summary(self):
return self.gan_model.summary()

Finally, we'll move on to training this model and understanding the output!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.69.199