pdfcoffee
Chapter 10Figure 11: Summary of the VAE modelNow we train the model. We define our loss function, which is the sum of thereconstruction loss and KL divergence loss:dataset = tf.data.Dataset.from_tensor_slices(x_train)dataset = dataset.shuffle(batch_size * 5).batch(batch_size)num_batches = x_train.shape[0] // batch_sizefor epoch in range(num_epochs):for step, x in enumerate(dataset):x = tf.reshape(x, [-1, image_size])with tf.GradientTape() as tape:# Forward passx_reconstruction_logits, mu, log_var = model(x)# Compute reconstruction loss and kl divergence# Scaled by 'image_size' for each individual pixel.reconstruction_loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=x, logits=x_reconstruction_logits)reconstruction_loss = tf.reduce_sum(reconstruction_loss) /batch_sizekl_div = - 0.5 * tf.reduce_sum(1. + log_var -tf.square(mu) - tf.exp(log_var), axis=-1)kl_div = tf.reduce_mean(kl_div)[ 403 ]
Unsupervised Learning# Backprop and optimizeloss = tf.reduce_mean(reconstruction_loss) + kl_divgradients = tape.gradient(loss, model.trainable_variables)for g in gradients:tf.clip_by_norm(g, 15)optimizer.apply_gradients(zip(gradients, model.trainable_variables))if (step + 1) % 50 == 0:print("Epoch[{}/{}], Step [{}/{}], Reconst Loss: {:.4f},KL Div: {:.4f}".format(epoch + 1, num_epochs, step + 1, num_batches,float(reconstruction_loss), float(kl_div)))Once the model is trained it should be able to generate images similar to the originalFashion-MNIST images. To do so we need to use only the decoder network and wewill pass to it a randomly generated z input:z = tf.random.normal((batch_size, latent_dim))out = model.decode(z) # decode with sigmoidout = tf.reshape(out, [-1, 28, 28]).numpy() * 255out = out.astype(np.uint8)In the following figure, you can see the result after 80 epochs; the generated imagesresemble the input space:Figure 12: Results after 80 epochsSummaryThe chapter covered the major unsupervised learning algorithms. We wentthrough algorithms best suited for dimension reduction, clustering, and imagereconstruction. We started with the dimension reduction algorithm PCA, then weperformed clustering using k-means and self-organized maps. After this we studiedthe restricted Boltzmann machine and saw how we can use it for both dimensionreduction and image reconstruction. Next the chapter delved into stacked RBMs,that is, deep belief networks, and we trained a DBN consisting of three RBM layerson the MNIST dataset. Lastly, we learned about variational autoencoders, which, likeGANs, can generate images after learning the distribution of the input sample space.[ 404 ]
- Page 388 and 389: Chapter 9plt.imshow(x_test[index].r
- Page 390 and 391: Chapter 9Keeping the rest of the co
- Page 392 and 393: noise = np.random.normal(loc=0.5, s
- Page 394 and 395: Chapter 9x_train,validation_data=(x
- Page 396 and 397: Chapter 9import matplotlib.pyplot a
- Page 398 and 399: Chapter 9self.conv4 = Conv2D(1, 3,
- Page 400 and 401: Chapter 9You can see that the image
- Page 402 and 403: [ 367 ]Chapter 9Let us use the prec
- Page 404 and 405: Chapter 9Our autoencoder model take
- Page 406 and 407: We train the autoencoder for 20 epo
- Page 408 and 409: Chapter 90.97905576229095460.989323
- Page 410 and 411: Unsupervised LearningThis chapter d
- Page 412 and 413: Chapter 10Next we load the MNIST da
- Page 414 and 415: Chapter 10TensorFlow Embedding APIT
- Page 416 and 417: 3. Recompute the centroids using cu
- Page 418 and 419: Chapter 10Figure 4: Plot of the fin
- Page 420 and 421: Chapter 10In SOMs, neurons are usua
- Page 422 and 423: [ 387 ]Chapter 10Colour mapping usi
- Page 424 and 425: Chapter 10# Calculating Neighbourho
- Page 426 and 427: We will also need to normalize the
- Page 428 and 429: Chapter 10ρρ(vv oo |h oo ) = σσ
- Page 430 and 431: # Generate the sample probabilityde
- Page 432 and 433: Chapter 10And the reconstructed ima
- Page 434 and 435: Chapter 10inpX = rbm.rbm_output(inp
- Page 436 and 437: Chapter 10(60000, 28, 28) (60000,)(
- Page 440 and 441: Chapter 10This chapter, along with
- Page 442 and 443: Reinforcement LearningThis chapter
- Page 444 and 445: Chapter 11And unlike unsupervised l
- Page 446 and 447: Chapter 11Normally, the value is de
- Page 448 and 449: Chapter 11• The next question tha
- Page 450 and 451: Chapter 11This neural network takes
- Page 452 and 453: Chapter 11The MuJoCo environment re
- Page 454 and 455: Chapter 11We will first import the
- Page 456 and 457: Chapter 11The αα is the learning
- Page 458 and 459: Chapter 11We set up the global valu
- Page 460 and 461: Chapter 11else:return np.argmax(sel
- Page 462 and 463: Chapter 11DQN to play a game of Ata
- Page 464 and 465: Chapter 11self.model.add( Conv2D(64
- Page 466 and 467: Chapter 11Here the action A was sel
- Page 468 and 469: Chapter 11Image source: https://arx
- Page 470 and 471: Chapter 11A neural network is used
- Page 472: Chapter 1111. Details regarding ins
- Page 475 and 476: TensorFlow and Cloud• Scalability
- Page 477 and 478: TensorFlow and Cloud• Azure DevOp
- Page 479 and 480: TensorFlow and Cloud• Lambda: The
- Page 481 and 482: TensorFlow and Cloud• Deep Learni
- Page 483 and 484: TensorFlow and CloudEC2 on AmazonTo
- Page 485 and 486: TensorFlow and CloudCompute Instanc
- Page 487 and 488: TensorFlow and CloudYou just share
Unsupervised Learning
# Backprop and optimize
loss = tf.reduce_mean(reconstruction_loss) + kl_div
gradients = tape.gradient(loss, model.trainable_variables)
for g in gradients:
tf.clip_by_norm(g, 15)
optimizer.apply_gradients(zip(gradients, model.trainable_
variables))
if (step + 1) % 50 == 0:
print("Epoch[{}/{}], Step [{}/{}], Reconst Loss: {:.4f},
KL Div: {:.4f}"
.format(epoch + 1, num_epochs, step + 1, num_batches,
float(reconstruction_loss), float(kl_div)))
Once the model is trained it should be able to generate images similar to the original
Fashion-MNIST images. To do so we need to use only the decoder network and we
will pass to it a randomly generated z input:
z = tf.random.normal((batch_size, latent_dim))
out = model.decode(z) # decode with sigmoid
out = tf.reshape(out, [-1, 28, 28]).numpy() * 255
out = out.astype(np.uint8)
In the following figure, you can see the result after 80 epochs; the generated images
resemble the input space:
Figure 12: Results after 80 epochs
Summary
The chapter covered the major unsupervised learning algorithms. We went
through algorithms best suited for dimension reduction, clustering, and image
reconstruction. We started with the dimension reduction algorithm PCA, then we
performed clustering using k-means and self-organized maps. After this we studied
the restricted Boltzmann machine and saw how we can use it for both dimension
reduction and image reconstruction. Next the chapter delved into stacked RBMs,
that is, deep belief networks, and we trained a DBN consisting of three RBM layers
on the MNIST dataset. Lastly, we learned about variational autoencoders, which, like
GANs, can generate images after learning the distribution of the input sample space.
[ 404 ]