R语言利用neuralnet包训练神经网络

xiaoxiao2021-02-28  134

说明

神经网络由一组互联的结点组成,这些节点分别负责网络的输入,连接,处理以及输出。神经网络被广泛用于诸如分类、聚类、预测等诸多领域。借助neuralnet训练得到神经网络模型。

操作

导入数据集,并将数据分为训练集和测试集

data("iris") set.seed(2) ind = sample(2,nrow(iris),replace = TRUE,prob = c(0.7,0.3)) trainset = iris[ind == 1,] testset = iris[ind == 2,]

导入与安装包

library(neuralnet)

根据数据集在species列取值不同,为训练集新增三种数列

trainset$setosa = trainset$Species == "setosa" trainset$virginica = trainset$Species == "virginica" trainset$versicolor = trainset$Species == "versicolor"

调用neuralnet函数创建一个包括3个隐藏层的神经网络,训练结果有可能随机发生变化,所以得到的结果可能不同。

network = neuralnet(versicolor + virginica + setosa ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,trainset,hidden = 3) network

输出构建好的神经网络模型的结果矩阵:

network$result.matrix

调用head函数,返回network模型的权重:

head(network$generalized.weights[[1]]) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] 1 5.5675563744 -13.281912587 5.2459904114 33.746710950 -2.906787334 7.031555044 -2.749901018 -17.73019634 -1.2379513313 3 2.2572675955 -5.169337136 2.1024817333 13.435152356 -6.790756552 16.418898544 -6.423334093 -41.41160529 -0.8305330849 4 1.0782516438 -2.228244922 0.9770173426 6.141688866 5.365276983 -12.958834166 5.073453551 32.70324652 -0.4777021984 7 1.6391253199 -3.762948576 1.5277698560 9.766544848 -30.359480987 73.406133715 -28.717075480 -185.14149381 -0.6949596908 9 0.7474439297 -1.011406101 0.6168865152 3.646846059 2.449187406 -5.900257155 2.314240535 14.91113491 -0.2230558823 10 2.1275289707 -4.854196745 1.9795980842 12.642310807 -7.821189126 18.909515514 -7.397923856 -47.69451202 -0.8023523247 [,10] [,11] [,12] 1 2.8875213587 -1.15900813378 -7.42835295690 3 1.7197601502 -0.75294535768 -4.73462401428 4 0.6411743053 -0.39366882722 -2.32476324087 7 1.4502392301 -0.63130620246 -3.97459288028 9 -0.7157660261 -0.06886041151 0.07691859618 10 1.6417515499 -0.72517139999 -4.55146663674

原理

神经网络是一种运算模型,由大量的节点(或称神经元)之间相互联接构成。每个节点代表一种特定的输出函数,称为激励函数(activation function)。每两个节点间的连接都代表一个对于通过该连接信号的加权值,称之为权重,这相当于人工神经网络的记忆。网络的输出则依网络的连接方式,权重值和激励函数的不同而不同。而网络自身通常都是对自然界某种算法或者函数的逼近,也可能是对一种逻辑策略的表达。 神经网络的优势: 1.可以检测因变量与自变量之间的非线性关系 2.可以利用算法并行化实现对大数据的高效训练 3.属于无参模型,能够避免参数估计过程中产生的错误 神经网络的不足: 1.容易陷入局部最优得到不到全局最优解 2.算法训练时间过长,容易导致过度适应

本例中用iris数据集,将数据分为两部分,增加三列判断各自的类型,然后训练模型,每层隐藏神经元个数 为3

$result.matrix 1 error 0.925073710768 reached.threshold 0.009456398022 steps 8331.00000000000

整个训练执行了8331步,结束条件为误差函数的绝对偏导数小于0.01,误差依然值的计算采用AIC准则

network$result.matrix 1 error 0.925073710768 reached.threshold 0.009456398022 steps 8331.000000000000 Intercept.to.1layhid1 44.658240484819 Sepal.Length.to.1layhid1 0.612522292642 Sepal.Width.to.1layhid1 7.041077031692 Petal.Length.to.1layhid1 -7.046937868734 Petal.Width.to.1layhid1 -20.061061593562 Intercept.to.1layhid2 1.414346465947 Sepal.Length.to.1layhid2 -1.036427753005 Sepal.Width.to.1layhid2 5.740216449543 Petal.Length.to.1layhid2 -1.346608377213 Petal.Width.to.1layhid2 -10.023922425287 Intercept.to.1layhid3 8.137322526858 Sepal.Length.to.1layhid3 0.596475351104 Sepal.Width.to.1layhid3 -1.443141539140 Petal.Length.to.1layhid3 0.564311602143 Petal.Width.to.1layhid3 3.638550013191 Intercept.to.versicolor 1.502838080418 1layhid.1.to.versicolor 1.027080776702 1layhid.2.to.versicolor -1.013084187861 1layhid.3.to.versicolor -1.517640879271 Intercept.to.virginica -0.215288202304 1layhid.1.to.virginica -1.024880544970 1layhid.2.to.virginica 0.010690840251 1layhid.3.to.virginica 1.229945000921 Intercept.to.setosa -0.594439453162 1layhid.1.to.setosa -0.001999340288 1layhid.2.to.setosa 1.002324704853 1layhid.3.to.setosa 0.594535559128

模型的权值范围在-20到40之间;第一层隐含网络的截距分别是1.69,1.41,24.40,该层有两个神经元,权重预测值分别为 Sepal.Length.to.1layhid1 0.612522292642 Sepal.Width.to.1layhid1 7.041077031692 Petal.Length.to.1layhid1 -7.046937868734 Petal.Width.to.1layhid1 -20.061061593562 最后,可以确定包括泛化权值在内的训练神经网络模型参数信息。本例可以由4个协变量(Sepal.Length、Sepal.Width、Petal.Length、Petal.Width)组合得到12个泛化权值分别对应三个类别(versicolor,virginica,setosa)

可视化由neuralnet包得到的神经网络模型

plot(network)

接下来,用gwplot函数可视化泛化权

library(neuralnet) par(mfrow=c(2,2)) gwplot(network,selected.covariate = "Petal.Width") gwplot(network,selected.covariate = "Petal.Length") gwplot(network,selected.covariate = "Sepal.Length") gwplot(network,selected.covariate = "Sepal.Width)

gwplot绘制泛化权值图 本节展示了如何绘制已经训练好的神经模型和每个属性的泛化权值。 神经网络模型的结构图,包括预测的权值、截距与训练过程的基本信息。 泛化权值图展示了四个协变量以versicolor的响应。 如果所示的泛化值都接近于0,则说明协变量对分类影响不大,然而总体方差大于1,则意味协变量对方差结果对方差存在非线性影响。

基于neuralnet包得到模型实现类标号预测

基于已经训练好的神经网络和测试数据集testset生成相关的预测概率矩阵

net.predict = compute(network,testset[-5])$net.result

通过概率最大的那一列得到其可能的类别,产生分类表

net.prediction = c("versicolor","virginica","setosa")[apply(net.predict, 1, which.max)] predict.table = table(testset$Species,net.prediction) predict.table net.prediction setosa versicolor virginica setosa 17 0 0 versicolor 0 14 0 virginica 0 1 14

调用classAgreement函数计算分类表

library(e1071) classAgreement(predict.table) classAgreement(predict.table) $diag [1] 0.9782609 $kappa [1] 0.9673063 $rand [1] 0.9729469 $crand [1] 0.9379167

调用confusio能Matrix评测预测性能

confusionMatrix(predict.table) Confusion Matrix and Statistics net.prediction setosa versicolor virginica setosa 17 0 0 versicolor 0 14 0 virginica 0 1 14 Overall Statistics Accuracy : 0.9783 95% CI : (0.8847, 0.9994) No Information Rate : 0.3696 P-Value [Acc > NIR] : < 2.2e-16 Kappa : 0.9673 Mcnemar's Test P-Value : NA Statistics by Class: Class: setosa Class: versicolor Class: virginica Sensitivity 1.0000 0.9333 1.0000 Specificity 1.0000 1.0000 0.9688 Pos Pred Value 1.0000 1.0000 0.9333 Neg Pred Value 1.0000 0.9688 1.0000 Prevalence 0.3696 0.3261 0.3043 Detection Rate 0.3696 0.3043 0.3043 Detection Prevalence 0.3696 0.3043 0.3261 Balanced Accuracy 1.0000 0.9667 0.9844

调用compute函数,根据训练好的神经网络和测试数据集生成得到的概率矩阵,为了实现概率矩阵到类标号的转换,还需要调用which.max函数找到每一个观测值对应概率最大的标号列作为该观测值的类标号。使用分类列表根据测试数据集本身的实际类标号和预测类标号产生分类矩阵。

转载请注明原文地址: https://www.6miu.com/read-21302.html

最新回复(0)