在R中将as.h2o()上传文件到h2o环境需要很长时间

h.l*_*l.m 5 r h2o

我正在使用h2o进行一些建模,并且已经调整了模型,我现在希望它用于执行大约6bln预测/行的大量预测,每个预测行需要80列数据

数据集我已经将输入数据集向下分解,因此它大约有500 x 12百万个行块,每个行块都有相关的80列数据.

然而,上传一个data.table1200万到80列到h2o需要相当长的时间,而对我来说这样做需要花费相当长的时间...我认为这是因为它在上传之前首先解析了对象.

预测部分比较快......

有什么建议加速这部分吗?改变核心数量有帮助吗?

以下是问题的可重现的例子......

  # Load libraries
  library(h2o)
  library(data.table)

  # start up h2o using all cores...
  localH2O = h2o.init(nthreads=-1,max_mem_size="16g")

  # create a test input dataset
  temp <- CJ(v1=seq(20),
             v2=seq(7),
             v3=seq(24),
             v4=seq(60),
             v5=seq(60))
  temp <- do.call(cbind,lapply(seq(16),function(y){temp}))
  colnames(temp) <- paste0('v',seq(80))

  # this is the part that takes a long time!!
  system.time(tmp.obj <- as.h2o(localH2O,temp,key='test_input'))

  #|======================================================================| 100%
  #   user  system elapsed 
  #357.355   6.751 391.048 
Run Code Online (Sandbox Code Playgroud)

小智 12

由于您在本地运行H2O,因此您希望将该数据保存为文件,然后使用:

h2o.importFile(localH2O, file_path, key='test_intput')
Run Code Online (Sandbox Code Playgroud)

这将使每个线程并行读取它们的文件部分.如果在单独的服务器上运行H2O,则需要将数据复制到服务器可以读取的位置(大多数人不会将服务器设置为从其笔记本电脑上的文件系统中读取).

as.h2o()将文件串行上传到H2O.使用h2o.importFile(),H2O服务器找到该文件并并行读取它.

看起来你正在使用H2O的第2版.相同的命令可以在H2Ov3中使用,但是一些参数名称已经改变了一点.新参数名称如下:http://cran.r-project.org/web/packages/h2o/h2o.pdf


Ing*_*ifs 5

在解决这个问题的过程中,我做了一些测试,发现对于 R 内存中的对象(即您没有奢侈地已经以 .csv 或 .txt 形式提供它们),这是迄今为止加载它们的最快方法(~21 x) 是使用data.table 中的fwrite 函数将 csv 写入磁盘并使用 h2o.importFile 读取它。

我尝试过的四种方法:

  1. 直接使用 as.h2o()
  2. 使用 write.csv() 写入磁盘,然后使用 h2o.importFile() 加载
  3. 将数据分成两半,在每一半上运行 as.h2o(),然后使用 h2o.rbind() 组合
  4. 使用 fwrite() 从 data.table 写入磁盘,然后使用 h2o.importFile() 加载

我对不同大小的 data.frame 进行了测试,结果似乎很清楚。在此处输入图片说明

如果有人对复制感兴趣,代码如下。

library(h2o)
library(data.table)
h2o.init()

testdf <-as.data.frame(matrix(nrow=4000000,ncol=100))
testdf[1:1000000,] <-1000       # R won't let me assign the whole thing at once
testdf[1000001:2000000,] <-1000
testdf[2000001:3000000,] <-1000
testdf[3000001:4000000,] <-1000

resultsdf <-as.data.frame(matrix(nrow=20,ncol=5))
names(resultsdf) <-c("subset","method 1 time","method 2 time","method 3 time","method 4 time")
for(i in 1:20){
    subdf <- testdf[1:(200000*i),]
    resultsdf[i,1] <-100000*i
    
    # 1: use as.h2o()
    
    start <-Sys.time()
    as.h2o(subdf)
    stop <-Sys.time()
    resultsdf[i,2] <-as.numeric(stop)-as.numeric(start)
    
    # 2: use write.csv then h2o.importFile() 

    start <-Sys.time()
    write.csv(subdf,"hundredsandthousands.csv",row.names=FALSE)
    h2o.importFile("hundredsandthousands.csv")
    stop <-Sys.time()
    resultsdf[i,3] <-as.numeric(stop)-as.numeric(start)

    # 3: Split dataset in half, load both halves, then merge

    start <-Sys.time()
    length_subdf <-dim(subdf)[1]
    h2o1 <-as.h2o(subdf[1:(length_subdf/2),])
    h2o2 <-as.h2o(subdf[(1+length_subdf/2):length_subdf,])
    h2o.rbind(h2o1,h2o2)
    stop <-Sys.time()
    resultsdf[i,4] <- as.numeric(stop)-as.numeric(start)
    
    # 4: use fwrite then h2o.importfile()

    start <-Sys.time()
    fwrite(subdf,file="hundredsandthousands.csv",row.names=FALSE)
    h2o.importFile("hundredsandthousands.csv")
    stop <-Sys.time()
    resultsdf[i,5] <-as.numeric(stop)-as.numeric(start)

    plot(resultsdf[,1],resultsdf[,2],xlim=c(0,4000000),ylim=c(0,900),xlab="rows",ylab="time/s",main="Scaling of different methods of h2o frame loading")
    for (i in 1:3){
        points(resultsdf[,1],resultsdf[,(i+2)],col=i+1)
        }
    legendtext <-c("as.h2o","write.csv then h2o.importFile","Split in half, as.h2o and rbind","fwrite then h2o.importFile")
    legend("topleft",legend=legendtext,col=c(1,2,3,4),pch=1)

    print(resultsdf)
    flush.console()
    }
    
Run Code Online (Sandbox Code Playgroud)