pdfcoffee
Chapter 6d_loss = (dA_loss + dB_loss) / 2# Translate images back to original domainreconstr_A = generator_BA(fake_B, training=True)reconstr_B = generator_AB(fake_A, training=True)id_A = generator_BA(imgs_A, training=True)id_B = generator_AB(imgs_B, training=True)gen_loss = tf.math.reduce_sum([1 * tf.math.reduce_mean(mean_squared_error(logits_fake_A,valid)),1 * tf.math.reduce_mean(mean_squared_error(logits_fake_B,valid)),10 * tf.math.reduce_mean(mean_squared_error(reconstr_A,imgs_A)),10 * tf.math.reduce_mean(mean_squared_error(reconstr_B,imgs_B)),0.1 * tf.math.reduce_mean(mean_squared_error(id_A,imgs_A)),0.1 * tf.math.reduce_mean(mean_squared_error(id_B,imgs_B)),])gradients_of_d = d_tape.gradient(d_loss, discriminator_A.trainable_variables + discriminator_B.trainable_variables)discriminator_optimizer.apply_gradients(zip(gradients_of_d,discriminator_A.trainable_variables + discriminator_B.trainable_variables))gradients_of_generator = g.gradient(gen_loss, generator_AB.trainable_variables + generator_BA.trainable_variables)optimizer.apply_gradients(zip(gradients_of_generator, generator_AB.trainable_variables + generator_BA.trainable_variables))return dA_loss, dB_loss, gen_lossWe define checkpoints to save the model weights:checkpoint_dird_A = './training_checkpointsd_A'checkpoint_prefixd_A = os.path.join(checkpoint_dird_A, "ckpt_{epoch}")checkpoint_dird_B = './training_checkpointsd_B'checkpoint_prefixd_B = os.path.join(checkpoint_dird_B, "ckpt_{epoch}")checkpoint_dirg_AB = './training_checkpointsg_AB'[ 225 ]
Generative Adversarial Networkscheckpoint_prefixg_AB = os.path.join(checkpoint_dirg_AB, "ckpt_{epoch}")checkpoint_dirg_BA = './training_checkpointsg_BA'checkpoint_prefixg_BA = os.path.join(checkpoint_dirg_BA, "ckpt_{epoch}")Let us now combine it all and train the network for 50 epochs. Please remember thatin the paper, the test network was trained for 200 epochs, so our results will not bethat good:def train(trainA_, trainB_, epochs):for epoch in range(epochs):start = time.time()for batch_i, (imgs_A, imgs_B) in enumerate(zip(trainA_,trainB_)):dA_loss, dB_loss, g_loss = train_batch(imgs_A, imgs_B)if batch_i % 1000 == 0:test_imgA = next(iter(test_A))test_imgB = next(iter(test_B))print ('Time taken for epoch {} batch index {} is {}seconds\n'.format(epoch, batch_i, time.time()-start))print("discriminator A: ", dA_loss.numpy())print("discriminator B: ", dB_loss.numpy())print("generator: {}\n".format(g_loss))fig, axs = plt.subplots(2, 2, figsize=(6, 3),sharey=True, sharex=True)gen_outputA = generator_AB(test_imgA, training=False)gen_outputB = generator_BA(test_imgB, training=False)axs[0,0].imshow(test_imgA[0]*0.5 + 0.5)axs[0,0].set_title("Generator A Input")axs[0,1].imshow(gen_outputA[0]*0.5 + 0.5)axs[0,1].set_title("Generator A Output")axs[1,0].imshow(test_imgB[0]*0.5 + 0.5)axs[1,0].set_title("Generator B Input")axs[1,1].imshow(gen_outputB[0]*0.5 + 0.5)axs[1,1].set_title("Generator B Output")plt.show()discriminator_A.save_weights(checkpoint_prefixd_A.format(epoch=epoch))discriminator_B.save_weights(checkpoint_prefixd_B.format(epoch=epoch))[ 226 ]
- Page 209 and 210: Advanced Convolutional Neural Netwo
- Page 211 and 212: Advanced Convolutional Neural Netwo
- Page 213 and 214: Advanced Convolutional Neural Netwo
- Page 215 and 216: Advanced Convolutional Neural Netwo
- Page 217 and 218: Advanced Convolutional Neural Netwo
- Page 219 and 220: Advanced Convolutional Neural Netwo
- Page 221 and 222: Advanced Convolutional Neural Netwo
- Page 223 and 224: Advanced Convolutional Neural Netwo
- Page 226 and 227: GenerativeAdversarial NetworksIn th
- Page 228 and 229: [ 193 ]Chapter 6Eventually, we reac
- Page 230 and 231: [ 195 ]Chapter 6Next, we combine th
- Page 232 and 233: Chapter 6And handwritten digits gen
- Page 234 and 235: Chapter 6Figure 1: Visualizing the
- Page 236 and 237: Chapter 6The resultant generator mo
- Page 238 and 239: Chapter 6Figure 4: A summary of res
- Page 240 and 241: Chapter 6def train(self, epochs, ba
- Page 242 and 243: Chapter 6The preceding images were
- Page 244 and 245: Chapter 6Another interesting paper
- Page 246 and 247: Chapter 6To elaborate, let us say t
- Page 248 and 249: Chapter 6Figure 7: The architecture
- Page 250 and 251: Chapter 6Figure 11: Illegible initi
- Page 252 and 253: Chapter 6Bedrooms: Generated bedroo
- Page 254 and 255: Chapter 6The images need to be norm
- Page 256 and 257: Chapter 6initializer = tf.random_no
- Page 258 and 259: Cool, right? Now we can define the
- Page 262 and 263: Chapter 6generator_AB.save_weights(
- Page 264: 6. Ledig, Christian, et al. Photo-R
- Page 267 and 268: Word EmbeddingsDeep learning models
- Page 269 and 270: Word EmbeddingsFor example, "crucia
- Page 271 and 272: Word EmbeddingsAssuming a window si
- Page 273 and 274: Word EmbeddingsGloVeThe Global vect
- Page 275 and 276: Word Embeddingsgensim is an open so
- Page 277 and 278: Word Embeddingsgensim also provides
- Page 279 and 280: Word EmbeddingsSpecifically, we wil
- Page 281 and 282: Word EmbeddingsWe will also convert
- Page 283 and 284: Word EmbeddingsE = np.zeros((vocab_
- Page 285 and 286: Word Embeddingsx = self.embedding(x
- Page 287 and 288: Word EmbeddingsThe change in valida
- Page 289 and 290: Word EmbeddingsThe dataset is a 114
- Page 291 and 292: Word Embeddingsprint("random walks
- Page 293 and 294: Word Embeddingssize=128, # size of
- Page 295 and 296: Word EmbeddingsfastText computes em
- Page 297 and 298: Word EmbeddingsIn the future, once
- Page 299 and 300: Word EmbeddingsA much earlier relat
- Page 301 and 302: Word EmbeddingsOnce you have the fi
- Page 303 and 304: Word EmbeddingsThis will create the
- Page 305 and 306: Word EmbeddingsClassifying with BER
- Page 307 and 308: Word Embeddings2. Each Transformer
- Page 309 and 310: Word EmbeddingsOnce trained, we sav
Chapter 6
d_loss = (dA_loss + dB_loss) / 2
# Translate images back to original domain
reconstr_A = generator_BA(fake_B, training=True)
reconstr_B = generator_AB(fake_A, training=True)
id_A = generator_BA(imgs_A, training=True)
id_B = generator_AB(imgs_B, training=True)
gen_loss = tf.math.reduce_sum([
1 * tf.math.reduce_mean(mean_squared_error(logits_fake_A,
valid)),
1 * tf.math.reduce_mean(mean_squared_error(logits_fake_B,
valid)),
10 * tf.math.reduce_mean(mean_squared_error(reconstr_A,
imgs_A)),
10 * tf.math.reduce_mean(mean_squared_error(reconstr_B,
imgs_B)),
0.1 * tf.math.reduce_mean(mean_squared_error(id_A,
imgs_A)),
0.1 * tf.math.reduce_mean(mean_squared_error(id_B,
imgs_B)),
])
gradients_of_d = d_tape.gradient(d_loss, discriminator_A.
trainable_variables + discriminator_B.trainable_variables)
discriminator_optimizer.apply_gradients(zip(gradients_of_d,
discriminator_A.trainable_variables + discriminator_B.trainable_
variables))
gradients_of_generator = g.gradient(gen_loss, generator_
AB.trainable_variables + generator_BA.trainable_variables)
optimizer.apply_gradients(zip(gradients_of_generator, generator_
AB.trainable_variables + generator_BA.trainable_variables))
return dA_loss, dB_loss, gen_loss
We define checkpoints to save the model weights:
checkpoint_dird_A = './training_checkpointsd_A'
checkpoint_prefixd_A = os.path.join(checkpoint_dird_A, "ckpt_{epoch}")
checkpoint_dird_B = './training_checkpointsd_B'
checkpoint_prefixd_B = os.path.join(checkpoint_dird_B, "ckpt_{epoch}")
checkpoint_dirg_AB = './training_checkpointsg_AB'
[ 225 ]