【问题标题】:Parallel execution of train in caret fails with function not found在插入符号中并行执行火车失败,找不到函数
【发布时间】:2014-01-28 12:33:25
【问题描述】:

昨天我更新了我的 R 包,然后并行执行 train 函数失败了。

似乎从工作人员内部调用的某些函数不可用。这些函数至少是 flatTable 和 probFunction。

我在生产机器上遇到了这个问题,并且能够在干净的 Windows 7 x64 VM 上重现它。

我在下面添加了一个最小的工作示例。尊敬的 stackoverflow 用户:感谢您的帮助!

# R 3.0.2 x64, RStudio Version 0.98.490, Windows 7 x64

data(iris)
library(caret) # 6.0-21
library(doParallel) # 1.0.6

model <- "rf"

# Fail
?probFunction
?flatTable

fitControl <- trainControl(
  method = "repeatedcv"
  , number = 5  ## 5-fold CV
  , repeats = 1   ## repeated one times
  , verboseIter =TRUE
)

#### Sequential Version ####

# Runs
train(Species ~ ., data = iris, method = model, trControl = fitControl)

#### Parallelized version ####

# Fails with 
# Error in e$fun(obj, substitute(ex), parent.frame(), e$data) : 
#  worker initialization failed: Error in eval(expr, envir, enclos): could not find function "flatTable"
cl <- makeCluster(3)
registerDoParallel(cl)

train(Species ~ ., data = iris, method = model, trControl = fitControl)

stopCluster(cl)

# Fails with 
# Error in { : task 1 failed - "could not find function "probFunction""
fitControl <- trainControl(
  method = "repeatedcv"
  , number = 5  ## 5-fold CV
  , repeats = 1   ## repeated one times
  , verboseIter =TRUE
  , classProbs = TRUE
)

cl <- makeCluster(3)
registerDoParallel(cl)

train(Species ~ ., data = iris, method = model, trControl = fitControl)

stopCluster(cl)

#### Again sequential version ####

# Fails with
# Error in summary.connection(connection) : invalid connection
train(Species ~ ., data = iris, method = model, trControl = fitControl)

R 会话信息

R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C                    LC_TIME=German_Germany.1252   

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base    

other attached packages:
[1] e1071_1.6-1        class_7.3-9        randomForest_4.6-7 doParallel_1.0.6   iterators_1.0.6  
[6] foreach_1.4.1      caret_6.0-21       ggplot2_0.9.3.1    lattice_0.20-23  

loaded via a namespace (and not attached):
[1] car_2.0-19         codetools_0.2-8    colorspace_1.2-4   compiler_3.0.2     dichromat_2.0-0  
 [6] digest_0.6.4       grid_3.0.2         gtable_0.1.2       labeling_0.2       MASS_7.3-29      
[11] munsell_0.4.2      nnet_7.3-7         plyr_1.8           proto_0.3-10       RColorBrewer_1.0-5
[16] reshape2_1.2.2     scales_0.2.3       stringr_0.6.2      tools_3.0.2      

【问题讨论】:

    标签: r parallel-processing r-caret


    【解决方案1】:

    您遇到的错误是由使用 doParallel、doSNOW 和 doMPI 时插入符号 6.0-21 中的错误引起的。它已在 R-forge 的 6.0-22 版本中得到修复,但尚未发布到 CRAN。如果您不想等待新版本发布,您可以:

    1. 降级到插入符号 5.x
    2. 从 R-forge 安装插入符号 6.0-22
    3. 从 R-forge 而不是 doParallel 安装和使用 doSNOW 1.0.10

    问题是由 CRAN 策略的更改引起的,该更改禁止使用 ::: 运算符,即使在引用同一包中的非导出函数时也是如此。


    更新

    Caret 6.0-22 于 2014 年 1 月 18 日发布给 CRAN。这应该使用带有 doSNOW 和类似并行后端的插入符号解决报告的问题。

    【讨论】:

    • 非常感谢。我应用了选项 1,降级。
    • @Ahue 一旦固定版本在 CRAN 上,我会更新答案。
    • 一年多后,我在caret的当前版本中遇到了这个问题... &gt; packageVersion("caret") [1] ‘6.0.41’
    • @Hack-R 我刚刚使用插入符号 6.0.41 成功运行了这个示例。这个例子对你来说是失败的,还是你在一个稍微不同的例子中得到了类似的错误?
    【解决方案2】:

    按照@Steve Weston 的建议,第一个错误 (could not find function ...) 在更新版本中消失了,但第二个错误 (Error in summary.connection(connection) : invalid connection) 仍然存在。

    对于插入符号版本 6.0.84,我可以通过将 allowParallel = F 添加到最后顺序运行的 trainControl 参数来修复它。

    问题中代码的最后一部分更改为:

    #### Again sequential version (new) ####
    
    fitControl_new <- trainControl(
      method = "repeatedcv"
      , number = 5  
      , repeats = 1   
      , verboseIter =TRUE
      , classProbs = TRUE
      , allowParallel = F     ## add this argument to overwrite the default TRUE
    )
    
    train(Species ~ ., data = iris, method = model, trControl = fitControl_new)
    
    

    【讨论】:

    • 谢谢您 - 遇到了同样的问题,您的解决方案立即提供了帮助 :)
    猜你喜欢
    • 2015-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 2018-06-08
    • 2016-08-29
    • 1970-01-01
    相关资源
    最近更新 更多