# Chapter 6: Project: Handwritten Digit Generation with VAEs

## 6.6 Example of Full Code for the Project

In the previous sections, we have walked through the individual steps required to generate handwritten digits using Variational Autoencoders (VAEs). Now, let's bring all these steps together into a complete, runnable code snippet. This complete script includes all the parts from data collection and preprocessing, model creation and training, to generating new handwritten digits and model evaluation.

Remember, this code is intended to provide a practical hands-on experience to understand how VAEs work. It's vital to note that training deep learning models is a complex process, and various factors such as the architecture of the model, the quality and size of the dataset, the selection of loss function, and hyperparameters can significantly influence the performance and effectiveness of the model.

Feel free to modify and experiment with this code to understand the different components and how they affect the output better. This process of learning and experimenting is a crucial part of mastering any machine learning technique.

`import numpy as np`

import tensorflow as tf

from tensorflow.keras.datasets import mnist

from tensorflow.keras.layers import Input, Dense, Lambda

from tensorflow.keras.models import Model

from tensorflow.keras import backend as K

from tensorflow.keras.losses import binary_crossentropy

import matplotlib.pyplot as plt

# Load MNIST data and preprocess it

(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255.

x_test = x_test.astype('float32') / 255.

x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))

x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))

# Define the model parameters

original_dim = x_train.shape[1]

intermediate_dim = 512

latent_dim = 2

batch_size = 100

epochs = 50

# Define sampling with reparameterization trick

def sampling(args):

z_mean, z_log_var = args

epsilon = K.random_normal(shape=(K.shape(z_mean)[0], latent_dim), mean=0., stddev=1.)

return z_mean + K.exp(z_log_var / 2) * epsilon

# Define encoder part

x = Input(shape=(original_dim,))

h = Dense(intermediate_dim, activation='relu')(x)

z_mean = Dense(latent_dim)(h)

z_log_var = Dense(latent_dim)(h)

z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])

# Define decoder part

decoder_h = Dense(intermediate_dim, activation='relu')

decoder_mean = Dense(original_dim, activation='sigmoid')

h_decoded = decoder_h(z)

x_decoded_mean = decoder_mean(h_decoded)

# end-to-end VAE model

vae = Model(x, x_decoded_mean)

# encoder model, to encode input into latent variable

encoder = Model(x, z_mean)

# decoder model, start from latent space

decoder_input = Input(shape=(latent_dim,))

_h_decoded = decoder_h(decoder_input)

_x_decoded_mean = decoder_mean(_h_decoded)

generator = Model(decoder_input, _x_decoded_mean)

# Define the VAE loss

def vae_loss(x, x_decoded_mean):

xent_loss = binary_crossentropy(x, x_decoded_mean)

kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)

return K.mean(xent_loss + kl_loss)

vae.compile(optimizer='adam', loss=vae_loss)

# Train the VAE

vae.fit(x_train, x_train, shuffle=True, epochs=epochs, batch_size=batch_size, validation_data=(x_test, x_test))

# Generate new digits

n = 15 # figure with 15x15 digits

digit_size = 28

figure = np.zeros((digit_size * n, digit_size * n))

# linearly spaced coordinates corresponding to the 2D plot

# of digit classes in the latent space

grid_x = np.linspace(-4, 4, n)

grid_y = np.linspace(-4, 4, n)

for i, yi in enumerate(grid_x):

for j, xi in enumerate(grid_y):

z_sample = np.array([[xi, yi]])

x_decoded = generator.predict(z_sample)

digit = x_decoded[0].reshape(digit_size, digit_size)

figure[i * digit_size: (i + 1) * digit_size,

j * digit_size: (j + 1) * digit_size] = digit

plt.figure(figsize=(10, 10))

plt.imshow(figure, cmap='Greys_r')

plt.show()

This last block of code generates a grid of 15x15 new handwritten digits by sampling points from the latent space, decoding each point into a digit image, and displaying the digit images in a grid.

Please note that you'll need to import

as **matplotlib.pyplot**

at the beginning of your code to display the figure:**plt**

`import matplotlib.pyplot as plt`

Also, keep in mind that the latent space we have defined is 2D for simplicity and for easy visualization. A larger-dimensional latent space may be required for more complex datasets.

Remember that you can tune the hyperparameters like the dimensionality of the latent space, the size of the intermediate layer, the batch size, and the number of epochs to suit your specific needs and compute resources.

Finally, as always, please ensure that you have the necessary permissions and resources to download and process the dataset, and to train and deploy the model.

**Chapter 6 Conclusion**

And there we have it - the conclusion of our hands-on chapter on a project involving Variational Autoencoders (VAEs)! By now, you should have a practical understanding of how VAEs work, how to design and implement them, and most importantly, how to apply them in a real-world setting, as we've done with the digit generation project.

In this chapter, we started from the ground up, by gathering and preprocessing our data - a critical first step in any machine learning project. We then proceeded to construct our VAE model, which consisted of both an encoder and a decoder. The model was trained using our dataset, enabling it to learn the intricate details necessary to generate new, realistic handwritten digits.

Throughout this process, we covered several critical aspects related to the successful implementation of VAEs, such as loss function details, model evaluation, and even advanced considerations. We've also walked through the process of generating new samples using the trained model - a task that has important implications across a wide range of fields, from design to entertainment to security.

Lastly, we provided the entire project's code, serving as a functional template that you can utilize and adapt for your VAE projects. Remember, this code, while operational, might require slight adjustments depending on your specific dataset or computational resources.

This chapter's project illuminated the power and potential of VAEs and deep learning as a whole. With this knowledge, you are now equipped to tackle your projects, whether it's creating new content, making predictions, or exploring the unknown through the power of generative models.

As we move forward, it's our hope that you'll feel confident in not only your understanding of VAEs and their inner workings but also in your ability to apply this knowledge practically. This chapter was not the end of our journey with generative models, but rather a launching point into a wide world of possibilities. Keep exploring, keep questioning, and most importantly, keep generating!

## 6.6 Example of Full Code for the Project

In the previous sections, we have walked through the individual steps required to generate handwritten digits using Variational Autoencoders (VAEs). Now, let's bring all these steps together into a complete, runnable code snippet. This complete script includes all the parts from data collection and preprocessing, model creation and training, to generating new handwritten digits and model evaluation.

Remember, this code is intended to provide a practical hands-on experience to understand how VAEs work. It's vital to note that training deep learning models is a complex process, and various factors such as the architecture of the model, the quality and size of the dataset, the selection of loss function, and hyperparameters can significantly influence the performance and effectiveness of the model.

Feel free to modify and experiment with this code to understand the different components and how they affect the output better. This process of learning and experimenting is a crucial part of mastering any machine learning technique.

`import numpy as np`

import tensorflow as tf

from tensorflow.keras.datasets import mnist

from tensorflow.keras.layers import Input, Dense, Lambda

from tensorflow.keras.models import Model

from tensorflow.keras import backend as K

from tensorflow.keras.losses import binary_crossentropy

import matplotlib.pyplot as plt

# Load MNIST data and preprocess it

(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255.

x_test = x_test.astype('float32') / 255.

x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))

x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))

# Define the model parameters

original_dim = x_train.shape[1]

intermediate_dim = 512

latent_dim = 2

batch_size = 100

epochs = 50

# Define sampling with reparameterization trick

def sampling(args):

z_mean, z_log_var = args

epsilon = K.random_normal(shape=(K.shape(z_mean)[0], latent_dim), mean=0., stddev=1.)

return z_mean + K.exp(z_log_var / 2) * epsilon

# Define encoder part

x = Input(shape=(original_dim,))

h = Dense(intermediate_dim, activation='relu')(x)

z_mean = Dense(latent_dim)(h)

z_log_var = Dense(latent_dim)(h)

z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])

# Define decoder part

decoder_h = Dense(intermediate_dim, activation='relu')

decoder_mean = Dense(original_dim, activation='sigmoid')

h_decoded = decoder_h(z)

x_decoded_mean = decoder_mean(h_decoded)

# end-to-end VAE model

vae = Model(x, x_decoded_mean)

# encoder model, to encode input into latent variable

encoder = Model(x, z_mean)

# decoder model, start from latent space

decoder_input = Input(shape=(latent_dim,))

_h_decoded = decoder_h(decoder_input)

_x_decoded_mean = decoder_mean(_h_decoded)

generator = Model(decoder_input, _x_decoded_mean)

# Define the VAE loss

def vae_loss(x, x_decoded_mean):

xent_loss = binary_crossentropy(x, x_decoded_mean)

kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)

return K.mean(xent_loss + kl_loss)

vae.compile(optimizer='adam', loss=vae_loss)

# Train the VAE

vae.fit(x_train, x_train, shuffle=True, epochs=epochs, batch_size=batch_size, validation_data=(x_test, x_test))

# Generate new digits

n = 15 # figure with 15x15 digits

digit_size = 28

figure = np.zeros((digit_size * n, digit_size * n))

# linearly spaced coordinates corresponding to the 2D plot

# of digit classes in the latent space

grid_x = np.linspace(-4, 4, n)

grid_y = np.linspace(-4, 4, n)

for i, yi in enumerate(grid_x):

for j, xi in enumerate(grid_y):

z_sample = np.array([[xi, yi]])

x_decoded = generator.predict(z_sample)

digit = x_decoded[0].reshape(digit_size, digit_size)

figure[i * digit_size: (i + 1) * digit_size,

j * digit_size: (j + 1) * digit_size] = digit

plt.figure(figsize=(10, 10))

plt.imshow(figure, cmap='Greys_r')

plt.show()

This last block of code generates a grid of 15x15 new handwritten digits by sampling points from the latent space, decoding each point into a digit image, and displaying the digit images in a grid.

Please note that you'll need to import

as **matplotlib.pyplot**

at the beginning of your code to display the figure:**plt**

`import matplotlib.pyplot as plt`

Also, keep in mind that the latent space we have defined is 2D for simplicity and for easy visualization. A larger-dimensional latent space may be required for more complex datasets.

Remember that you can tune the hyperparameters like the dimensionality of the latent space, the size of the intermediate layer, the batch size, and the number of epochs to suit your specific needs and compute resources.

Finally, as always, please ensure that you have the necessary permissions and resources to download and process the dataset, and to train and deploy the model.

**Chapter 6 Conclusion**

And there we have it - the conclusion of our hands-on chapter on a project involving Variational Autoencoders (VAEs)! By now, you should have a practical understanding of how VAEs work, how to design and implement them, and most importantly, how to apply them in a real-world setting, as we've done with the digit generation project.

In this chapter, we started from the ground up, by gathering and preprocessing our data - a critical first step in any machine learning project. We then proceeded to construct our VAE model, which consisted of both an encoder and a decoder. The model was trained using our dataset, enabling it to learn the intricate details necessary to generate new, realistic handwritten digits.

Throughout this process, we covered several critical aspects related to the successful implementation of VAEs, such as loss function details, model evaluation, and even advanced considerations. We've also walked through the process of generating new samples using the trained model - a task that has important implications across a wide range of fields, from design to entertainment to security.

Lastly, we provided the entire project's code, serving as a functional template that you can utilize and adapt for your VAE projects. Remember, this code, while operational, might require slight adjustments depending on your specific dataset or computational resources.

This chapter's project illuminated the power and potential of VAEs and deep learning as a whole. With this knowledge, you are now equipped to tackle your projects, whether it's creating new content, making predictions, or exploring the unknown through the power of generative models.

As we move forward, it's our hope that you'll feel confident in not only your understanding of VAEs and their inner workings but also in your ability to apply this knowledge practically. This chapter was not the end of our journey with generative models, but rather a launching point into a wide world of possibilities. Keep exploring, keep questioning, and most importantly, keep generating!

## 6.6 Example of Full Code for the Project

In the previous sections, we have walked through the individual steps required to generate handwritten digits using Variational Autoencoders (VAEs). Now, let's bring all these steps together into a complete, runnable code snippet. This complete script includes all the parts from data collection and preprocessing, model creation and training, to generating new handwritten digits and model evaluation.

Remember, this code is intended to provide a practical hands-on experience to understand how VAEs work. It's vital to note that training deep learning models is a complex process, and various factors such as the architecture of the model, the quality and size of the dataset, the selection of loss function, and hyperparameters can significantly influence the performance and effectiveness of the model.

Feel free to modify and experiment with this code to understand the different components and how they affect the output better. This process of learning and experimenting is a crucial part of mastering any machine learning technique.

`import numpy as np`

import tensorflow as tf

from tensorflow.keras.datasets import mnist

from tensorflow.keras.layers import Input, Dense, Lambda

from tensorflow.keras.models import Model

from tensorflow.keras import backend as K

from tensorflow.keras.losses import binary_crossentropy

import matplotlib.pyplot as plt

# Load MNIST data and preprocess it

(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255.

x_test = x_test.astype('float32') / 255.

x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))

x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))

# Define the model parameters

original_dim = x_train.shape[1]

intermediate_dim = 512

latent_dim = 2

batch_size = 100

epochs = 50

# Define sampling with reparameterization trick

def sampling(args):

z_mean, z_log_var = args

epsilon = K.random_normal(shape=(K.shape(z_mean)[0], latent_dim), mean=0., stddev=1.)

return z_mean + K.exp(z_log_var / 2) * epsilon

# Define encoder part

x = Input(shape=(original_dim,))

h = Dense(intermediate_dim, activation='relu')(x)

z_mean = Dense(latent_dim)(h)

z_log_var = Dense(latent_dim)(h)

z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])

# Define decoder part

decoder_h = Dense(intermediate_dim, activation='relu')

decoder_mean = Dense(original_dim, activation='sigmoid')

h_decoded = decoder_h(z)

x_decoded_mean = decoder_mean(h_decoded)

# end-to-end VAE model

vae = Model(x, x_decoded_mean)

# encoder model, to encode input into latent variable

encoder = Model(x, z_mean)

# decoder model, start from latent space

decoder_input = Input(shape=(latent_dim,))

_h_decoded = decoder_h(decoder_input)

_x_decoded_mean = decoder_mean(_h_decoded)

generator = Model(decoder_input, _x_decoded_mean)

# Define the VAE loss

def vae_loss(x, x_decoded_mean):

xent_loss = binary_crossentropy(x, x_decoded_mean)

kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)

return K.mean(xent_loss + kl_loss)

vae.compile(optimizer='adam', loss=vae_loss)

# Train the VAE

vae.fit(x_train, x_train, shuffle=True, epochs=epochs, batch_size=batch_size, validation_data=(x_test, x_test))

# Generate new digits

n = 15 # figure with 15x15 digits

digit_size = 28

figure = np.zeros((digit_size * n, digit_size * n))

# linearly spaced coordinates corresponding to the 2D plot

# of digit classes in the latent space

grid_x = np.linspace(-4, 4, n)

grid_y = np.linspace(-4, 4, n)

for i, yi in enumerate(grid_x):

for j, xi in enumerate(grid_y):

z_sample = np.array([[xi, yi]])

x_decoded = generator.predict(z_sample)

digit = x_decoded[0].reshape(digit_size, digit_size)

figure[i * digit_size: (i + 1) * digit_size,

j * digit_size: (j + 1) * digit_size] = digit

plt.figure(figsize=(10, 10))

plt.imshow(figure, cmap='Greys_r')

plt.show()

This last block of code generates a grid of 15x15 new handwritten digits by sampling points from the latent space, decoding each point into a digit image, and displaying the digit images in a grid.

Please note that you'll need to import

as **matplotlib.pyplot**

at the beginning of your code to display the figure:**plt**

`import matplotlib.pyplot as plt`

Also, keep in mind that the latent space we have defined is 2D for simplicity and for easy visualization. A larger-dimensional latent space may be required for more complex datasets.

Remember that you can tune the hyperparameters like the dimensionality of the latent space, the size of the intermediate layer, the batch size, and the number of epochs to suit your specific needs and compute resources.

Finally, as always, please ensure that you have the necessary permissions and resources to download and process the dataset, and to train and deploy the model.

**Chapter 6 Conclusion**

And there we have it - the conclusion of our hands-on chapter on a project involving Variational Autoencoders (VAEs)! By now, you should have a practical understanding of how VAEs work, how to design and implement them, and most importantly, how to apply them in a real-world setting, as we've done with the digit generation project.

In this chapter, we started from the ground up, by gathering and preprocessing our data - a critical first step in any machine learning project. We then proceeded to construct our VAE model, which consisted of both an encoder and a decoder. The model was trained using our dataset, enabling it to learn the intricate details necessary to generate new, realistic handwritten digits.

Throughout this process, we covered several critical aspects related to the successful implementation of VAEs, such as loss function details, model evaluation, and even advanced considerations. We've also walked through the process of generating new samples using the trained model - a task that has important implications across a wide range of fields, from design to entertainment to security.

Lastly, we provided the entire project's code, serving as a functional template that you can utilize and adapt for your VAE projects. Remember, this code, while operational, might require slight adjustments depending on your specific dataset or computational resources.

This chapter's project illuminated the power and potential of VAEs and deep learning as a whole. With this knowledge, you are now equipped to tackle your projects, whether it's creating new content, making predictions, or exploring the unknown through the power of generative models.

As we move forward, it's our hope that you'll feel confident in not only your understanding of VAEs and their inner workings but also in your ability to apply this knowledge practically. This chapter was not the end of our journey with generative models, but rather a launching point into a wide world of possibilities. Keep exploring, keep questioning, and most importantly, keep generating!

## 6.6 Example of Full Code for the Project

`import numpy as np`

import tensorflow as tf

from tensorflow.keras.datasets import mnist

from tensorflow.keras.layers import Input, Dense, Lambda

from tensorflow.keras.models import Model

from tensorflow.keras import backend as K

from tensorflow.keras.losses import binary_crossentropy

import matplotlib.pyplot as plt

# Load MNIST data and preprocess it

(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255.

x_test = x_test.astype('float32') / 255.

x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))

x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))

# Define the model parameters

original_dim = x_train.shape[1]

intermediate_dim = 512

latent_dim = 2

batch_size = 100

epochs = 50

# Define sampling with reparameterization trick

def sampling(args):

z_mean, z_log_var = args

epsilon = K.random_normal(shape=(K.shape(z_mean)[0], latent_dim), mean=0., stddev=1.)

return z_mean + K.exp(z_log_var / 2) * epsilon

# Define encoder part

x = Input(shape=(original_dim,))

h = Dense(intermediate_dim, activation='relu')(x)

z_mean = Dense(latent_dim)(h)

z_log_var = Dense(latent_dim)(h)

z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])

# Define decoder part

decoder_h = Dense(intermediate_dim, activation='relu')

decoder_mean = Dense(original_dim, activation='sigmoid')

h_decoded = decoder_h(z)

x_decoded_mean = decoder_mean(h_decoded)

# end-to-end VAE model

vae = Model(x, x_decoded_mean)

# encoder model, to encode input into latent variable

encoder = Model(x, z_mean)

# decoder model, start from latent space

decoder_input = Input(shape=(latent_dim,))

_h_decoded = decoder_h(decoder_input)

_x_decoded_mean = decoder_mean(_h_decoded)

generator = Model(decoder_input, _x_decoded_mean)

# Define the VAE loss

def vae_loss(x, x_decoded_mean):

xent_loss = binary_crossentropy(x, x_decoded_mean)

kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)

return K.mean(xent_loss + kl_loss)

vae.compile(optimizer='adam', loss=vae_loss)

# Train the VAE

vae.fit(x_train, x_train, shuffle=True, epochs=epochs, batch_size=batch_size, validation_data=(x_test, x_test))

# Generate new digits

n = 15 # figure with 15x15 digits

digit_size = 28

figure = np.zeros((digit_size * n, digit_size * n))

# linearly spaced coordinates corresponding to the 2D plot

# of digit classes in the latent space

grid_x = np.linspace(-4, 4, n)

grid_y = np.linspace(-4, 4, n)

for i, yi in enumerate(grid_x):

for j, xi in enumerate(grid_y):

z_sample = np.array([[xi, yi]])

x_decoded = generator.predict(z_sample)

digit = x_decoded[0].reshape(digit_size, digit_size)

figure[i * digit_size: (i + 1) * digit_size,

j * digit_size: (j + 1) * digit_size] = digit

plt.figure(figsize=(10, 10))

plt.imshow(figure, cmap='Greys_r')

plt.show()

as **matplotlib.pyplot**

at the beginning of your code to display the figure:**plt**

`import matplotlib.pyplot as plt`