我想使用caret包构建一个神经网络分类器。我已经指定了一个tunegrid,包含了一些我想测试的超参数以获得最佳准确性。
运行模型后,train函数总是默认使用标准的衰减和大小值。这是caret中的一个bug吗?还是我的代码有问题?
代码:
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-processingResampling: 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.2840Accuracy 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 =
,所以caret将其解释为nnet
的参数,并选择了自己的网格
在您看到的网格中,caret会选择准确率最高的模型,根据提供的结果,最高准确率为0.318,选择的参数是size=5和decay=0.1。
要使用您定义的网格,以下是一个示例:
data = MASS::Pima.trnnet_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-processingResampling: 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.1642272Accuracy 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.
不太确定您是否对数据进行了缩放,但通常需要这样做,请参见帖子:
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