pdfcoffee

soumyasankar99
from soumyasankar99 More from this publisher
09.05.2023 Views

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 ]

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 ]

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

Saved successfully!

Ooh no, something went wrong!