09.05.2023 Views

pdfcoffee

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

noise = np.random.normal(loc=0.5, scale=0.5, size=x_train.shape)

x_train_noisy = x_train + noise

noise = np.random.normal(loc=0.5, scale=0.5, size=x_test.shape)

x_test_noisy = x_test + noise

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

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

Chapter 9

Clearing images using a Denoising

autoencoder

Let us use the Denoising autoencoder to clear the handwritten MNIST digits.

1. We start with importing the required modules:

import numpy as np

import tensorflow as tf

import tensorflow.keras as K

import matplotlib.pyplot as plt

2. Next we define the hyperparameters for our model:

np.random.seed(11)

tf.random.set_seed(11)

batch_size = 256

max_epochs = 50

learning_rate = 1e-3

momentum = 8e-1

hidden_dim = 128

original_dim = 784

3. We read in the MNIST dataset, normalize it, and introduce noise in it:

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

x_train = x_train / 255.

x_test = x_test / 255.

x_train = x_train.astype(np.float32)

x_test = x_test.astype(np.float32)

x_train = np.reshape(x_train, (x_train.shape[0], 784))

x_test = np.reshape(x_test, (x_test.shape[0], 784))

# Generate corrupted MNIST images by adding noise with normal dist

# centered at 0.5 and std=0.5

noise = np.random.normal(loc=0.5, scale=0.5, size=x_train.shape)

[ 357 ]

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

Saved successfully!

Ooh no, something went wrong!