首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在神经网络和插入符号中设置隐藏层和神经元(R)

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

Stack Overflow用户
提问于 2020-06-04 16:14:25
回答 1查看 2K关注 0票数 0

我想交叉验证一个神经网络使用包neuralnetcaret

数据df可以从这个职位中复制。

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

然而,由于我想交叉验证它,我决定使用奇妙的caret包。但是当使用函数train()时,我不知道如何设置层数和神经元数。

有人知道我在哪里能把这些数字加起来吗?

这是我运行的代码:

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

顺便说一句,我收到了前面代码中的一些警告:

代码语言:javascript
复制
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

关于如何解决它的想法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-04 18:10:30

当在插入符号中使用神经网络模型时,为了指定三个支持层中每个层的隐藏单元数,可以使用参数layer1layer2layer3。我是通过检查来源发现的。

代码语言:javascript
复制
library(caret)

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

使用BostonHousing数据的用例:

代码语言:javascript
复制
library(mlbench)

data(BostonHousing)

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

代码语言:javascript
复制
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)
            )

部分

代码语言:javascript
复制
preProc = c("center", "scale", "nzv")

为了使算法收敛,神经网络不喜欢无标度特征。

不过太慢了。

代码语言:javascript
复制
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.
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62199352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档