library(mxnet)
# Data preparation
train <- read.csv('https://github.com/ozt-ca/tjo.hatenablog.samples/raw/master/r_samples/public_lib/jp/mnist_reproduced/short_prac_train.csv')
test <- read.csv('https://github.com/ozt-ca/tjo.hatenablog.samples/raw/master/r_samples/public_lib/jp/mnist_reproduced/short_prac_test.csv')
train <- data.matrix(train)
test <- data.matrix(test)
train.x <- train[,-1]
train.y <- train[,1]
train.x <- t(train.x/255)
test_org <- test
test <- test[,-1]
test <- t(test/255)
test_org[97:103,1:7]
##      label pixel0 pixel1 pixel2 pixel3 pixel4 pixel5
## [1,]     0      0      0      0      0      0      0
## [2,]     0      0      0      0      0      0      0
## [3,]     0      0      0      0      0      0      0
## [4,]     0      0      0      0      0      0      0
## [5,]     1      0      0      0      0      0      0
## [6,]     1      0      0      0      0      0      0
## [7,]     1      0      0      0      0      0      0
data <- mx.symbol.Variable("data")
 fc1 <- mx.symbol.FullyConnected(data, name="fc1", num_hidden=128)
 act1 <- mx.symbol.Activation(fc1, name="relu1", act_type="relu")
 fc2 <- mx.symbol.FullyConnected(act1, name="fc2", num_hidden=64)
 act2 <- mx.symbol.Activation(fc2, name="relu2", act_type="relu")
 fc3 <- mx.symbol.FullyConnected(act2, name="fc3", num_hidden=10)
 softmax <- mx.symbol.SoftmaxOutput(fc3, name="sm")
 devices <- mx.cpu()
 mx.set.seed(0)
 model <- mx.model.FeedForward.create(softmax, X=train.x, y=train.y, ctx=devices, num.round=10, array.batch.size=100,
                                      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))
## Warning in mx.model.select.layout.train(X, y): Auto detect layout input matrix, use colmajor..
## Start training with 1 devices
## [1] Train-accuracy=0.470204081632653
## [2] Train-accuracy=0.8326
## [3] Train-accuracy=0.9052
## [4] Train-accuracy=0.9278
## [5] Train-accuracy=0.9466
## [6] Train-accuracy=0.9568
## [7] Train-accuracy=0.9646
## [8] Train-accuracy=0.9756
## [9] Train-accuracy=0.9888
## [10] Train-accuracy=0.9926
 preds <- predict(model, test)
## Warning in mx.model.select.layout.predict(X, model): Auto detect layout input matrix, use colmajor..
 dim(preds)
## [1]   10 1000
 dim(test)
## [1]  784 1000
 t(preds[1:8,1:7])
##           [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.9999969 1.317755e-14 1.589962e-06 6.074838e-09 3.611948e-11
## [2,] 0.9999995 5.126884e-15 2.002695e-07 1.661002e-07 3.718969e-13
## [3,] 0.9999769 5.292092e-15 1.876923e-07 3.734210e-09 7.665295e-11
## [4,] 0.1416395 5.842936e-12 3.163636e-02 7.424699e-07 7.118647e-04
## [5,] 0.9999815 1.539962e-11 4.302275e-06 1.112681e-06 1.971993e-09
## [6,] 0.9999206 8.039203e-12 8.970006e-06 2.368191e-07 5.628424e-08
## [7,] 0.9990907 1.000745e-10 3.315137e-05 3.895680e-07 2.098697e-04
##              [,6]         [,7]         [,8]
## [1,] 3.478055e-09 9.787780e-07 2.249146e-09
## [2,] 2.376122e-09 3.812215e-08 1.035085e-10
## [3,] 1.806618e-08 5.179310e-08 1.256901e-05
## [4,] 1.657250e-07 8.259764e-01 2.155527e-08
## [5,] 2.141903e-07 6.556791e-06 1.561373e-06
## [6,] 7.062349e-07 3.770629e-05 1.259197e-07
## [7,] 2.745748e-04 3.264020e-04 4.216875e-06
## get max.col only for the 7 first rows
max.col(t(preds))[1:7]
## [1] 1 1 1 7 1 1 1
##  rea-ttribute labels to tested images
pred.label <- max.col(t(preds)) - 1
table(pred.label)
## pred.label
##   0   1   2   3   4   5   6   7   8   9 
##  95 101 101  94 104 104  99 107 102  93

Normally, there are 100 images by digital number. for example 0 gets 95, there are 5 image not classed as zero. -1: I think because R starts conting position of matrices by 1, C++ starts counting latruces by 0 max.col returns the position (column number/index ) of higher number in each row.

## predicted labels
table(pred.label)
## pred.label
##   0   1   2   3   4   5   6   7   8   9 
##  95 101 101  94 104 104  99 107 102  93
## initial labels
table(test_org[,1])
## 
##   0   1   2   3   4   5   6   7   8   9 
## 100 100 100 100 100 100 100 100 100 100
## compare predicted label with initial labels
table(test_org[,1], pred.label)
##    pred.label
##       0   1   2   3   4   5   6   7   8   9
##   0  94   0   1   0   0   0   3   0   1   1
##   1   0 100   0   0   0   0   0   0   0   0
##   2   0   0  97   1   1   0   0   1   0   0
##   3   0   0   2  91   0   3   0   1   2   1
##   4   0   0   0   0  95   0   2   1   0   2
##   5   0   1   0   1   0  96   0   0   2   0
##   6   1   0   0   0   1   3  94   0   1   0
##   7   0   0   0   0   0   0   0 100   0   0
##   8   0   0   1   1   1   1   0   0  96   0
##   9   0   0   0   0   6   1   0   4   0  89
## get means of identical labels, located in the diagonal
sum(diag(table(test_org[,1],pred.label)))/1000
## [1] 0.952

To predicted an other dataset of image

#  results <- predict(model, new.dataset)
## same as
# results <- mxnet:::predict.MXFeedForwardModel(model, new.dataset)