09.05.2023 Views

pdfcoffee

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9

import matplotlib.pyplot as plt

from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D,

UpSampling2D

2. We specify our hyperparameters. If you look carefully, the list is slightly

different; as compared to earlier autoencoder implementations, instead of

learning rate and momentum, this time we are concerned with filters of the

convolutional layer:

np.random.seed(11)

tf.random.set_seed(11)

batch_size = 128

max_epochs = 50

filters = [32,32,16]

3. In the next step, we read in the data and preprocess it. Again, you may

observe slight variation from the previous code, especially in the way we

are adding noise and then limiting the range in between [0-1]. We are doing

so because in this case, instead of the mean square error loss, we will be

using binary cross entropy loss and the final output of the decoder will

pass through sigmoid activation, restricting it between [0-1]:

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

x_train = x_train / 255.

x_test = x_test / 255.

x_train = np.reshape(x_train, (len(x_train),28, 28, 1))

x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))

noise = 0.5

x_train_noisy = x_train + noise * np.random.normal(loc=0.0,

scale=1.0, size=x_train.shape)

x_test_noisy = x_test + noise * np.random.normal(loc=0.0,

scale=1.0, size=x_test.shape)

x_train_noisy = np.clip(x_train_noisy, 0, 1)

x_test_noisy = np.clip(x_test_noisy, 0, 1)

x_train_noisy = x_train_noisy.astype('float32')

x_test_noisy = x_test_noisy.astype('float32')

#print(x_test_noisy[1].dtype)

[ 361 ]

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

Saved successfully!

Ooh no, something went wrong!