tuneGrid 在神经网络模型中无法正常工作

tuneGrid not working properly in neural network model

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

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

代码:

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.

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

根据您在上面看到的网格,插入符号将选择精度最高的模型,从提供的结果来看,它的大小为 5,衰减为 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