#Here is the script from TJO  http://tjo-en.hatenablog.com/entry/2015/02/25/125417 with h2o 

train <- data.matrix(read.csv("../input/train.csv", header=T))
test <- data.matrix(read.csv("../input/test.csv", header=T))

labels<-dat[,1]
test_idx<-c()
for (i in 1:10) {
	tmp1<-which(labels==(i-1))
 	tmp2<-sample(tmp1,1000,replace=F)
	test_idx<-c(test_idx,tmp2)
 }
test<-dat[test_idx,]
train<-dat[-test_idx,]
write.table(train,file="prac_train.csv",quote=F,col.names=T,row.names=F,sep=",")
write.table(test,file="prac_test.csv",quote=F,col.names=T,row.names=F,sep=",")

test<-read.delim("prac_test.csv",sep=',')
id0<-which(test$label==0)[1]
id1<-which(test$label==1)[1]
id2<-which(test$label==2)[1]
id3<-which(test$label==3)[1]
id4<-which(test$label==4)[1]
id5<-which(test$label==5)[1]
id6<-which(test$label==6)[1]
id7<-which(test$label==7)[1]
id8<-which(test$label==8)[1]
id9<-which(test$label==9)[1]
par(mfrow=c(2,5))
image(t(apply(matrix(as.vector(as.matrix(test[id0,-1])),ncol=28,nrow=28,byrow=T),2,rev)),col=grey(seq(1,0,length.out=256)))
image(t(apply(matrix(as.vector(as.matrix(test[id1,-1])),ncol=28,nrow=28,byrow=T),2,rev)),col=grey(seq(1,0,length.out=256)))
image(t(apply(matrix(as.vector(as.matrix(test[id2,-1])),ncol=28,nrow=28,byrow=T),2,rev)),col=grey(seq(1,0,length.out=256)))
image(t(apply(matrix(as.vector(as.matrix(test[id3,-1])),ncol=28,nrow=28,byrow=T),2,rev)),col=grey(seq(1,0,length.out=256)))
image(t(apply(matrix(as.vector(as.matrix(test[id4,-1])),ncol=28,nrow=28,byrow=T),2,rev)),col=grey(seq(1,0,length.out=256)))
image(t(apply(matrix(as.vector(as.matrix(test[id5,-1])),ncol=28,nrow=28,byrow=T),2,rev)),col=grey(seq(1,0,length.out=256)))
image(t(apply(matrix(as.vector(as.matrix(test[id6,-1])),ncol=28,nrow=28,byrow=T),2,rev)),col=grey(seq(1,0,length.out=256)))
image(t(apply(matrix(as.vector(as.matrix(test[id7,-1])),ncol=28,nrow=28,byrow=T),2,rev)),col=grey(seq(1,0,length.out=256)))
image(t(apply(matrix(as.vector(as.matrix(test[id8,-1])),ncol=28,nrow=28,byrow=T),2,rev)),col=grey(seq(1,0,length.out=256)))
image(t(apply(matrix(as.vector(as.matrix(test[id9,-1])),ncol=28,nrow=28,byrow=T),2,rev)),col=grey(seq(1,0,length.out=256)))

library(h2o)
localH2O <- h2o.init(ip = "localhost", port = 54321, startH2O = TRUE, nthreads=-1)
trData<-h2o.importFile(localH2O,path = "prac_train.csv")
tsData<-h2o.importFile(localH2O,path = "prac_test.csv")


#In order to set a benchmark, we run a random forest classifier first as MNIST Kaggle competition recommends.

prac_train <- read.csv("prac_train.csv")
prac_test <- read.csv("prac_test.csv")
library(randomForest)
prac_train$label<-as.factor(prac_train$label)
prac.rf<-randomForest(label~.,prac_train)
my_table<-table(prac_test$label,predict(prac.rf,newdata=prac_test[,-1]))
print(my_table)

my_sum<-sum(diag(table(prac_test$label,predict(prac.rf,newdata=prac_test[,-1]))))
print(my_sum)


#Our first mission is to overcome this benchmark with h2o.deeplearning. Let's run h2o.deeplearning just as below. This is the easiest one.
#activation: Tanh
#hidden: rep(160,5)
#epochs: 20

res.dl <- h2o.deeplearning(x = 2:785, y = 1, data = trData, activation = "Tanh",hidden=rep(160,5),epochs = 20)
pred.dl<-h2o.predict(object=res.dl,newdata=tsData[,-1])
pred.dl.df<-as.data.frame(pred.dl)
sum(diag(table(prac_test$label,pred.dl.df[,1])))

#Successfully we overcame the benchmark!:) But we still have a long way to go... for example, we can get information about parameter tuning from Hinton's paper in 2012.

#activation: Tanh
#hidden: c(500,500,1000)
#epochs: 20
#rate: 0.01
#rate_annealing: 0.001
res.dl <- h2o.deeplearning(x = 2:785, y = 1, data = trData, activation = "Tanh",hidden=c(500,500,1000),
	epochs = 20,rate=0.01,rate_annealing = 0.001)
pred.dl<-h2o.predict(object=res.dl,newdata=tsData[,-1])
pred.dl.df<-as.data.frame(pred.dl)
sum(diag(table(prac_test$label,pred.dl.df[,1])))

write.table(pred.dl.df[,1],file='output.csv',quote=F,col.names=F,row.names=F,sep=',')
