简体   繁体   English

如何在nnet中使用大小和衰减

[英]How to use size and decay in nnet

I am quite new to the neural network world so I ask for your understanding. 我对神经网络世界很陌生,所以我要求你理解。 I am generating some tests and thus I have a question about the parameters size and decay . 我正在生成一些测试,因此我对参数sizedecay有疑问。 I use the caret package and the method nnet . 我使用caret包和方法nnet Example dataset: 示例数据集:

require(mlbench)
require(caret)
require (nnet)

data(Sonar)
mydata=Sonar[,1:12] 

set.seed(54878)
ctrl = trainControl(method="cv", number=10,returnResamp = "all")
for_train= createDataPartition(mydata$V12, p=.70, list=FALSE) 
my_train=mydata[for_train,]
my_test=mydata[-for_train,] 

t.grid=expand.grid(size=5,decay=0.2)
mymodel = train(V12~ .,data=my_train,method="nnet",metric="Rsquared",trControl=ctrl,tuneGrid=t.grid) 

So, two are my questions. 所以,有两个是我的问题。 First, is this the best way with caret to use the nnet method?Second, I have read about the size and the decay (eg. Purpose of decay parameter in nnet function in R? ) but I cannot understand how to use them in practice here. 首先,这是使用nnet方法的最佳方式吗?其次,我已经读过大小和衰减(例如,R中nnet函数中衰减参数的目的 )但我无法理解如何在实践中使用它们这里。 Can anyone help? 有人可以帮忙吗?

Brief Caret explanation 简短的Caret解释

The Caret package lets you train different models and tuning hyper-parameters using Cross Validation (Hold-Out or K-fold) or Bootstrap. Caret软件包允许您使用交叉验证(Hold-Out或K-fold)或Bootstrap来训练不同的模型和调整超参数。

There are two different ways to tune the hyper-parameters using Caret: Grid Search and Random Search. 使用Caret调整超参数有两种不同的方法:网格搜索和随机搜索。 If you use Grid Search (Brute Force) you need to define the grid for every parameter according to your prior knowledge or you can fix some parameters and iterate on the remain ones. 如果使用网格搜索(蛮力),则需要根据您的先前知识为每个参数定义网格,或者您可以修复一些参数并迭代剩余参数。 If you use Random Search you need to specify a tuning length (maximum number of iterations) and Caret is going to use random values for hyper-parameters until the stop criteria holds. 如果使用随机搜索,则需要指定调整长度(最大迭代次数),Caret将使用超参数的随机值,直到停止条件成立。

No matter what method you choose Caret is going to use each combination of hyper-parameters to train the model and compute performance metrics as follows: 无论您选择何种方法,Caret都将使用每个超参数组合来训练模型并计算性能指标,如下所示:

  1. Split the initial Training samples into two different sets: Training and Validation (For bootstrap or Cross validation) and into k sets (For k-fold Cross Validation). 将初始训练样本分为两组:训练和验证(用于自举或交叉验证)和k组(用于k折交叉验证)。

  2. Train the model using the training set and to predict on validation set (For Cross Validation Hold-Out and Bootstrap). 使用训练集训练模型并预测验证集(用于交叉验证保持和引导)。 Or using k-1 training sets and to predict using the k-th training set (For K-fold Cross Validation). 或者使用k-1训练集并使用第k个训练集进行预测(用于K-fold交叉验证)。

  3. On the validation set Caret computes some performance metrics as ROC, Accuracy... 在验证集上,Caret将一些性能指标计算为ROC,Accuracy ......

  4. Once the Grid Search has finished or the Tune Length is completed Caret uses the performance metrics to select the best model according to the criteria previously defined (You can use ROC, Accuracy, Sensibility, RSquared, RMSE....) 一旦网格搜索完成或调整长度完成,Caret使用性能指标根据先前定义的标准选择最佳模型(您可以使用ROC,准确度,敏感度,RSquared,RMSE ....)

  5. You can create some plot to understand the resampling profile and to pick the best model (Keep in mind performance and complexity) 您可以创建一些图来理解重采样配置文件并选择最佳模型(请记住性能和复杂性)

if you need more information about Caret you can check the Caret web page 如果您需要有关Caret的更多信息,请查看Caret网页

Neural Network Training Process using Caret 基于Caret的神经网络训练过程

When you train a neural network (nnet) using Caret you need to specify two hyper-parameters: size and decay . 当您使用Caret训练神经网络(nnet)时,您需要指定两个超参数: 大小衰减 Size is the number of units in hidden layer (nnet fit a single hidden layer neural network) and decay is the regularization parameter to avoid over-fitting. 大小是隐藏层中的单位数(nnet适合单个隐藏层神经网络),衰减是正则化参数,以避免过度拟合。 Keep in mind that for each R package the name of the hyper-parameters can change. 请记住,对于每个R包,超参数的名称都可以更改。

An example of training a Neural Network using Caret for classification: 使用Caret训练神经网络进行分类的示例:

fitControl <- trainControl(method = "repeatedcv", 
                           number = 10, 
                           repeats = 5, 
                           classProbs = TRUE, 
                           summaryFunction = twoClassSummary)

nnetGrid <-  expand.grid(size = seq(from = 1, to = 10, by = 1),
                        decay = seq(from = 0.1, to = 0.5, by = 0.1))

nnetFit <- train(Label ~ ., 
                 data = Training[, ],
                 method = "nnet",
                 metric = "ROC",
                 trControl = fitControl,
                 tuneGrid = nnetGrid,
                 verbose = FALSE)

Finally, you can make some plots to understand the resampling results. 最后,您可以制作一些图来了解重采样结果。 The following plot was generated from a GBM training process 以下图是从GBM培训过程生成的

GBM Training Process using Caret 使用Caret的GBM培训流程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM