R Package Deepnet: Training and Testing the MNIST dataset

ぐ巨炮叔叔 提交于 2019-11-30 15:58:48

Don't know if you are still working on it, or if you've found the solution but : 1/ try this : train.image.labels <- data.matrix(train.image.labels)

2/ i use nn.predict, even if the neural network is trained by dbn.dnn.train.

As you know the input values for neural network better to be between 0 and 1. In "deepnet" package, unlike the nn.train function, for dbn.dnn.train you need to normalize the input yourself. Here is the complete code to load, train, and test.

#loading MNIST
setwd("path/to/MNIST/")
mnist <- load.mnist(".")
# the function to normalize the input values
normalize <- function(x) {
  return (x/255)
}
# standardization
train_x_n <-  apply(mnist$train$x, c(1,2),FUN = normalize)
test_x_n <- apply(mnist$test$x, c(1,2),FUN = normalize)
#training and prediction
dnn <- dbn.dnn.train(train_x_n, mnist$train$yy, hidden = c(100, 70, 80), numepochs = 3, cd = 3)
err.dnn <- nn.test(dnn, test_x_n, mnist$test$yy)
dnn_predict <- nn.predict(dnn, test_x_n)
# test the outputs
print(err.dnn)
print(dnn_predict[1,])
print(mnist$test$y[1])

Outout:

> err.dnn
[1] 0.0829
> dnn_predict[1,]
[1] 7.549055e-04 1.111647e-03 1.946491e-03 7.417489e-03 3.221340e-04 7.306264e-04 4.088365e-05 9.944441e-01 8.953903e-05
[10] 9.085863e-03
> mnist$test$y[1]
[1] 7
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!