https://github.com/tdeboissiere/DeepLearningImplementations
使用mnist数据集。
train_GAN.py:
gen_loss = 100
disc_loss = 100
# Start training
print("Start training")
for e in range(nb_epoch):
# 每一回合
# Initialize progbar and batch counter
progbar = generic_utils.Progbar(epoch_size)
batch_counter = 1
start = time.time()
for X_real_batch in data_utils.gen_batch(X_real_train, batch_size):
# 从X_real_train随机抽取一个batch,循环次数,通过下面
# 达到batch_counter >= n_batch_per_epoch,break控
# 制
# Create a batch to feed the discriminator model
X_disc, y_disc = data_utils.get_disc_batch(X_real_batch,
generator_model,
batch_counter,
batch_size,
noise_dim,
noise_scale=noise_scale,
label_smoothing=label_smoothing,
label_flipping=label_flipping)
# get_disc_batch为判别器生成一个batch数据,X_disc,
# y_disc分别为数据和标签
# data_utils.get_disc_batch 与WGAN中不同,这里是
# batch_counter奇偶交替,数据为真实数据和生成的数据交替
# Update the discriminator
disc_loss = discriminator_model.train_on_batch(X_disc, y_disc)
# 判别器更新一次
# Create a batch to feed the generator model
X_gen, y_gen = data_utils.get_gen_batch(batch_size, noise_dim, noise_scale=noise_scale)
# 采样一个batch的噪声给生成器
# Freeze the discriminator
discriminator_model.trainable = False
gen_loss = DCGAN_model.train_on_batch(X_gen, y_gen)
# 更新一次生成器
# Unfreeze the discriminator
discriminator_model.trainable = True
batch_counter += 1
progbar.add(batch_size, values=[("D logloss", disc_loss),
("G logloss", gen_loss)])
# Save images for visualization
if batch_counter % 100 == 0:
data_utils