Example source: https://github.com/fdavidcl/ruta/blob/master/vignettes/examples/autoencoder_denoising.R


This example demonstrates the use of denoising autoencoders with the Ruta package.

Define a denoising autoencoder with 36-variable encoding.

Load MNIST and normalize

Train

Generate reconstructions from test data

Utility functions for plotting

plot_digit <- function(digit, ...) {
  image(array_reshape(digit, c(28, 28), "F")[, 28:1], xaxt = "n", yaxt = "n", col=gray((255:0)/255), ...)
}

plot_sample <- function(digits_test, digits_dec, sample) {
  sample_size <- length(sample)
  layout(
    matrix(c(1:sample_size, (sample_size + 1):(2 * sample_size)), byrow = F, nrow = 2)
  )

  for (i in sample) {
    par(mar = c(0,0,0,0) + 1)
    plot_digit(digits_test[i, ])
    plot_digit(digits_dec[i, ])
  }
}

Plot reconstructions

Generate noisy test data and plot denoised reconstructions. Notice that values of noisy instances may not restrict themselves to the \([0,1]\) range.