【问题标题】:tuneGrid not working properly in neural network modeltuneGrid 在神经网络模型中无法正常工作
【发布时间】:2021-07-19 18:32:34
【问题描述】:

我想使用 caret 包构建一个神经网络分类器。我指定了一个带有一些超参数的 tunegrid,我想测试这些超参数以获得最佳精度。

运行模型后,训练函数函数将始终默认为标准衰减和大小值。这是插入符号中的错误吗?还是我的代码有问题?

代码:

nnet_grid <- expand.grid(.decay = c(0.5, 0.1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7), .size = c(3, 5, 10, 20))

features.nn <- train(label ~ .,
                      method     = "nnet",
                      trControl  = trControl,
                      data       = features,
                      tunegrid = nnet_grid,
                      verbose = FALSE)

输出:

No pre-processing
Resampling: Cross-Validated (5 fold) 
Summary of sample sizes: 1680, 1680, 1680, 1680, 1680 
Resampling results across tuning parameters:

  size  decay  Accuracy    Kappa 
  1     0e+00  0.10904762  0.0645
  1     1e-04  0.10142857  0.0565
  1     1e-01  0.14380952  0.1010
  3     0e+00  0.09571429  0.0505
  3     1e-04  0.05523810  0.0080
  3     1e-01  0.19190476  0.1515
  5     0e+00  0.13000000  0.0865
  5     1e-04  0.14761905  0.1050
  5     1e-01  0.31809524  0.2840

Accuracy was used to select the optimal model using the largest value.
The final values used for the model were size = 5 and decay = 0.1.

【问题讨论】:

    标签: r machine-learning r-caret


    【解决方案1】:

    您提供了错误的参数,它应该是 tuneGrid = 而不是 tunegrid = ,因此插入符号将此解释为 nnet 的参数并选择自己的网格

    使用您在上面看到的网格,caret 将选择精度最高的模型,并且从提供的结果中,它是 size=5 和 decay=0.1,最高精度为 0.318。

    要使用您定义的网格,使用示例:

    data = MASS::Pima.tr
    
    nnet_grid <- expand.grid(
    decay = c(0.5, 1e-2, 1e-3),
    size = c(3,5,10,20))
    
    set.seed(123)
    nn <- train( type ~ .,
                          method     = "nnet",
                          trControl  = trainControl(method="cv",10),
                          data       = data,
                          tuneGrid = nnet_grid,
                          verbose = FALSE)
    

    在这里您可以看到选择了另一个参数,但是如果您查看结果的准确性,差异很小:

            Neural Network 
    
    200 samples
      7 predictor
      2 classes: 'No', 'Yes' 
    
    No pre-processing
    Resampling: Cross-Validated (10 fold) 
    Summary of sample sizes: 179, 180, 180, 181, 180, 180, ... 
    Resampling results across tuning parameters:
    
      decay  size  Accuracy   Kappa    
      0.001   3    0.7211153  0.3138427
      0.001   5    0.6253008  0.1391728
      0.001  10    0.6948747  0.2848068
      0.001  20    0.6546366  0.2369800
      0.010   3    0.7103509  0.3215962
      0.010   5    0.6861153  0.2861830
      0.010  10    0.6596115  0.2438720
      0.010  20    0.6448496  0.1722412
      0.500   3    0.6403258  0.1484703
      0.500   5    0.6603258  0.1854491
      0.500  10    0.6603509  0.1896705
      0.500  20    0.6400877  0.1642272
    
    Accuracy was used to select the optimal model using the largest value.
    The final values used for the model were size = 3 and decay = 0.001.
    

    不太确定您是否扩展了数据,但通常您需要它,请参阅post

    nn <- train( type ~ .,
                  method     = "nnet",
                  trControl  = trainControl(method="cv",10),
                  data       = data,
                  tuneGrid = nnet_grid,
                  preProcess = c("center","scale"),
                              verbose = FALSE)
    
    Neural Network 
    
    200 samples
      7 predictor
      2 classes: 'No', 'Yes' 
    
    Pre-processing: centered (7), scaled (7) 
    Resampling: Cross-Validated (10 fold) 
    Summary of sample sizes: 180, 180, 180, 179, 180, 180, ... 
    Resampling results across tuning parameters:
    
      decay  size  Accuracy   Kappa    
      0.001   3    0.7158772  0.3699193
      0.001   5    0.6653759  0.2586270
      0.001  10    0.6458772  0.2193141
      0.001  20    0.6606140  0.2648904
      0.010   3    0.6945865  0.3465460
      0.010   5    0.6706140  0.2479049
      0.010  10    0.6651128  0.2433722
      0.010  20    0.6858521  0.2918013
      0.500   3    0.7403759  0.4060926
      0.500   5    0.7453759  0.4154149
      0.500  10    0.7553759  0.4345907
      0.500  20    0.7553759  0.4275870
    

    【讨论】:

      猜你喜欢
      • 2013-12-17
      • 2012-02-04
      • 2015-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-16
      相关资源
      最近更新 更多