Adding noise

To add noise to the training images, we need to obtain 60,000 × 28 × 28 random numbers between 0 and 1 using uniform distribution using the following code:

# Random numbers from uniform distribution
n <- runif(60000*28*28,0,1) n <- array_reshape(n, c(60000,28,28,1))

# Plot par(mfrow = c(8,8), mar = rep(0, 4)) for (i in 1:64) plot(as.raster(n[i,,,]))

Here, we're reshaping the random numbers that were generated using uniform distribution to match the dimensions of the matrix that we have for the training images. The results are plotted in the form of images that show resulting images containing noise.

The following image shows the images containing noise:

The images depicting noise are added to the images that are stored in trainx. We need to divide this by 2 to keep the resulting trainn values between 0 and 1. We can use the following code to do this:

# Adding noise to handwritten images - train data
trainn <- (trainx + n)/2 par(mfrow = c(8,8), mar = rep(0, 4)) for (i in 1:64) plot(as.raster(trainn[i,,,]))

The first 64 training images, along with their noise, are shown in the following image:

Although noise is added to the original handwritten digits, the digits are still readable. The main objective of using a denoising autoencoder is to train a network that retains the handwritten digits and removes noise from the images.

We will repeat the same steps for the test data using the following code:

# Adding noise to handwritten images - test data
n1 <- runif(10000*28*28,0,1)
n1 <- array_reshape(n1, c(10000,28,28,1))
testn <- (testx +n1)/2

Here, we have added noise to test images and stored them in testn. Now, we can specify the encoder architecture.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset