09.05.2023 Views

pdfcoffee

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Generative Adversarial Networks

We will use a simple multi-layered perceptron (MLP) and we will feed it an image

as a flat vector of size 784, so we reshape the training data:

X_train = X_train.reshape(60000, 784)

Now we will need to build a generator and discriminator. The purpose of the

generator is to take in a noisy input and generate an image similar to the training

dataset. The size of the noisy input is decided by the variable randomDim; you

can initialize it to any integral value. Conventionally people set it to 100. For our

implementation we tried a value of 10. This input is fed to a Dense layer with

256 neurons with LeakyReLU activation. We next add another Dense layer with

512 hidden neurons, followed by the third hidden layer with 1024 neurons and

finally the output layer with 784 neurons. You can change the number of neurons

in the hidden layers and see how the performance changes; however, the number

of neurons in the output unit has to match the number of pixels in the training

images. The corresponding generator is then:

generator = Sequential()

generator.add(Dense(256, input_dim=randomDim))

generator.add(LeakyReLU(0.2))

generator.add(Dense(512))

generator.add(LeakyReLU(0.2))

generator.add(Dense(1024))

generator.add(LeakyReLU(0.2))

generator.add(Dense(784, activation='tanh'))

Similarly, we build a discriminator. Notice now that the discriminator takes in the

images, either from the training set or images generated by generator, thus its input

size is 784. The output of the discriminator however is a single bit, with 0 signifying

a fake image (generated by generator) and 1 signifying that the image is from the

training dataset:

discriminator = Sequential()

discriminator.add(Dense(1024, input_dim=784) )

discriminator.add(LeakyReLU(0.2))

discriminator.add(Dropout(0.3))

discriminator.add(Dense(512))

discriminator.add(LeakyReLU(0.2))

discriminator.add(Dropout(0.3))

discriminator.add(Dense(256))

discriminator.add(LeakyReLU(0.2))

discriminator.add(Dropout(0.3))

discriminator.add(Dense(1, activation='sigmoid'))

[ 194 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!