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

在神经网络和插入符号中设置隐藏层和神经元(R),r,neural-network,cross-validation,r-caret,R,Neural Network,Cross Validation,R Caret,我想使用软件包neuralnet和caret交叉验证一个神经网络 数据df可以从中复制 运行neuralnet函数时,有一个名为hidden的参数,您可以在其中设置每个函数中的隐藏层和神经元。假设我想要两个隐藏层,分别有3个和2个神经元。它将被写为hidden=c3,2 然而,由于我想交叉验证它,我决定使用奇妙的插入符号包。但在使用功能训练时,我不知道如何设置层数和神经元数 有人知道在哪里可以添加这些数字吗 这是我运行的代码: nn <- caret::train(DC1 ~ ., dat

我想使用软件包neuralnet和caret交叉验证一个神经网络

数据df可以从中复制

运行neuralnet函数时,有一个名为hidden的参数,您可以在其中设置每个函数中的隐藏层和神经元。假设我想要两个隐藏层,分别有3个和2个神经元。它将被写为hidden=c3,2

然而,由于我想交叉验证它,我决定使用奇妙的插入符号包。但在使用功能训练时,我不知道如何设置层数和神经元数

有人知道在哪里可以添加这些数字吗

这是我运行的代码:

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

如何解决它的想法?

在插入符号中使用神经网络模型时,为了指定三个受支持层中每个层的隐藏单元数,可以使用参数layer1、layer2和layer3。我查了一下报纸就知道了

让我们仅为示例选择数字列,使其变得简单:

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)
            )
是算法收敛的必要条件,神经网络不喜欢无标度特征

不过速度非常慢

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.

当你说preProc=ccenter,scale,nzv时,我猜你是在缩放数据集,正如你提到的。但是,我想使用最小-最大归一化来重新缩放。如何在预处理程序中实现它?此外,中心和标度代表什么?中心-将数据中心化为均值=0,标度-将数据标度为单位方差var=1,nzv-删除接近零的方差预测值,大部分是常数预测值。如果您想使用最小-最大比例,则使用preProc=crange,rangeBounds=c0,1。签出:适用于所有选项。应用预处理的正确方法是在参数训练期间,而不是在参数训练之前,因此在训练中使用预处理功能,而不是对所有数据使用预处理功能。
library(mlbench)

data(BostonHousing)
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.