Building and compiling the networks

In this section, we will build and compile our networks required for the training:

  1. Start by defining the optimizers required for the training, as shown here:
# Define optimizers
dis_optimizer = SGD(lr=dis_learning_rate, momentum=dis_momentum, nesterov=dis_nesterov)
gen_optimizer = SGD(lr=gen_learning_rate, momentum=gen_momentum, nesterov=gen_nesterov)

  1. Next, create an instance of the generator model, and compile the generator model (compiling will initialize the weights parameters, the optimizer algorithm, the loss function, and other essential steps required to use the network):
gen_model = build_generator()
gen_model.compile(loss='binary_crossentropy', optimizer=gen_optimizer)

Use binary_crossentropy as the loss function for the generator networks and gen_optimizer as the optimizer.

  1. Next, create an instance of the discriminator model, and compile it, as shown here:
dis_model = build_discriminator()
dis_model.compile(loss='binary_crossentropy', optimizer=dis_optimizer)

Similarly, use binary_crossentropy as the loss function for the discriminator network and  dis_optimizer as the optimizer.

  1. Next, create an adversarial model. An adversarial contains both networks in a single model. The architecture of the adversarial model will be as follows:
    • input -> generator->discriminator->output

The code to create and compile an adversarial model is as follows:

adversarial_model = Sequential()
adversarial_model.add(gen_model)
dis_model.trainable = False
adversarial_model.add(dis_model)

When we train this network, we don't want to train the discriminator network, so make it non-trainable before we add it to the adversarial model.

Compile the adversarial model, as follows:

adversarial_model.compile(loss='binary_crossentropy', optimizer=gen_optimizer)

Use binary_crossentropy as the loss function and gen_optimizer as the optimizer for the adversarial model.

Before starting the training, add TensorBoard to visualize the losses, as follows:

tensorboard = TensorBoard(log_dir="logs/{}".format(time.time()), write_images=True, write_grads=True, write_graph=True)
tensorboard.set_model(gen_model)
tensorboard.set_model(dis_model)

We will train the network for a specified number of iterations, so create a loop that should run for a specified number of epochs. Inside each epoch, we will train our networks on a mini-batch of a size of 128. Calculate the number of batches that need to be processed:

for epoch in range(epcohs):
print("Epoch is", epoch)
number_of_batches = int(X.shape[0] / batch_size)
print("Number of batches", number_of_batches)
for index in range(number_of_batches):

We will now take a closer look at the training process. The following points explain the different steps involved in the training of DCGAN:

  • Initially, both of the networks are naive and have random weights.
  • The standard process to train a DCGAN network is to first train the discriminator on the batch of samples.
  • To do this, we need fake samples as well as real samples. We already have the real samples, so we now need to generate the fake samples.
  • To generate fake samples, create a latent vector of a shape of (100,) over a uniform distribution. Feed this latent vector to the untrained generator network. The generator network will generate fake samples that we use to train our discriminator network.
  • Concatenate the real images and the fake images to create a new set of sample images. We also need to create an array of labels: label 1 for real images and label 0 for fake images.
..................Content has been hidden....................

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