我如何使用foreach函数进行并行编码?

问题描述 投票:1回答:1

[根据Jared Lander的教科书“ R for Everyone”(第19章),我一直在尝试使用Elastic Net。教科书使用以下“ foreach”代码通过并行编码找到最佳参数值。但是,即使我编写并运行完全相同的代码,生成的对象“ acsDouble”也不是应该包含11个cv.glmnet对象实例的列表。相反,它是一个空白列表。

我已经检查了教科书的代码,并在运行之前清除了环境,但问题并未解决。这里似乎是什么问题?

acs <- read.table("http://jaredlander.com/data/acs_ny.csv", sep=",",
                  header=TRUE, stringsAsFactors = FALSE)
require(useful)
# make a binary Income variable for building a logistic regression
acs$Income <- with(acs, FamilyIncome >= 150000)
# build predictor matrix
# do not include the intercept as glmnet will add that automatically
acsX <- build.x(Income ~ NumBedrooms + NumChildren + NumPeople +
                  NumRooms + NumUnits + NumVehicles + NumWorkers +
                  OwnRent + YearBuilt + ElectricBill + FoodStamp +
                  HeatingFuel + Insurance + Language - 1,
                data=acs, contrasts = FALSE)
# build response predictor
acsY <- build.y(Income ~ NumBedrooms + NumChildren + NumPeople +
                  NumRooms + NumUnits + NumVehicles + NumWorkers +
                  OwnRent + YearBuilt + ElectricBill + FoodStamp +
                  HeatingFuel + Insurance + Language - 1, data=acs)
require(glmnet)
require(parallel)
require(doParallel)
# set the seed for repeatability of random results
set.seed(2834673)
# create folds, we want observations to be in the same fold each time
# it is run
theFolds <- sample(rep(x = 1:5, length.out = nrow(acsX)))
# make sequence of alpha values
alphas <- seq(from = 0.5, to = 1, by = 0.05)
# set the seed for the repeatbility of random results
set.seed(5127151)
# start a cluster with two workers
cl <- makeCluster(2)
# regiser the workers
registerDoParallel(cl)
# keep track of timing
before <- Sys.time()
# build foreach loop to run in parallel
## several arguments
acsDouble <- foreach(i=1:length(alphas), .errorhandling = "remove",
                     .inorder = FALSE, .multicombine = TRUE,
                     .export = c("acsX", "acsY", "alphas", "theFolds"),
                     .packages = "glmnet") %dopar%
  {
    print(alphas[i])
    cv.glmnet(x=acsX, y=acsY, family="binamial", nfolds=5,
              foldid = theFolds, alpha = alphas[i])
  }
# stop timing
after <- Sys.time()
# make sure to stop the cluster when done
stopCluster(cl)
# time difference
# this will depend on speed, memory & number of cores of the machine
after - before
r foreach parallel-processing glmnet
1个回答
0
投票

cv.glmnet通话中有错字。应该是family="binomial";不是双亲的。

如果在群集节点中遇到任何错误,则会在foreach循环中显示.verbose=TRUE

© www.soinside.com 2019 - 2024. All rights reserved.