目前正在尝试使用并行处理重现SVM递归特征消除算法,但遇到了并行化后端的一些问题。
当RFE SVM算法并行成功运行时,大约需要250秒。但是,大多数情况下它从未完成计算,需要在30分钟后手动关闭。当后者发生时,对活动监视器的检查表明,尽管Rstudio已将其关闭,但核仍在运行。需要使用终端中的killall R
终止这些核心。
包AppliedPredictiveModeling
中的代码段如下所示,删除了冗余代码。
library(AppliedPredictiveModeling)
data(AlzheimerDisease)
## The baseline set of predictors
bl <- c("Genotype", "age", "tau", "p_tau", "Ab_42", "male")
## The set of new assays
newAssays <- colnames(predictors)
newAssays <- newAssays[!(newAssays %in% c("Class", bl))]
## Decompose the genotype factor into binary dummy variables
predictors$E2 <- predictors$E3 <- predictors$E4 <- 0
predictors$E2[grepl("2", predictors$Genotype)] <- 1
predictors$E3[grepl("3", predictors$Genotype)] <- 1
predictors$E4[grepl("4", predictors$Genotype)] <- 1
genotype <- predictors$Genotype
## Partition the data
library(caret)
set.seed(730)
split <- createDataPartition(diagnosis, p = .8, list = FALSE)
adData <- predictors
adData$Class <- diagnosis
training <- adData[ split, ]
testing <- adData[-split, ]
predVars <- names(adData)[!(names(adData) %in% c("Class", "Genotype"))]
## This summary function is used to evaluate the models.
fiveStats <- function(...) c(twoClassSummary(...), defaultSummary(...))
## We create the cross-validation files as a list to use with different
## functions
set.seed(104)
index <- createMultiFolds(training$Class, times = 5)
## The candidate set of the number of predictors to evaluate
varSeq <- seq(1, length(predVars)-1, by = 2)
# Beginning parallelization
library(doParallel)
cl <- makeCluster(7)
registerDoParallel(cl)
getDoParWorkers()
# Rfe and train control objects created
ctrl <- rfeControl(method = "repeatedcv", repeats = 5,
saveDetails = TRUE,
index = index,
returnResamp = "final")
fullCtrl <- trainControl(method = "repeatedcv",
repeats = 5,
summaryFunction = fiveStats,
classProbs = TRUE,
index = index)
## Here, the caretFuncs list allows for a model to be tuned at each iteration
## of feature seleciton.
ctrl$functions <- caretFuncs
ctrl$functions$summary <- fiveStats
## This options tells train() to run it's model tuning
## sequentially. Otherwise, there would be parallel processing at two
## levels, which is possible but requires W^2 workers. On our machine,
## it was more efficient to only run the RFE process in parallel.
cvCtrl <- trainControl(method = "cv",
verboseIter = FALSE,
classProbs = TRUE,
allowParallel = FALSE)
set.seed(721)
svmRFE <- rfe(training[, predVars],
training$Class,
sizes = varSeq,
rfeControl = ctrl,
metric = "ROC",
## Now arguments to train() are used.
method = "svmRadial",
tuneLength = 12,
preProc = c("center", "scale"),
trControl = cvCtrl)
这不是导致我出现问题的唯一模型。有时带有RFE的随机森林也会导致同样的问题。原始代码使用包doMQ
,但是,对活动监视器的检查显示多个rsession
作为并行化,并且我猜测使用GUI运行,因为在计算时将其关闭不要停止需要中止整个R通信并重新启动会话,而不是简单地放弃计算。前者当然有擦拭我的环境清洁的不幸后果。
我在2013年中期使用了8核的MacBook Pro。
知道可能导致此问题的原因是什么?有没有办法解决它,如果有的话,什么?有没有办法确保并行化在没有GUI的情况下运行而无需从终端运行脚本(我希望能够控制执行哪些模型以及何时执行)。
编辑:似乎在退出失败的执行后,R在通过Caret并行化的所有后续任务上失败,即使是那些之前运行的任务。这意味着集群不再运作。