09.05.2023 Views

pdfcoffee

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

[ 195 ]

Chapter 6

Next, we combine the generator and discriminator together to form a GAN. In the

GAN we ensure that the discriminator weights are fixed by setting the trainable

argument to False:

discriminator.trainable = False

ganInput = Input(shape=(randomDim,))

x = generator(ganInput)

ganOutput = discriminator(x)

gan = Model(inputs=ganInput, outputs=ganOutput)

The trick to train the two is that we first train the discriminator separately; we

use binary cross entropy loss for the discriminator. Later we freeze the weights of

the discriminator and train the combined GAN; this results in the training of the

generator. The loss this time is also binary cross entropy:

discriminator.compile(loss='binary_crossentropy', optimizer='adam')

gan.compile(loss='binary_crossentropy', optimizer='adam')

Let us now perform the training. For each epoch we take a sample of random noise

first, feed it to the generator, and the generator produces a fake image. We combine

the generated fake images and the actual training images in a batch with their

specific labels and use them to train the discriminator first on the given batch:

def train(epochs=1, batchSize=128):

batchCount = int(X_train.shape[0] / batchSize)

print ('Epochs:', epochs)

print ('Batch size:', batchSize)

print ('Batches per epoch:', batchCount)

for e in range(1, epochs+1):

print ('-'*15, 'Epoch %d' % e, '-'*15)

for _ in range(batchCount):

# Get a random set of input noise and images

noise = np.random.normal(0, 1, size=[batchSize, randomDim])

imageBatch = X_train[np.random.randint(0, X_train.

shape[0], size=batchSize)]

# Generate fake MNIST images

generatedImages = generator.predict(noise)

# print np.shape(imageBatch), np.shape(generatedImages)

X = np.concatenate([imageBatch, generatedImages])

# Labels for generated and real data

yDis = np.zeros(2*batchSize)

# One-sided label smoothing

yDis[:batchSize] = 0.9

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

Saved successfully!

Ooh no, something went wrong!