在神经网络和插入符号 (R) 中设置隐藏层和神经元

Setting hidden layers and neurons in neuralnet and caret (R)

我想使用包 neuralnetcaret.

交叉验证神经网络

可以从this post复制数据df

当运行 neuralnet() 函数时,有一个名为hidden 的参数,您可以在其中设置隐藏层和神经元。假设我想要 2 个分别具有 3 个和 2 个神经元的隐藏层。它会写成 hidden = c(3, 2).

然而,由于我想对其进行交叉验证,我决定使用出色的 caret 包。但是在使用函数train()的时候,我不知道如何设置层数和神经元数。

有谁知道我可以在哪里添加这些号码?

这是我的代码 运行:

nn <- caret::train(DC1 ~ ., data=df, 
                   method = "neuralnet", 
                   #tuneGrid = tune.grid.neuralnet,
                   metric = "RMSE",
                   trControl = trainControl (
                     method = "cv", number = 10,
                     verboseIter = TRUE
))

顺便说一下,我在使用之前的代码时收到了一些警告:

predictions failed for Fold01: layer1=3, layer2=0, layer3=0 Error in cbind(1, pred) %*% weights[[num_hidden_layers + 1]] : 
  requires numeric/complex matrix/vector arguments

关于如何解决的想法?

在插入符号中使用神经网络模型时,为了指定三个支持层中每一层的隐藏单元数,您可以使用参数 layer1layer2layer3 .我通过检查 source.

发现了
library(caret)

grid <-  expand.grid(layer1 = c(32, 16),
                     layer2 = c(32, 16),
                     layer3 = 8)

BostonHousing 数据用例:

library(mlbench)

data(BostonHousing)

为了简单起见,让示例只包含 select 个数字列:

BostonHousing[,sapply(BostonHousing, is.numeric)] -> df

nn <- train(medv ~ ., 
            data = df, 
            method = "neuralnet", 
            tuneGrid = grid,
            metric = "RMSE",
            preProc = c("center", "scale", "nzv"), #good idea to do this with neural nets - your error is due to non scaled data
            trControl = trainControl(
              method = "cv",
              number = 5,
              verboseIter = TRUE)
            )

部分

preProc = c("center", "scale", "nzv")

对于算法收敛至关重要,神经网络不喜欢未缩放的特征

虽然超级慢。

nn
#output
Neural Network 

506 samples
 12 predictor

Pre-processing: centered (12), scaled (12) 
Resampling: Cross-Validated (5 fold) 
Summary of sample sizes: 405, 404, 404, 405, 406 
Resampling results across tuning parameters:

  layer1  layer2  RMSE      Rsquared   MAE     
  16      16           NaN        NaN       NaN
  16      32      4.177368  0.8113711  2.978918
  32      16      3.978955  0.8275479  2.822114
  32      32      3.923646  0.8266605  2.783526

Tuning parameter 'layer3' was held constant at a value of 8
RMSE was used to select the optimal model using the smallest value.
The final values used for the model were layer1 = 32, layer2 = 32 and layer3 = 8.