我必须对模型进行多次仿真。我试图在不同的内核上运行不同的仿真,并将结果保存到全局环境中。我有下面的代码不起作用。例如,我必须运行xgboost 3次(使用所有相同的参数,仅需重新运行3次,而我想在不同的内核上运行这3个仿真,并将imp_df_i返回到全局环境。
cores=detectCores()
cl <- makeCluster(cores[1]-1) #not to overload your computer
registerDoParallel(cl)
finalMatrix <- foreach(i=1:3, .combine=cbind) %dopar% {
require(xgboost)
xgb_1 = xgboost(dtrain,
params = xgb_params_1,
nrounds = 3, # max number of trees to build
verbose = TRUE,
print_every_n = 1,
early_stop_round = 10) # stop if no improvement within 10 trees
col_names = attr(tr.x, ".Dimnames")[[2]]
imp = xgb.importance(col_names, xgb_1)
imp_df=data.frame(imp)
assign(paste("imp_df",i, sep = "_"),imp_df,envir = .GlobalEnv )
}
#stop cluster
stopCluster(cl)