Home > front end >  Output of the neural network's parameters in the neuralnet package in R
Output of the neural network's parameters in the neuralnet package in R

Time:10-19

I am new to R, and I am trying to execute some simple code, which uses the neuralnet package with the built-in dataset Iris.

library(neuralnet)
data(iris)
#Add a "fake" class to allow for all factors
levels(iris$Species) <- c(levels(iris$Species),"fake")
#Relevel to make the fake class the factor
iris$Species <- relevel(iris$Species,ref = "fake")
#Create dummy variables and remove the intercept
iris_multinom <- model.matrix(~Species Sepal.Length 
                                Petal.Length Petal.Width,
                              data=iris)[,-1]

colnames(iris_multinom)[1:3] <- c("setosa","versicolor","virginica")


nn_multi <- neuralnet(setosa versicolor virginica~
                        Sepal.Length Petal.Length Petal.Width,
                      data=iris_multinom,hidden=5,lifesign = "minimal",
                      stepmax = 1e 05,rep=10)

print(nn_multi)

However, when I print the neural network (print(nn_multi)), I get a lot of information, which is in this format:

enter image description here

However, I would like my output to have this format (this is an example with 5 repetitions):

enter image description here

Any help would be highly appreciated.

CodePudding user response:

The results are saved in the nn_multi$result.matrix. You could select the first three rows of the matrix which shows the error, threshold, and steps. The get your desired result you need to transpose the matrix first like this:

devtools::install_github("bips-hb/neuralnet")
library(neuralnet)
data(iris)
#Add a "fake" class to allow for all factors
levels(iris$Species) <- c(levels(iris$Species),"fake")
#Relevel to make the fake class the factor
iris$Species <- relevel(iris$Species,ref = "fake")
#Create dummy variables and remove the intercept
iris_multinom <- model.matrix(~Species Sepal.Length 
                                Petal.Length Petal.Width,
                              data=iris)[,-1]

colnames(iris_multinom)[1:3] <- c("setosa","versicolor","virginica")


nn_multi <- neuralnet(setosa versicolor virginica~
                        Sepal.Length Petal.Length Petal.Width,
                      data=iris_multinom,hidden=5,lifesign = "minimal",
                      stepmax = 1e 05,rep=10)
#> hidden: 5 thresh: 0.01 rep: 1/10 steps:
#> stepmax  min thresh: 0.0111100846918453
#> hidden: 5    thresh: 0.01    rep:  2/10    steps: stepmax    min thresh: 0.0101632025801019
#> hidden: 5    thresh: 0.01    rep:  3/10    steps: stepmax    min thresh: 0.0100642864690584
#> hidden: 5    thresh: 0.01    rep:  4/10    steps:   51616    error: 1.92804  time: 6.13 secs
#> hidden: 5    thresh: 0.01    rep:  5/10    steps:   28311    error: 2.2231   time: 3.56 secs
#> hidden: 5    thresh: 0.01    rep:  6/10    steps:   38277    error: 2.00322  time: 4.69 secs
#> hidden: 5    thresh: 0.01    rep:  7/10    steps: stepmax    min thresh: 0.010559918220807
#> hidden: 5    thresh: 0.01    rep:  8/10    steps:   82787    error: 1.75016  time: 10.13 secs
#> hidden: 5    thresh: 0.01    rep:  9/10    steps:   40516    error: 1.91582  time: 4.31 secs
#> hidden: 5    thresh: 0.01    rep: 10/10    steps:   50714    error: 2.09759  time: 5.41 secs
#> Warning: Algorithm did not converge in 4 of 10 repetition(s) within the stepmax.

#Results
t(nn_multi$result.matrix[1:3,])
#>         error reached.threshold steps
#> [1,] 1.928045       0.009545829 51616
#> [2,] 2.223097       0.009947644 28311
#> [3,] 2.003221       0.009738232 38277
#> [4,] 1.750163       0.009371444 82787
#> [5,] 1.915816       0.009579309 40516
#> [6,] 2.097587       0.009220242 50714

Created on 2022-10-18 with reprex v2.0.2

  • Related