Plotting neural network model from nnet package R cran
Asked Answered
C

1

7

Is there any package or other software to plot neural network models from the nnet package cran.

I trained a neural network model (3 inputs and 1 output) using nnet and Rattle:

crs$nnet <- nnet(as.factor(Target) ~ .,
                 data=crs$dataset[crs$sample,c(crs$input, crs$target)],
                 size=10, skip=TRUE, MaxNWts=10000, 
                 trace=FALSE, maxit=100)

This was the summary of the model :

Neural Network build options: skip-layer connections; entropy fitting.

In the following table:
   b  represents the bias associated with a node
   h1 represents hidden layer node 1
   i1 represents input node 1 (i.e., input variable 1)
   o  represents the output node

Weights for node h1:
 b->h1 i1->h1 i2->h1 i3->h1 
 -0.66   0.15   0.24  -0.31 

Weights for node h2:
 b->h2 i1->h2 i2->h2 i3->h2 
 -0.62   1.32   1.16   0.24 

Weights for node h3:
 b->h3 i1->h3 i2->h3 i3->h3 
 13.59 -10.44   0.78  -6.46 

Weights for node h4:
 b->h4 i1->h4 i2->h4 i3->h4 
  0.16  -0.46   2.09   0.23 

Weights for node h5:
 b->h5 i1->h5 i2->h5 i3->h5 
 -0.16  -0.55  -0.52   0.25 

Weights for node h6:
 b->h6 i1->h6 i2->h6 i3->h6 
 -1.49  -7.07   1.67  -0.21 

Weights for node h7:
 b->h7 i1->h7 i2->h7 i3->h7 
  2.00   1.67  -5.51   0.66 

Weights for node h8:
 b->h8 i1->h8 i2->h8 i3->h8 
  0.56   0.44   0.41   0.51 

Weights for node h9:
 b->h9 i1->h9 i2->h9 i3->h9 
  0.38   0.21   0.47  -0.41 

Weights for node h10:
 b->h10 i1->h10 i2->h10 i3->h10 
   0.53   -1.60    4.79   -0.04 

Weights for node o:
  b->o  h1->o  h2->o  h3->o  h4->o  h5->o  h6->o  h7->o  h8->o  h9->o 
  1.08   1.83   0.17   1.21   1.21   0.64  -0.13  -8.37   0.98   2.03 
h10->o  i1->o  i2->o  i3->o 
 -8.41   0.03   0.00   0.01 

Thank you very much

Cowskin answered 16/9, 2012 at 14:48 Comment(2)
If you do not name your function and perhaps even include data, you are leaving this problem only for people who will recognize the style of output and know how to construct a similar case. That's probably a much smaller audience than might take interest than if you included more details.Hedva
@user1594303: I would rather remove the summary of your model and the details on how you trained your network. I think it does not add useful information to the question and it only distracts from the main point: how to plot a NN model with R.Spectrogram
S
13

Thanks to "R is my friend", now you can easily do that:

http://beckmw.wordpress.com/2013/11/14/visualizing-neural-networks-in-r-update/

I forked the function made by fawda123 and added the possibility to change border color as well. My changes are available here:

https://gist.github.com/Peque/41a9e20d6687f2f3108d

Example with custom border color (black):

Neural network plot example

Here is the complete code of the example above (notice you will need packages 'clusterGeneration', 'nnet' and 'devtools'):

library(clusterGeneration)
library(nnet)
library(devtools)

seed.val<-2
set.seed(seed.val)

num.vars<-8
num.obs<-1000

#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
set.seed(seed.val)
mod1<-nnet(rand.vars,resp,data=dat.in,size=10,linout=T)

#import the function from Github
source_url('https://gist.githubusercontent.com/Peque/41a9e20d6687f2f3108d/raw/85e14f3a292e126f1454864427e3a189c2fe33f3/nnet_plot_update.r')

#plot each model
pdf('./nn-example.pdf', width = 7, height = 7)
plot.nnet(mod1, alpha.val = 0.5, circle.col = list('lightgray', 'white'), bord.col = 'black')
dev.off()
Spectrogram answered 5/6, 2014 at 10:26 Comment(2)
Hi @Peque, please how can I use nnet using tanh activation function? my output is Y1, Y2, Y3, Y4.Foehn
@DevEx: you should open a new question for that if you didn't find it yet. ;-)Spectrogram

© 2022 - 2024 — McMap. All rights reserved.