A H2O FNN Model for MNIST

Please read this first:
https://charleshsliao.wordpress.com/2017/04/14/identify-arguments-of-h2o-deep-learning-model-with-tuned-auto-encoder-in-r-with-mnist/

Following the auto encoder results of arguments in last article and a sample FNN model at the end of that article, we can build a full FNN model for MNIST.

library(jsonlite)
library(caret)
library(h2o)
library(ggplot2)
library(data.table)

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
}
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"))
imagetraining[,1]<-labeltraining
imagetest[,1]<-labeltest
Training<-imagetraining
Test<-imagetest 
sample_n<-3000
order<-sample(60000,sample_n)
training<-Training[order,]
validating<-Training[-order,]
test_x<-Test[,-1]

cl<-h2o.init(max_mem_size = "20G",nthreads = 10)
h2odigits<-as.h2o(training, destination_frame = "h2odigits")
h2odigits_v<-as.h2o(validating,destination_frame = "h2odigits_v")
h2odigits_t<-as.h2o(Test, destination_frame = "h2odigits_t")
h2odigits_train_x<-h2odigits[,-1]
h2odigits_test_x<-h2odigits_t[,-1]
xnames<-colnames(h2odigits_train_x)

system.time(hd_fnn_01<-h2o.deeplearning(
  x=xnames,y="n",training_frame = h2odigits,validation_frame = h2odigits_v,
  activation = "RectifierWithDropout",hidden=c(400,100),epochs=400,
  loss="CrossEntropy",adaptive_rate = F,
  rate=0.0001,input_dropout_ratio = c(0.2),hidden_dropout_ratios = c(0.5,0.25)
))

hd_fnn_01_results<-h2o.predict(hd_fnn_01,h2odigits_t)
predict_results<-as.data.frame(hd_fnn_01_results[,1])
caret::confusionMatrix(unlist(predict_results),Test$n)$overall
#     Accuracy          Kappa  AccuracyLower  AccuracyUpper   AccuracyNull AccuracyPValue 
#    0.9261000      0.9178560      0.9207974      0.9311528      0.1135000      0.0000000 
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