我在 8 核 Linux 上使用以下代码,它占用了所有 8 个核心(6 个工作人员中的每一个都占用 130% 的利用率)
library(mlr); library(parallel); library(parallelMap)
# Load data
iris_num <- iris; iris_num$Species <- as.numeric(iris_num$Species)
#create tasks
traintask <- makeRegrTask(data = iris_num, target = 'Species')
#create learner
lrn = makeLearner('regr.xgboost'); nthread <- min(6, detectCores());
lrn$par.vals = list(print.every.n = 500, objective = "reg:linear", eval_metric="rmse", nthread = nthread)
#set parameter space
params <- makeParamSet(
makeIntegerParam("max_depth",lower = 5L,upper = 20L), # 6
makeNumericParam("min_child_weight",lower = 1L,upper = 20L), # 1
makeNumericParam("subsample",lower = 0.5,upper = 1),
makeNumericParam("colsample_bytree",lower = 0.5,upper = 1),
makeIntegerParam("nrounds",lower=3000,upper=5000),
makeNumericParam("lambda",lower=0.75,upper=1),
makeNumericParam("lambda_bias",lower=0,upper=0.75),
makeNumericParam("gamma",lower=0,upper=1),
makeNumericParam("eta", lower = 0.01, upper = 0.05) # 0.3
)
#set resampling strategy
rdesc <- makeResampleDesc("CV",iters=9L)
#search strategy
ctrl <- makeTuneControlRandom(maxit = 10L)
#set parallel backend
if(Sys.info()['sysname'] == "Linux") {
parallelStartMulticore(cpus = nthread, show.info = T)
} else parallelStartSocket(cpus = nthread, show.info = T)
tune <- tuneParams(learner = lrn, task = traintask,resampling = rdesc,measures = rmse, par.set = params, control = ctrl, show.info = T)
如何确保 mlr 只占用 6 个核心