library(clusterGeneration)
seed.val<-2
set.seed(seed.val)
num.vars<-8
num.obs<-1000
这是学习了一位大神的文章,原文地址https://beckmw.wordpress.com/2013/11/14/visualizing-neural-networks-in-r-update/
十分感谢!总共讲了三种神经网络的包,neuralnet、nnet、RSNNS包,首先自己去安装这些包。
通常来说,选用一些数据建立神经网络,数据集包括输入层和输出层两个变量。最后的数据集是一个有着所有变量的数据框,并且输入层和输出层分开。
#input variables
cov.mat<-genPositiveDefMat(num.vars,covMethod = c("unifcorrmat"))$Sigma
rand.vars<-mvrnorm(num.obs,rep(0,num.vars),Sigma = cov.mat)
#output variables
parms<-runif(num.vars,-10,10)
y1<-rand.vars %*% matrix(parms)+rnorm(num.obs,sd=20)
parms2<-runif(num.vars,-10,10)
y2<-rand.vars %*% matrix(parms2)+rnorm(num.obs,sd=20)
#final datasets
rand.vars<-data.frame(rand.vars)
resp<-data.frame(y1,y2)
names(resp)<-c('Y1','Y2')
dat.in<-data.frame(resp,rand.vars)
用这个数据集应用于不同神经网络的包中
#nnet function from nnet package library(nnet) set.seed(seed.val) mod1<-nnet(rand.vars,resp,data=dat.in,size=10,linout=T)
# weights: 112 initial value 3108050.610739 iter 10 value 1327058.097107 iter 20 value 1195359.515760 iter 30 value 1161119.267049 iter 40 value 1148959.017226 iter 50 value 1137711.703212 iter 60 value 1129485.784260 iter 70 value 1124003.301170 iter 80 value 1120064.439320 iter 90 value 1112401.065836 iter 100 value 1104747.216182 final value 1104747.216182 stopped after 100 iterations
#neuralnet function from neuralnet package,notice use of only one responselibrary(neuralnet)form.in<-as.formula('Y1~X1+X2+X3+X4+X5+X6+X7+X8')set.seed(seed.val)mod2<-neuralnet(form.in,data=dat.in,hidden=10)#mlp function from RSNNS packagelibrary(RSNNS)set.seed(seed.val)mod3<-mlp(rand.vars,resp,size=10,linOut=T) 简单来说,每个包都能创建神经网络,但有几个不同,首先就是输入变量的函数不同。nnet函数可以是x、y的数据框或者公式,neuralnet函数智能使用公式,mlp函数只能使用数据框作为输入。neuralnet函数不能对多个反应变量进行建模,除非反应变量是对每个结果使用一个节点的分类变量。除此之外,neuralnet默认的输出是联想的,这与那两个函数不同。
#import the functin from Github library(devtools) source_url('https://gist.githubusercontent.com/fawda123/7471137/raw/466c1474d0a505ff044412703516c34f1a4684a5/nnet_plot_update.r') #plot each model plot.nnet(mod1)
> plot.nnet(mod2) Error in data.frame(wts, row.nms) : arguments imply differing number of rows: 0, 9 Called from: data.frame(wts, row.nms)
plot.nnet(mod3)
mlp函数并不显示偏置层,具体的参数含义参考原文。
绘图函数也会有任意权向量,而不是一个特定的模型对象。如果这个选择被使用,struct也必须被包括。最简单的画图就是将输入权值作为数值向量,包括偏置层,在这里使用输入权值绘制mod1
wts.in<-mod1$wts struct<-mod1$n plot.nnet(wts.in,struct=struct)
wts是一个数值向量,其长度等于预期给定的长度。8个输入,10个隐藏,2个输出,100个权值,2个偏置。
如果假定不用R来创作,就输入mod.in来更新绘图函数。也标记struct。大的权值线粗,+black,-grey mod.in<-c(13.12,1.49,0.16,-0.11,-0.19,-0.16,0.56,-0.52,0.81) struct<-c(2,2,1)#two inputs,two hidden,one output plot.nnet(mod.in,struct = struct)
> fact<-factor(sample(c('a','b','c'),size=num.obs,replace=T)) > form.in<-formula('cbind(Y2,Y1)~X1+X2+X3+fact') > mod5<-nnet(form.in,data=cbind(dat.in,fact),size=10,linout=T) # weights: 82 initial value 3132856.842214 iter 10 value 1664693.390509 iter 20 value 1484802.020256 iter 30 value 1440543.876651 iter 40 value 1427461.978571 iter 50 value 1422604.307325 iter 60 value 1408964.085040 iter 70 value 1399782.468372 iter 80 value 1389515.027463 iter 90 value 1379794.255889 iter 100 value 1346834.265977 final value 1346834.265977 stopped after 100 iterations > plot.nnet(mod5,nid=T)
#neural net with three hidden layers, 9, 11, and 8 nodes in each mod<-mlp(rand.vars, resp, size=c(9,11,8),linOut=T) par(mar=numeric(4),family='serif') plot.nnet(mod)
使用neuralnet进行二元预测和分类输出 library(neuralnet) #response AND<-c(rep(0,7),1) OR<-c(0,rep(1,7)) #response with predictors binary.data<-data.frame(expand.grid(c(0,1),c(0,1),c(0,1)),AND,OR) #model net<-neuralnet(AND+OR~Var1+Var2+Var3,binary.data, hidden=c(6,12,8),rep=10,err.fct = "ce",linear.output = F) #plot output par(mar=numeric(4),family='serif') plot.nnet(net)
circle.col是颜色向量,第一层改变颜色。 #example showing use of separate colors for input layer #color based on relative importance using 'gar.fun' ## #create input data seed.val<-3 set.seed(seed.val) num.vars<-8 num.obs<-1000 #input variables library(clusterGeneration) cov.mat<-genPositiveDefMat(num.vars,covMethod=c("unifcorrmat"))$Sigma rand.vars<-mvrnorm(num.obs,rep(0,num.vars),Sigma=cov.mat) #output variables parms<-runif(num.vars,-10,10) y1<-rand.vars %*% matrix(parms) + rnorm(num.obs,sd=20) #final datasets rand.vars<-data.frame(rand.vars) resp<-data.frame(y1) names(resp)<-'Y1' dat.in<-data.frame(resp,rand.vars) ## #create model library(nnet) mod1<-nnet(rand.vars,resp,data=dat.in,size=10,linout=T) ## #relative importance function library(devtools) source_url('https://gist.githubusercontent.com/fawda123/6206737/raw/2e1bc9cbc48d1a56d2a79dd1d33f414213f5f1b1/gar_fun.r') #relative importance of input variables for Y1 rel.imp<-gar.fun('Y1',mod1,bar.plot=F)$rel.imp #color vector based on relative importance of input values cols<-colorRampPalette(c('green','red'))(num.vars)[rank(rel.imp)] ## #plotting function source_url('https://gist.githubusercontent.com/fawda123/7471137/raw/466c1474d0a505ff044412703516c34f1a4684a5/nnet_plot_update.r') #plot model with new color vector #separate colors for input vectors using a list for 'circle.col' plot(mod1,circle.col=list(cols,'lightblue'))