Deep Neural Networking for MNIST Hand Writing Digits Recognition, to Minimize Feature of Metrics

Deep Learning is a powerful tool to solve photo recognition problem.

Background: https://charleshsliao.wordpress.com/2017/02/24/svm-tuning-based-on-mnist/

load_image_file <- function(filename) {
  ret = list()
  f = file(filename,'rb')
  readBin(f,'integer',n=1,size=4,endian='big')
  ret$n = readBin(f,'integer',n=1,size=4,endian='big')
  nrow = readBin(f,'integer',n=1,size=4,endian='big')
  ncol = readBin(f,'integer',n=1,size=4,endian='big')
  x = readBin(f,'integer',n=ret$n*nrow*ncol,size=1,signed=F)
  ret$x = matrix(x, ncol=nrow*ncol, byrow=T)
  close(f)
  ret
}

load_label_file <- function(filename) { 
  f = file(filename,'rb')
  readBin(f,'integer',n=1,size=4,endian='big')
  n = readBin(f,'integer',n=1,size=4,endian='big')
  y = readBin(f,'integer',n=n,size=1,signed=F)
  close(f)
  y
}

#show handwritten digit with show_digit(matriximage$x[n,]),n is any number below 60000.
matriximage<-load_image_file("train-images-idx3-ubyte")
show_digit <- function(arr784, col=gray(12:1/12), ...) {
  image(matrix(arr784, nrow=28)[,28:1], col=col, ...)
}
#Prepare fore prediction visualization
matrixtest<-as.list(load_image_file("t10k-images-idx3-ubyte"))
show_number <- function(arr784, col=gray(12:1/12), ...) {
  image(matrix(arr784, nrow=28)[,28:1], col=col, ...)
}

#save data as dataframe
# though not sure what to do with label data set...now
# convert labels in to categorial value
imagetraining<-as.data.frame(load_image_file("train-images-idx3-ubyte"))
imagetest<-as.data.frame(load_image_file("t10k-images-idx3-ubyte"))
labeltraining<-as.factor(load_label_file("train-labels-idx1-ubyte"))
labeltest<-as.factor(load_label_file("t10k-labels-idx1-ubyte"))

#load necessary packages
library(knitr)  # Markdown
library(ggplot2) # Make it chartful
library(nnet)
library(caret)
library(pROC)
library(doParallel)
# Ensure load data right 
dim(imagetraining)
dim(imagetest)
dim(labeltraining)
dim(labeltest) 
# Check NA
sum(is.na(imagetraining))
sum(is.na(labeltraining)) 
# Check column names; lack of label...
# Labels are stored in label datasets...Seriously who would do things like this!!
sapply(imagetraining[1,],class) 

# Combine label and image integer. Rebuild Training and Test.
imagetraining[,1]<-labeltraining
imagetest[,1]<-labeltest
Training<-imagetraining
Test<-imagetest 

# set number of Obs here.
n<-4000
order<-sample(60000,n)
Training_n<-Training[order,]
Training_nx<-Training_n[,-1]
Training_ny<-Training_n[,1]

#use mlp
#received a fatal error
library(RSNNS)
mnist_mlp<-as.data.frame(rbind(Training_n,Test))
mnist_mlp <- mnist_mlp[sample(1:nrow(mnist_mlp),length(1:nrow(mnist_mlp))),1:ncol(mnist_mlp)]
mnist_y<-decodeClassLabels(mnist_mlp[,1])
mnist_mlp<-splitForTrainingAndTest(mnist_mlp[,-1],mnist_y,ratio=1-n/nrow(mnist_mlp))
mnist_mlp<-normTrainingAndTestSet(mnist_mlp,type="0_1")
cl <- makeCluster(detectCores())
registerDoParallel(cl)
pt<- proc.time()
nn_01<-mlp(mnist_mlp$inputsTrain,mnist_mlp$targetsTrain,size=5,learnFunc = 0.1,
           maxit = 100,inputsTest=mnist_mlp$inputsTest,
           targetsTest=mnist_mlp$targetsTest)
proc.time()-pt
stopCluster(cl)

#use drat and mxnet to build DNN
##########################################################################
###### NOTICE: This approach might need 35GB free harddrive space   ######
###### Otherwise R/Rstudio will collapse; I need a new MAC 😛       ######
##########################################################################
library(drat)
drat:::addRepo("dmlc")
library(mxnet)
train<-data.matrix(Training_n)
test<-data.matrix(Test)
train.x<-train[,-1]
train.y<-train[,1]
train.x<-t(train.x/255)
test_org<-test
test<-t(test[,-1]/255)
data <- mx.symbol.Variable("data")
fc1 <- mx.symbol.FullyConnected(data, name="fc1", num_hidden=7680)
act1 <- mx.symbol.Activation(fc1, name="relu1", act_type="relu")
fc2 <- mx.symbol.FullyConnected(act1, name="fc2", num_hidden=2560)
act2 <- mx.symbol.Activation(fc2, name="relu2", act_type="relu")
fc3 <- mx.symbol.FullyConnected(act2, name="fc3", num_hidden=640)
act3 <- mx.symbol.Activation(fc3, name="relu3", act_type="relu")
fc4 <- mx.symbol.FullyConnected(act3, name="fc4", num_hidden=160)
act4 <- mx.symbol.Activation(fc4, name="relu4", act_type="relu")
fc5 <- mx.symbol.FullyConnected(act4, name="fc5", num_hidden=40)
softmax <- mx.symbol.SoftmaxOutput(fc5, name="sm")
devices <- mx.cpu()
mx.set.seed(2017)
dnn_01 <- mx.model.FeedForward.create(softmax, X=train.x, 
                                     y=train.y,
       ctx=devices, num.round=15, array.batch.size=1000,
       learning.rate=0.07, momentum=0.9,  eval.metric=mx.metric.accuracy,
       initializer=mx.init.uniform(0.07),
       epoch.end.callback=mx.callback.log.train.metric(100))
dnn_01_pr<-predict(dnn_01,test)
dnn_01_pr_y <- max.col(t(dnn_01_pr)) - 1
table(test_org[,1],dnn_01_pr_y)
error_rate_dnn_01<-sum(dnn_01_pr_y!=test_org[,1])/nrow(Test)
paste0("error rate of DNN: ",error_rate_dnn_01)
FOM_dnn_01<-(n/60000)/2+error_rate_dnn_01
paste0("FOM of DNN: ",FOM_dnn_01)
#4000,15,5, 7680, 2560, 640, 160, 40 : 9.8733%

Denoted the percentage number for the training data as P1
Denoted error rate as P2
The final FOM = P1/2 + P2
With these parameters above we have a FOM of 9.8733%

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s