How to do it...

  1. The MNIST dataset is used to demonstrate the concept of sparse decomposition. The MNIST dataset uses handwritten digits. It is downloaded from the tensorflow dataset library. The dataset consists of handwritten images of 28 x 28 pixels. It consists of 55,000 training examples, 10,000 test examples, and 5,000 test examples. The dataset can be downloaded from the tensorflow library using the following script:
library(tensorflow)
datasets <- tf$contrib$learn$datasets
mnist <- datasets$mnist$read_data_sets("MNIST-data", one_hot = TRUE)
  1. For computational simplicity, the MNIST image size is reduced from 28 x 28 pixels to 16 x 16 pixels using the following function:
# Function to reduce image size
reduceImage<-function(actds, n.pixel.x=16, n.pixel.y=16){
actImage<-matrix(actds, ncol=28, byrow=FALSE)
img.col.mat <- imappend(list(as.cimg(actImage)),"c")
thmb <- resize(img.col.mat, n.pixel.x, n.pixel.y)
outputImage<-matrix(thmb[,,1,1], nrow = 1, byrow = F)
return(outputImage)
}
  1. The following script can be used to prepare the MNIST training data with a 16 x 16 pixel image:
# Covert train data to 16 x 16  image
trainData<-t(apply(mnist$train$images, 1, FUN=reduceImage))
validData<-t(apply(mnist$test$images, 1, FUN=reduceImage))
  1. The plot_mnist function can be used to visualize the selected MNIST image:
# Function to plot MNIST dataset
plot_mnist<-function(imageD, pixel.y=16){
actImage<-matrix(imageD, ncol=pixel.y, byrow=FALSE)
img.col.mat <- imappend(list(as.cimg(actImage)), "c")
plot(img.col.mat)
}
..................Content has been hidden....................

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