Initializing generator – the DCGAN update

The Generator class initialization looks very similar to what we saw in Chapter 3, My First GAN in Under 100 Lines. There is, however, one difference: the if statement stuck in the middle:

  1. First, initialize the class, just like we did previously, but take care to add one additional argument for the model type, as follows:
class Generator(object):
def __init__(self, width = 28, height= 28, channels = 1, latent_size=100, model_type = 'simple'):

  1. Add all of the following variables as internal variables for the class to use:
self.W = width
self.H = height
self.C = channels
self.LATENT_SPACE_SIZE = latent_size
self.latent_space = np.random.normal(0,1,
(self.LATENT_SPACE_SIZE,))
  1. For the if statement, we can switch between the simple and DCGAN architecture at runtime. This feature will allow you to turn the DCGAN model on or off depending on what task you're doing, as shown in the following code block:
if model_type=='simple':
self.Generator = self.model()
self.OPTIMIZER = Adam(lr=0.0002, decay=8e-9)
self.Generator.compile(loss='binary_crossentropy',
optimizer=self.OPTIMIZER)
elif model_type=='DCGAN':
self.Generator = self.dc_model()
self.OPTIMIZER = Adam(lr=1e-4, beta_1=0.2)
self.Generator.compile(loss='binary_crossentropy',
optimizer=self.OPTIMIZER,metrics=['accuracy'])

You'll notice that the optimizer for the generator in the DCGAN architecture is slightly different. Note that we also specified a metric for evaluation in the declaration. It's very easy to get divergence when training GANs (when the discriminator loss goes to zero), where you simply get noise as output from the generator. The settings in the preceding files should allow you to get decent generator outputs, but you will need to tune them for improved performance.

  1. Finally, add the following lines as diagnostics:
self.save_model()
self.summary()

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

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