Visualization of neural network with R

2018/11/14

Neural network visualization package in R

The neuralnet function can visualize the calculation graph by the plot () function as standard. Note below how to visualize the computational graph when using other neural network packages that do not have features like the neuralnet function.

  • plot.nn function
  • plotnet function

Preparation

Sample data uses iris

Creating a learner

d = iris d $ Species <-as.factor (d $ Species) #train_test_split set.seed (0) sample <-sample.int (n = nrow (d), size = floor (0.80 * nrow (d)), replace = F) train <-d [sample,] test <-d [-sample,] summary (train) #nnet library (nnet) nn1 = nnet (Species ~., Size = 5, data = train) pred_nn1 <- predict (nn1, test, type = "class") table (test $ Species, pred_nn1)

Visualize nnet

In each case, the color indicates the positive or negative, and the thickness indicates the magnitude of the numerical value.

plot.nn function

source ("http://hosho.ees.hokudai.ac.jp/~kubo/log/2007/img07/plot.nn.txt") plot.nn (nn1)

 


plot.nnet function

install.packages ("NeuralNetTools") library (NeuralNetTools) plotnet (nn1)

 

 

By the way, in the neuralnet function

library (caret) tmp <-dummyVars (~., Data = train) dummy <-as.data.frame (predict (tmp, train)) library ("neuralnet") f = Species.setosa + Species.versicolor + Species. virginica ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width nn2 <-neuralnet (formula = f, data = dummy) plot (nn2)

 

When there are many variables, it is easier to see the visualization with the horizontal plotnet function. The plotnet function is easy to use because it can visualize not only nnet but also neural networks created with RSNNS and caret and has a wide range of applications.