在神经网络和插入符号(R)中设置隐藏层和神经元 [英] Setting hidden layers and neurons in neuralnet and caret (R)

查看:518
本文介绍了在神经网络和插入符号(R)中设置隐藏层和神经元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 neuralnet caret 包对神经网络进行交叉验证。



数据 df 可以从



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



但是,由于我想验证一下,我决定使用梦幻般的插入符包。但是当使用函数 train()时,我不知道如何设置层数和神经元。



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



这是我运行的代码:

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

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



<$ p Fold01的$ p> 预测失败:layer1 = 3,layer2 = 0,layer3 = 0 cbind(1,pred)%*%weights [[num_hidden_​​layers + 1]]错误:
需要数字/复杂矩阵/矢量参数

关于如何解决的想法?

解决方案

何时在插入符中使用神经网络模型以指定三个受支持层中每个层中隐藏单元的数量,可以使用参数 layer1 layer2 layer3 。我通过查看来发现。。 p>

 库(插入符)

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

使用BostonHousing数据的用例:

 库(mlbench)

数据(BostonHousing)

我们只需为示例选择数字列即可使其简单:

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

nn<-train(medv〜。,
data = df,
method = neuralnet,
tuneGrid = grid,
metric = RMSE,
preProc = c( center, scale, nzv),#使用神经网络做到这一点的好主意-您的错误是由于未缩放数据
trControl = trainControl(
method = cv,
number = 5,
verboseIter = TRUE)

零件

  preProc = c( center, scale,  nzv)

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



虽然它超级慢。

  nn 
#输出
神经网络

506个样本
12个预测变量

预处理:居中(12),定标(12)
重采样:交叉-有效(5折)
样本数量摘要:405、404、404、405、4 06
调整参数的重采样结果:

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

调整参数 layer3保持恒定,值为8
RMSE用于使用最小值选择最佳模型。
用于模型的最终值是layer1 = 32,layer2 = 32和layer3 =8。


I would like to cross-validate a neural network using the package neuralnet and caret.

The data df can be copied from this post.

When running the neuralnet() function, there is an argument called hidden where you can set the hidden layers and neurons in each. Let's say I want 2 hidden layers with 3 and 2 neurons respectively. It would be written as hidden = c(3, 2).

However, as I want to cross-validate it, I decided to use the fantastic caret package. But when using the function train(), I do not know how to set the number of layers and neurons.

Does anyone know where can I add these numbers?

This is the code I ran:

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

By the way, I am getting some warnings with the previous code:

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

Ideas on how to solve it?

解决方案

When using neural net model in caret in order to specify the number of hidden units in each of the three supported layers you can use the parameters layer1, layer2 and layer3. I found out by checking the source.

library(caret)

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

Use case with BostonHousing data:

library(mlbench)

data(BostonHousing)

lets just select numerical columns for the example to make it simple:

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)
            )

The part

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

is essential for the algorithm to converge, neural nets don't like unscaled features

Its super slow though.

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.

这篇关于在神经网络和插入符号(R)中设置隐藏层和神经元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆