为什么R deepnet慢?我怎样才能加快速度?

时间:2015-08-27 01:59:39

标签: r performance

最近,我使用“deepnet”训练MNIST。

我的代码是:

testMNIST <- function(){

    mnist <- load.mnist("./mnist/")
    cat ("Load MNIST data succeed!", "\n")

    train_x <- mnist$train$x
    train_y <- mnist$train$y
    train_y_mat <- mnist$train$yy

    test_x <- mnist$test$x
    test_y <- mnist$test$y
    test_y_mat <- mnist$test$yy

    dnn <- dbn.dnn.train(train_x, train_y, hidden = c(1000, 500, 200), learningrate = 0.01, numepochs = 100)

    err_rate <- nn.test(dnn, test_x, test_y)
    cat ("The Error rate of training DBN with label vector:", "\n")
    print (err_rate)
}

我在Linux服务(24GB内存,1T硬盘)上运行代码,但速度太慢了。它只训练了一层12小时。 那么,我该如何提高性能呢? 对于像MNIST数据这样的标签,当训练,矢量标签或矩阵标签时哪个更好?

1 个答案:

答案 0 :(得分:1)

首先deepnet由R本身编写,所以它有点慢。 deepnet中最耗时的函数是矩阵乘法。因此,在后端添加并行BLAS非常有用,例如Intel MKLOpenBLAS甚至NVIDIA cuBLAS

隐式并行模式部分的R with Parallel Computing with User Perspectives blog中的一个示例,OpenBLAS可以 2.5X 加速比较本地R + deepnet。

#install.packages("data.table")
#install.packages("deepnet")

library(data.table)
library(deepnet)

# download MNIST dataset in below links
# https://h2o-public-test-data.s3.amazonaws.com/bigdata/laptop/mnist/train.csv.gz
# https://h2o-public-test-data.s3.amazonaws.com/bigdata/laptop/mnist/test.csv.gz
mnist.train <- as.matrix(fread("./train.csv", header=F))
mnist.test  <- as.matrix(fread("./test.csv", header=F))

# V785 is the label
x <- mnist.train[, 1:784]/255
y <- model.matrix(~as.factor(mnist.train[, 785])-1)

system.time(
nn <- dbn.dnn.train(x,y,
                    hidden=c(64),
                    #hidden=c(500,500,250,125),
                    output="softmax",
                    batchsize=128, 
                    numepochs=100, 
                    learningrate = 0.1)
)

结果如下 2115 sec .vs。 867秒

> R CMD BATCH deepnet_mnist.R
> cat deepnet_mnist.Rout
deep nn has been trained.
 user system elapsed 
 2110.710 2.311 2115.042

> env LD_PRELOAD=/.../tools/OpenBLAS/lib/libopenblas.so R CMD BATCH deepnet_mnist.R
> cat deepnet_mnist.Rout
deep nn has been trained.
 user system elapsed 
 2197.394 10496.190 867.748

其次H2OMXNET对于深度学习会更快。

最后,请注意您的hidden = c(1000, 500, 200)网络非常庞大,即使使用多核也会非常慢:(