代码之家  ›  专栏  ›  技术社区  ›  Make42

使插入符号的泛型功能选择更快

  •  1
  • Make42  · 技术社区  · 6 年前

    我试图通过使用特征选择和插入符号的遗传算法来优化一个xgboost树。

    results <- gafs(iris[,1:4], iris[,5],
                   iters = 2,
                   method = "xgbTree",
                   metric = "Accuracy",
                   gafsControl = gafsControl(functions=caretGA, method="cv", repeats=2, verbose = TRUE),
                   trConrol = trainControl(method = "cv", classProbs = TRUE, verboseIter = TRUE)
                   )
    

    尽管我只是在使用 iters = 2 而不是 iters = 200 这更合适。我该怎么做才能让这个更快?

    1 回复  |  直到 6 年前
        1
  •  2
  •   makeyourownmaker    6 年前

    下面是一个将 gafs() 函数使用 doParallel 打包并修改一些其他参数以使其更快。在可能的情况下,我包括运行时间。

    原始代码正在使用交叉验证( method = "cv" )不重复交叉验证( method = "repeatedcv" )所以我相信 repeats = 2 参数被忽略。我没有在并行示例中包含这个选项。

    弗斯特 ,在不进行任何修改或并行化的情况下使用原始代码:

    > library(caret)
    > data(iris)
    
    > set.seed(1)
    > st.01 <- system.time(results.01 <- gafs(iris[,1:4], iris[,5],
                                              iters  = 2, 
                                              method = "xgbTree", 
                                              metric = "Accuracy",
                                              gafsControl = gafsControl(functions = caretGA, 
                                                                        method  = "cv", 
                                                                        repeats = 2, 
                                                                        verbose = TRUE),
                                              trConrol = trainControl(method = "cv", 
                                                                      classProbs  = TRUE, 
                                                                      verboseIter = TRUE)))
    
    Fold01 1 0.9596575 (1)
    Fold01 2 0.9596575->0.9667641 (1->1, 100.0%) *
    Fold02 1 0.9598146 (1)
    Fold02 2 0.9598146->0.9641482 (1->1, 100.0%) *
    Fold03 1 0.9502661 (1)
    

    我昨晚(8到10个小时)运行了上述代码,但由于运行时间太长而停止运行。运行时间的粗略估计至少为24小时。

    第二 ,包括减少 popSize 参数(从50到20) allowParallel genParallel 选项到 gafsControl() 最后减少 number 两个区域的折叠(从10到5) GAFS控件() trControl() :

    > library(doParallel)
    > cl <- makePSOCKcluster(detectCores() - 1)
    > registerDoParallel(cl)
    
    > set.seed(1)
    > st.09 <- system.time(results.09 <- gafs(iris[,1:4], iris[,5],
                                              iters   = 2, 
                                              popSize = 20, 
                                              method  = "xgbTree", 
                                              metric  = "Accuracy",
                                              gafsControl = gafsControl(functions = caretGA, 
                                                                        method    = "cv", 
                                                                        number    = 5, 
                                                                        verbose   = TRUE, 
                                                                        allowParallel = TRUE, 
                                                                        genParallel   = TRUE),
                                              trConrol = trainControl(method      = "cv", 
                                                                      number      = 5, 
                                                                      classProbs  = TRUE, 
                                                                      verboseIter = TRUE)))
    
     final GA
     1 0.9508099 (4)
     2 0.9508099->0.9561501 (4->1, 25.0%) *
     final model
    > st.09
       user   system  elapsed
       3.536    0.173 4152.988
    

    我的系统有4个核心,但按规定它只使用3个,我验证了它运行的是3R进程。

    这个 GAFS控件() 文档描述 允许并行 基因平行 像这样:

    • 允许并行 :如果并行后端已加载并可用, 函数应该使用它吗?

    • 基因平行 :如果并行后端已加载并可用,则应该 “gafs”使用it tp在 重新取样中的一代人?

    插入符号文档建议 允许并行 选项将比 基因平行 选项: https://topepo.github.io/caret/feature-selection-using-genetic-algorithms.html

    与原始代码相比,我希望并行代码的结果至少稍有不同。以下是并行代码的结果:

    > results.09
    
    Genetic Algorithm Feature Selection
    
    150 samples
    4 predictors
    3 classes: 'setosa', 'versicolor', 'virginica'
    
    Maximum generations: 2
    Population per generation: 20
    Crossover probability: 0.8
    Mutation probability: 0.1
    Elitism: 0
    
    Internal performance values: Accuracy, Kappa
    Subset selection driven to maximize internal Accuracy
    
    External performance values: Accuracy, Kappa
    Best iteration chose by maximizing external Accuracy
    External resampling method: Cross-Validated (5 fold)
    
    During resampling:
      * the top 4 selected variables (out of a possible 4):
        Petal.Width (80%), Petal.Length (40%), Sepal.Length (20%), Sepal.Width (20%)
      * on average, 1.6 variables were selected (min = 1, max = 4)
    
    In the final search using the entire training set:
       * 4 features selected at iteration 1 including:
         Sepal.Length, Sepal.Width, Petal.Length, Petal.Width
       * external performance at this iteration is
    
       Accuracy       Kappa
         0.9467      0.9200