// Concatenate the true and generated samples
imgs_x = np.concatenate((imgs, gen_imgs), axis=0)
// First half are valid and second are fake
valid_y = np.array([1] * half_batch + [0] * half_batch).reshape(-1, 1)
// Labels: First half are the digit classes and second are fake labels.
label_y = np.concatenate((y_train[idx], np.full((half_batch, 1), self.num_classes)), axis=0)
After Change
X_train = np.expand_dims(X_train, axis=3)
y_train = y_train.reshape(-1, 1)
half_batch = int(batch_size / 2)
noise_until = epochs
// Class weights:
// To balance the difference in occurences of digit class labels.
// 50% of labels that the discriminator trains on are "fake".
// Weight = 1 / frequency
cw1 = {0: 1, 1: 1}
cw2 = {i: self.num_classes / half_batch for i in range(self.num_classes)}
cw2[self.num_classes] = 1 / half_batch
for epoch in range(epochs):
// ---------------------
// Train Discriminator
// ---------------------
// Select a random half batch of images
idx = np.random.randint(0, X_train.shape[0], half_batch)
imgs = X_train[idx]
// Sample noise and generate a half batch of new images
noise = np.random.normal(0, 1, (half_batch, 100))
gen_imgs = self.generator.predict(noise)
valid = np.ones((half_batch, 1))
fake = np.zeros((half_batch, 1))
labels = to_categorical(y_train[idx], num_classes=self.num_classes+1)
fake_labels = to_categorical(np.full((half_batch, 1), self.num_classes), num_classes=self.num_classes+1)
// Train the discriminator
d_loss_real = self.discriminator.train_on_batch(imgs, [valid, labels], class_weight=[cw1, cw2])
d_loss_fake = self.discriminator.train_on_batch(gen_imgs, [fake, fake_labels], class_weight=[cw1, cw2])
d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)
// ---------------------