如您所知,神经网络的输入值最好在 0 和 1 之间。在“deepnet”包中,与nn.train
函数不同,dbn.dnn.train
您需要自己对输入进行归一化。这是加载、训练和测试的完整代码。
#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])
输出:
> 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