16.03.2021 Views

Advanced Deep Learning with Keras

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

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

Improved GANs

size=batch_size)

real_images = x_train[rand_indexes]

real_labels = y_train[rand_indexes]

# generate fake images from noise using generator

# generate noise using uniform distribution

noise = np.random.uniform(-1.0,

1.0,

size=[batch_size, latent_size])

# randomly pick one-hot labels

fake_labels = np.eye(num_labels)[np.random.choice(num_labels,

batch_size)]

# generate fake images

fake_images = generator.predict([noise, fake_labels])

# real + fake images = 1 batch of train data

x = np.concatenate((real_images, fake_images))

# real + fake labels = 1 batch of train data labels

labels = np.concatenate((real_labels, fake_labels))

# label real and fake images

# real images label is 1.0

y = np.ones([2 * batch_size, 1])

# fake images label is 0.0

y[batch_size:, :] = 0

# train discriminator network, log the loss and accuracy

# ['loss', 'activation_1_loss', 'label_loss',

# 'activation_1_acc', 'label_acc']

metrics = discriminator.train_on_batch(x, [y, labels])

fmt = "%d: [disc loss: %f, srcloss: %f, lblloss: %f, srcacc:

%f, lblacc: %f]"

log = fmt % (i, metrics[0], metrics[1], metrics[2],

metrics[3], metrics[4])

# train the adversarial network for 1 batch

# 1 batch of fake images with label=1.0 and

# corresponding one-hot label or class

# since the discriminator weights are frozen

# in adversarial network

# only the generator is trained

# generate noise using uniform distribution

noise = np.random.uniform(-1.0,

1.0,

size=[batch_size, latent_size])

# randomly pick one-hot labels

fake_labels = np.eye(num_labels)[np.random.choice(num_labels,

[ 156 ]

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

Saved successfully!

Ooh no, something went wrong!