library("neuralnet")

Going to create a neural network to perform square rooting Type ?neuralnet for more information on the neuralnet library

Generate 50 random numbers uniformly distributed between 0 and 100 And store them as a dataframe

traininginput <-  as.data.frame(runif(50, min=0, max=100))
trainingoutput <- sqrt(traininginput)

Column bind the data into one variable

trainingdata <- cbind(traininginput,trainingoutput)
colnames(trainingdata) <- c("Input","Output")

Train the neural network Going to have 10 hidden layers Threshold is a numeric value specifying the threshold for the partial derivatives of the error function as stopping criteria.

net.sqrt <- neuralnet(Output~Input,trainingdata, hidden=10, threshold=0.01)
print(net.sqrt)
## $call
## neuralnet(formula = Output ~ Input, data = trainingdata, hidden = 10, 
##     threshold = 0.01)
## 
## $response
##         Output
## 1  5.851392717
## 2  8.440391463
## 3  8.638619613
## 4  7.943876919
## 5  6.698937325
## 6  6.904396383
## 7  6.619515757
## 8  4.261340241
## 9  6.467765351
## 10 8.564697284
## 11 9.270867002
## 12 4.355408356
## 13 7.304841019
## 14 0.945039966
## 15 7.167649701
## 16 7.361314205
## 17 5.799598240
## 18 7.410426302
## 19 9.671606009
## 20 8.804359378
## 21 2.104655064
## 22 2.840205043
## 23 8.003080648
## 24 8.553072940
## 25 8.518440947
## 26 9.115074222
## 27 8.467743276
## 28 9.825145959
## 29 8.210314475
## 30 7.149447512
## 31 1.693619472
## 32 2.199430369
## 33 3.214397352
## 34 5.808405826
## 35 9.745618738
## 36 9.076903633
## 37 9.031498766
## 38 6.887047976
## 39 9.309182529
## 40 7.484717453
## 41 1.402485498
## 42 4.159932760
## 43 7.429124889
## 44 4.801393049
## 45 8.931425903
## 46 3.751783226
## 47 8.202653052
## 48 7.780300562
## 49 9.266059234
## 50 9.233962926
## 
## $covariate
##                [,1]
##  [1,] 34.2387967277
##  [2,] 71.2402080419
##  [3,] 74.6257488150
##  [4,] 63.1051805103
##  [5,] 44.8757612845
##  [6,] 47.6706894115
##  [7,] 43.8179888530
##  [8,] 18.1590206455
##  [9,] 41.8319886317
## [10,] 73.3540395740
## [11,] 85.9489749651
## [12,] 18.9695819514
## [13,] 53.3607023070
## [14,]  0.8931005374
## [15,] 51.3752022292
## [16,] 54.1889468208
## [17,] 33.6353397463
## [18,] 54.9144179793
## [19,] 93.5399628012
## [20,] 77.5167440530
## [21,]  4.4295729371
## [22,]  8.0667646835
## [23,] 64.0492998529
## [24,] 73.1550567085
## [25,] 72.5638361648
## [26,] 83.0845780671
## [27,] 71.7026761966
## [28,] 96.5334931156
## [29,] 67.4092637841
## [30,] 51.1145997327
## [31,]  2.8683469165
## [32,]  4.8374939477
## [33,] 10.3323503397
## [34,] 33.7375782430
## [35,] 94.9770845938
## [36,] 82.3901795549
## [37,] 81.5679699648
## [38,] 47.4314298248
## [39,] 86.6608793614
## [40,] 56.0209953459
## [41,]  1.9669655710
## [42,] 17.3050405690
## [43,] 55.1918966230
## [44,] 23.0533752125
## [45,] 79.7703686636
## [46,] 14.0758773778
## [47,] 67.2835170990
## [48,] 60.5330768274
## [49,] 85.8598537277
## [50,] 85.2660713252
## 
## $model.list
## $model.list$response
## [1] "Output"
## 
## $model.list$variables
## [1] "Input"
## 
## 
## $err.fct
## function (x, y) 
## {
##     1/2 * (y - x)^2
## }
## <environment: 0x7fc40d39cd40>
## attr(,"type")
## [1] "sse"
## 
## $act.fct
## function (x) 
## {
##     1/(1 + exp(-x))
## }
## <environment: 0x7fc40d39cd40>
## attr(,"type")
## [1] "logistic"
## 
## $linear.output
## [1] TRUE
## 
## $data
##            Input      Output
## 1  34.2387967277 5.851392717
## 2  71.2402080419 8.440391463
## 3  74.6257488150 8.638619613
## 4  63.1051805103 7.943876919
## 5  44.8757612845 6.698937325
## 6  47.6706894115 6.904396383
## 7  43.8179888530 6.619515757
## 8  18.1590206455 4.261340241
## 9  41.8319886317 6.467765351
## 10 73.3540395740 8.564697284
## 11 85.9489749651 9.270867002
## 12 18.9695819514 4.355408356
## 13 53.3607023070 7.304841019
## 14  0.8931005374 0.945039966
## 15 51.3752022292 7.167649701
## 16 54.1889468208 7.361314205
## 17 33.6353397463 5.799598240
## 18 54.9144179793 7.410426302
## 19 93.5399628012 9.671606009
## 20 77.5167440530 8.804359378
## 21  4.4295729371 2.104655064
## 22  8.0667646835 2.840205043
## 23 64.0492998529 8.003080648
## 24 73.1550567085 8.553072940
## 25 72.5638361648 8.518440947
## 26 83.0845780671 9.115074222
## 27 71.7026761966 8.467743276
## 28 96.5334931156 9.825145959
## 29 67.4092637841 8.210314475
## 30 51.1145997327 7.149447512
## 31  2.8683469165 1.693619472
## 32  4.8374939477 2.199430369
## 33 10.3323503397 3.214397352
## 34 33.7375782430 5.808405826
## 35 94.9770845938 9.745618738
## 36 82.3901795549 9.076903633
## 37 81.5679699648 9.031498766
## 38 47.4314298248 6.887047976
## 39 86.6608793614 9.309182529
## 40 56.0209953459 7.484717453
## 41  1.9669655710 1.402485498
## 42 17.3050405690 4.159932760
## 43 55.1918966230 7.429124889
## 44 23.0533752125 4.801393049
## 45 79.7703686636 8.931425903
## 46 14.0758773778 3.751783226
## 47 67.2835170990 8.202653052
## 48 60.5330768274 7.780300562
## 49 85.8598537277 9.266059234
## 50 85.2660713252 9.233962926
## 
## $net.result
## $net.result[[1]]
##            [,1]
## 1  5.8545706809
## 2  8.4405329478
## 3  8.6411563088
## 4  7.9395240395
## 5  6.7018761901
## 6  6.9053484522
## 7  6.6231075777
## 8  4.2595745330
## 9  6.4723257588
## 10 8.5663504429
## 11 9.2756481901
## 12 4.3519014459
## 13 7.3018850658
## 14 0.9466689648
## 15 7.1659170147
## 16 7.3579222209
## 17 5.8022566481
## 18 7.4066942373
## 19 9.6655016562
## 20 8.8086663009
## 21 2.1067850945
## 22 2.8335081310
## 23 7.9990406055
## 24 8.5545848968
## 25 8.5195307232
## 26 9.1207961072
## 27 8.4682153845
## 28 9.8105072437
## 29 8.2079102171
## 30 7.1478909020
## 31 1.6989799904
## 32 2.1985598167
## 33 3.2174837924
## 34 5.8111562465
## 35 9.7357554404
## 36 9.0826544108
## 37 9.0371960980
## 38 6.8881781922
## 39 9.3134995167
## 40 7.4805482517
## 41 1.3964110464
## 42 4.1602167298
## 43 7.4252737087
## 44 4.7937233469
## 45 8.9367123476
## 46 3.7592176787
## 47 8.2001750211
## 48 7.7754821003
## 49 9.2708916191
## 50 9.2390984265
## 
## 
## $weights
## $weights[[1]]
## $weights[[1]][[1]]
##                [,1]           [,2]           [,3]           [,4]
## [1,] -0.06230735274 -1.51572110901 -0.09727195245  2.58518628586
## [2,] -0.73958607903  0.02809383609  0.06375553541 -0.03070444516
##                [,5]          [,6]          [,7]           [,8]
## [1,] -1.09746848283 -1.1536076414 -0.5256476305  1.47356334123
## [2,] -0.03224842736 -0.0335923939 -0.3411163694 -0.08721712414
##               [,9]          [,10]
## [1,] -1.5925064196 -1.26311160899
## [2,]  0.2836748451  0.02703760589
## 
## $weights[[1]][[2]]
##                [,1]
##  [1,]  2.6531858159
##  [2,] -1.7912904088
##  [3,]  2.9053698001
##  [4,]  2.8361244782
##  [5,] -2.6607234446
##  [6,] -0.8009277332
##  [7,] -0.7514631590
##  [8,] -0.2934013306
##  [9,] -1.1757085363
## [10,]  1.0272508127
## [11,]  2.7380450170
## 
## 
## 
## $startweights
## $startweights[[1]]
## $startweights[[1]][[1]]
##              [,1]          [,2]           [,3]          [,4]          [,5]
## [1,] -1.132092536 -0.8173301416  0.20311975886  0.9134799612 -1.2231103298
## [2,] -2.665907915  0.6318022294 -0.09094601702 -0.5893173963 -0.3975576421
##               [,6]          [,7]          [,8]          [,9]         [,10]
## [1,] -1.2517262785  1.1311361857  0.2380446694 -0.9311124457 -0.2951888200
## [2,] -0.4045253703 -0.5853325413 -1.7181460870  0.5153204893  0.9136720094
## 
## $startweights[[1]][[2]]
##                [,1]
##  [1,]  1.2547804557
##  [2,]  0.2548977783
##  [3,] -0.2660595186
##  [4,]  1.4428209510
##  [5,] -1.1161662745
##  [6,]  0.4540597153
##  [7,]  0.4318652721
##  [8,]  2.0410791466
##  [9,] -0.1438008738
## [10,] -0.4556588264
## [11,]  0.9827354616
## 
## 
## 
## $generalized.weights
## $generalized.weights[[1]]
##                [,1]
## 1  -0.0030351386889
## 2  -0.0009546152928
## 3  -0.0008868521133
## 4  -0.0011477023584
## 5  -0.0019361466875
## 6  -0.0017575903102
## 7  -0.0020126332815
## 8  -0.0082858880416
## 9  -0.0021717914257
## 10 -0.0009116163410
## 11 -0.0006949979979
## 12 -0.0077333227947
## 13 -0.0014754179544
## 14  9.1499206058903
## 15 -0.0015636301846
## 16 -0.0014413122756
## 17 -0.0031266460924
## 18 -0.0014126162924
## 19 -0.0005885123014
## 20 -0.0008333009630
## 21 -0.0987958003349
## 22 -0.0345780724823
## 23 -0.0011225200844
## 24 -0.0009155634424
## 25 -0.0009274111640
## 26 -0.0007393366287
## 27 -0.0009450004129
## 28 -0.0005504466328
## 29 -0.0010393342254
## 30 -0.0015759452888
## 31 -0.2543573322246
## 32 -0.0834913919335
## 33 -0.0223452954867
## 34 -0.0031108488479
## 35 -0.0005699780608
## 36 -0.0007504692973
## 37 -0.0007638552451
## 38 -0.0017716691198
## 39 -0.0006843552425
## 40 -0.0013708123137
## 41 -0.6727821959929
## 42 -0.0089530026998
## 43 -0.0014019168245
## 44 -0.0057237458151
## 45 -0.0007939266942
## 46 -0.0127194481055
## 47 -0.0010422841490
## 48 -0.0012210885163
## 49 -0.0006963405911
## 50 -0.0007053449148
## 
## 
## $result.matrix
##                                         1
## error                     0.0005333657894
## reached.threshold         0.0090926918928
## steps                  6064.0000000000000
## Intercept.to.1layhid1    -0.0623073527383
## Input.to.1layhid1        -0.7395860790341
## Intercept.to.1layhid2    -1.5157211090076
## Input.to.1layhid2         0.0280938360945
## Intercept.to.1layhid3    -0.0972719524479
## Input.to.1layhid3         0.0637555354117
## Intercept.to.1layhid4     2.5851862858564
## Input.to.1layhid4        -0.0307044451582
## Intercept.to.1layhid5    -1.0974684828258
## Input.to.1layhid5        -0.0322484273613
## Intercept.to.1layhid6    -1.1536076414094
## Input.to.1layhid6        -0.0335923938987
## Intercept.to.1layhid7    -0.5256476304757
## Input.to.1layhid7        -0.3411163693974
## Intercept.to.1layhid8     1.4735633412332
## Input.to.1layhid8        -0.0872171241387
## Intercept.to.1layhid9    -1.5925064196414
## Input.to.1layhid9         0.2836748450989
## Intercept.to.1layhid10   -1.2631116089948
## Input.to.1layhid10        0.0270376058877
## Intercept.to.Output       2.6531858158710
## 1layhid.1.to.Output      -1.7912904088013
## 1layhid.2.to.Output       2.9053698000991
## 1layhid.3.to.Output       2.8361244781609
## 1layhid.4.to.Output      -2.6607234445740
## 1layhid.5.to.Output      -0.8009277332056
## 1layhid.6.to.Output      -0.7514631589929
## 1layhid.7.to.Output      -0.2934013306500
## 1layhid.8.to.Output      -1.1757085362682
## 1layhid.9.to.Output       1.0272508127290
## 1layhid.10.to.Output      2.7380450169588
## 
## attr(,"class")
## [1] "nn"

Plot the neural network

plot(net.sqrt, rep = "best")

Test the neural network on some training data

testdata <- as.data.frame((1:10)^2) #Generate some squared numbers
net.results <- compute(net.sqrt, testdata) #Run them through the neural network

Lets see what properties net.sqrt has

ls(net.results)
## [1] "net.result" "neurons"

Lets see the results

print(net.results$net.result)
##              [,1]
##  [1,] 0.995651087
##  [2,] 2.004949735
##  [3,] 2.997236258
##  [4,] 4.003559121
##  [5,] 4.992983838
##  [6,] 6.004351125
##  [7,] 6.999959828
##  [8,] 7.995941860
##  [9,] 9.005608807
## [10,] 9.971903887

Lets display a better version of the results

cleanoutput <- cbind(testdata,sqrt(testdata),
                     as.data.frame(net.results$net.result))
colnames(cleanoutput) <- c("Input","Expected Output","Neural Net Output")
print(cleanoutput)
##    Input Expected Output Neural Net Output
## 1      1               1       0.995651087
## 2      4               2       2.004949735
## 3      9               3       2.997236258
## 4     16               4       4.003559121
## 5     25               5       4.992983838
## 6     36               6       6.004351125
## 7     49               7       6.999959828
## 8     64               8       7.995941860
## 9     81               9       9.005608807
## 10   100              10       9.971903887

sin function

Generate random data and the dependent variable

x <- sort(runif(50, min = 0, max = 4*pi))
y <- sin(x)

data <- cbind(x,y)

Create the neural network responsible for the sin function

library(neuralnet)
sin.nn <- neuralnet(y ~ x, data = data, hidden = 5, stepmax = 100000, learningrate = 10e-6,  
                    act.fct = 'logistic', err.fct = 'sse', rep = 5, lifesign = "minimal", 
                    linear.output = T)
## hidden: 5    thresh: 0.01    rep: 1/5    steps: stepmax  min thresh: 0.01599376894
## hidden: 5    thresh: 0.01    rep: 2/5    steps:    7943  error: 0.41295  time: 0.73 secs
## hidden: 5    thresh: 0.01    rep: 3/5    steps:   34702  error: 0.02068  time: 3.13 secs
## hidden: 5    thresh: 0.01    rep: 4/5    steps:    4603  error: 0.4004   time: 0.41 secs
## hidden: 5    thresh: 0.01    rep: 5/5    steps:    3582  error: 0.26375  time: 0.34 secs
## Warning: algorithm did not converge in 1 of 5 repetition(s) within the
## stepmax

Visualize the neural network

plot(sin.nn, rep = "best")

Generate data for the prediction of the using the neural net;

testdata<- as.data.frame(runif(10, min=0, max=(4*pi)))
testdata
##    runif(10, min = 0, max = (4 * pi))
## 1                         1.564816433
## 2                         4.692188270
## 3                        10.942269605
## 4                        11.432769193
## 5                         1.528565797
## 6                         4.277983023
## 7                         7.863112004
## 8                         3.233025098
## 9                         4.212822393
## 10                       11.584672483

Calculate the real value using the sin function

testdata.result <- sin(testdata)

Make the prediction

sin.nn.result <- compute(sin.nn, testdata)
sin.nn.result$net.result
##                 [,1]
##  [1,]  1.04026644587
##  [2,] -0.99122081475
##  [3,] -0.77154683268
##  [4,] -0.80702735515
##  [5,]  1.03394587608
##  [6,] -0.91997356615
##  [7,]  1.02031970677
##  [8,] -0.08226873533
##  [9,] -0.89463523567
## [10,] -0.81283835083

Compare with the real values:

better <- cbind(testdata, sin.nn.result$net.result, testdata.result, (sin.nn.result$net.result-testdata.result))
colnames(better) <- c("Input", "NN Result", "Result", "Error")

better
##           Input      NN Result         Result           Error
## 1   1.564816433  1.04026644587  0.99998212049  0.040284325379
## 2   4.692188270 -0.99122081475 -0.99979597259  0.008575157839
## 3  10.942269605 -0.77154683268 -0.99857964177  0.227032809091
## 4  11.432769193 -0.80702735515 -0.90594290260  0.098915547446
## 5   1.528565797  1.03394587608  0.99910842368  0.034837452408
## 6   4.277983023 -0.91997356615 -0.90712021799 -0.012853348159
## 7   7.863112004  1.02031970677  0.99995831846  0.020361388309
## 8   3.233025098 -0.08226873533 -0.09130510334  0.009036368006
## 9   4.212822393 -0.89463523567 -0.87779026852 -0.016844967152
## 10 11.584672483 -0.81283835083 -0.83144207031  0.018603719479

Calculate the RMSE:

library(Metrics)
rmse(better$Result, better$`NN Result`)
## [1] 0.08095028855

Plot the results:

plot(x,y)
plot(sin, 0, (4*pi), add=T)
x1 <- seq(0, 4*pi, by=0.1)
lines(x1, compute(sin.nn, data.frame(x=x1))$net.result, col="green")

A classification problem

Using the iris dataset

data(iris)
iris.dataset <- iris

Check what is inside the dataset:

head(iris.dataset)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

Change the dataset so we are able to predict classes:

iris.dataset$setosa <- iris.dataset$Species=="setosa"
iris.dataset$virginica = iris.dataset$Species == "virginica"
iris.dataset$versicolor = iris.dataset$Species == "versicolor"

Separate into train and test data:

train <- sample(x = nrow(iris.dataset), size = nrow(iris)*0.5)
train
##  [1] 116   3 137 124 100  48  28 123  99  54 129 128  96  11  97 115  53
## [18]   8 133  85  91  70  60  45 113 119  69 126 114  86 109 140  58  13
## [35]  77  57   7  61   9 111 141  39 120  98 104  88  83 106  20 147  74
## [52] 122  93  72  73 146   4  38   1  22 118 103  51  21  80  82  25  78
## [69] 148 143  14  50  23  84  40
iristrain <- iris.dataset[train,]
irisvalid <- iris.dataset[-train,]
print(nrow(iristrain))
## [1] 75
print(nrow(irisvalid))
## [1] 75

Build the Neural Network for the classification:

nn <- neuralnet(setosa+versicolor+virginica ~ Sepal.Length + Sepal.Width, data=iristrain, hidden=3,  
                rep = 2, err.fct = "ce", linear.output = F, lifesign = "minimal", stepmax = 1000000)
## hidden: 3    thresh: 0.01    rep: 1/2    steps:   77918  error: 54.96826 time: 9.41 secs
## hidden: 3    thresh: 0.01    rep: 2/2    steps:   53687  error: 54.24648 time: 6.25 secs

Let’s check the neural network that we just built

plot(nn, rep="best")

Let’s try to make the prediction:

comp <- compute(nn, irisvalid[-3:-8])
pred.weights <- comp$net.result
idx <- apply(pred.weights, 1, which.max)
pred <- c('setosa', 'versicolor', 'virginica')[idx]
table(pred, irisvalid$Species)
##             
## pred         setosa versicolor virginica
##   setosa         28          0         0
##   versicolor      1         13         5
##   virginica       0          9        19

AND operation

AND <- c(rep(0,3),1)
OR <- c(0,rep(1,3))
binary.data <- data.frame(expand.grid(c(0,1), c(0,1)), AND)
print(net <- neuralnet(AND~Var1+Var2, binary.data, hidden=0, rep=10, err.fct="ce", linear.output=FALSE))
## $call
## neuralnet(formula = AND ~ Var1 + Var2, data = binary.data, hidden = 0, 
##     rep = 10, err.fct = "ce", linear.output = FALSE)
## 
## $response
##   AND
## 1   0
## 2   0
## 3   0
## 4   1
## 
## $covariate
##      [,1] [,2]
## [1,]    0    0
## [2,]    1    0
## [3,]    0    1
## [4,]    1    1
## 
## $model.list
## $model.list$response
## [1] "AND"
## 
## $model.list$variables
## [1] "Var1" "Var2"
## 
## 
## $err.fct
## function (x, y) 
## {
##     -(y * log(x) + (1 - y) * log(1 - x))
## }
## <bytecode: 0x7fc40ffe5320>
## <environment: 0x7fc40f694320>
## attr(,"type")
## [1] "ce"
## 
## $act.fct
## function (x) 
## {
##     1/(1 + exp(-x))
## }
## <bytecode: 0x7fc411aad0b0>
## <environment: 0x7fc40f694320>
## attr(,"type")
## [1] "logistic"
## 
## $linear.output
## [1] FALSE
## 
## $data
##   Var1 Var2 AND
## 1    0    0   0
## 2    1    0   0
## 3    0    1   0
## 4    1    1   1
## 
## $net.result
## $net.result[[1]]
##                [,1]
## 1 0.000002695358491
## 2 0.012447835011968
## 3 0.012965173560536
## 4 0.983981385389474
## 
## $net.result[[2]]
##               [,1]
## 1 0.00000315688225
## 2 0.01310670401458
## 3 0.01365089349474
## 4 0.98311464468695
## 
## $net.result[[3]]
##                [,1]
## 1 0.000003460347827
## 2 0.013260919578620
## 3 0.014209021664229
## 4 0.982449843378523
## 
## $net.result[[4]]
##                [,1]
## 1 0.000001983238558
## 2 0.010703727902892
## 3 0.011850536824598
## 4 0.984945581601299
## 
## $net.result[[5]]
##                [,1]
## 1 0.000002596117268
## 2 0.012230765715951
## 3 0.012587604110748
## 4 0.983819270199875
## 
## $net.result[[6]]
##                [,1]
## 1 0.000008009074861
## 2 0.017574542535434
## 3 0.017074549365727
## 4 0.974874120014102
## 
## $net.result[[7]]
##               [,1]
## 1 0.00000745379384
## 2 0.01656711199861
## 3 0.01620580785285
## 4 0.97384223062722
## 
## $net.result[[8]]
##                [,1]
## 1 0.000002908306166
## 2 0.012616292497621
## 3 0.012785233809382
## 4 0.982728419604644
## 
## $net.result[[9]]
##               [,1]
## 1 0.00000477964004
## 2 0.01484648917102
## 3 0.01521870612542
## 4 0.97988978562439
## 
## $net.result[[10]]
##                [,1]
## 1 0.000007143257891
## 2 0.016544552786363
## 3 0.016578752614244
## 4 0.975431226286737
## 
## 
## $weights
## $weights[[1]]
## $weights[[1]][[1]]
##               [,1]
## [1,] -12.823976646
## [2,]   8.450294039
## [3,]   8.491538128
## 
## 
## $weights[[2]]
## $weights[[2]][[1]]
##               [,1]
## [1,] -12.665922490
## [2,]   8.344484422
## [3,]   8.385717112
## 
## 
## $weights[[3]]
## $weights[[3]][[1]]
##               [,1]
## [1,] -12.574137986
## [2,]   8.264553670
## [3,]   8.334570734
## 
## 
## $weights[[4]]
## $weights[[4]][[1]]
##               [,1]
## [1,] -13.130777431
## [2,]   8.604375660
## [3,]   8.707316634
## 
## 
## $weights[[5]]
## $weights[[5]][[1]]
##               [,1]
## [1,] -12.861490991
## [2,]   8.469996447
## [3,]   8.499115741
## 
## 
## $weights[[6]]
## $weights[[6]][[1]]
##               [,1]
## [1,] -11.734927292
## [2,]   7.711354230
## [3,]   7.681983027
## 
## 
## $weights[[7]]
## $weights[[7]][[1]]
##               [,1]
## [1,] -11.806779961
## [2,]   7.723150089
## [3,]   7.700732928
## 
## 
## $weights[[8]]
## $weights[[8]][[1]]
##               [,1]
## [1,] -12.747936811
## [2,]   8.387867120
## [3,]   8.401340097
## 
## 
## $weights[[9]]
## $weights[[9]][[1]]
##               [,1]
## [1,] -12.251140540
## [2,]   8.056106480
## [3,]   8.081246298
## 
## 
## $weights[[10]]
## $weights[[10]][[1]]
##               [,1]
## [1,] -11.849334455
## [2,]   7.764319030
## [3,]   7.766418807
## 
## 
## 
## $startweights
## $startweights[[1]]
## $startweights[[1]][[1]]
##               [,1]
## [1,] 0.07602335385
## [2,] 1.06109403863
## [3,] 1.17073812795
## 
## 
## $startweights[[2]]
## $startweights[[2]][[1]]
##              [,1]
## [1,] 0.6340775097
## [2,] 1.3552844223
## [3,] 0.4649171120
## 
## 
## $startweights[[3]]
## $startweights[[3]][[1]]
##                [,1]
## [1,] -0.40573798565
## [2,] -0.05624633045
## [3,] -1.84422926563
## 
## 
## $startweights[[4]]
## $startweights[[4]][[1]]
##               [,1]
## [1,]  1.1692225691
## [2,]  1.1116985397
## [3,] -0.4134833657
## 
## 
## $startweights[[5]]
## $startweights[[5]][[1]]
##               [,1]
## [1,] -0.8614909914
## [2,]  0.3175964468
## [3,] -0.5164842595
## 
## 
## $startweights[[6]]
## $startweights[[6]][[1]]
##                [,1]
## [1,] -0.63492729243
## [2,] -0.07264577009
## [3,]  0.22958302743
## 
## 
## $startweights[[7]]
## $startweights[[7]][[1]]
##               [,1]
## [1,] -0.2067799610
## [2,]  1.4339500887
## [3,] -0.9148670724
## 
## 
## $startweights[[8]]
## $startweights[[8]][[1]]
##              [,1]
## [1,] 0.2520631886
## [2,] 0.0986671202
## [3,] 0.4364629771
## 
## 
## $startweights[[9]]
## $startweights[[9]][[1]]
##                [,1]
## [1,] -0.05114054006
## [2,]  1.40022936031
## [3,] -1.13435370244
## 
## 
## $startweights[[10]]
## $startweights[[10]][[1]]
##               [,1]
## [1,] -0.4493344552
## [2,] -1.1144809705
## [3,]  1.6015416869
## 
## 
## 
## $generalized.weights
## $generalized.weights[[1]]
##             [,1]        [,2]
## [1,] 8.450294039 8.491538128
## [2,] 8.450294039 8.491538128
## [3,] 8.450294039 8.491538128
## [4,] 8.450294039 8.491538128
## 
## $generalized.weights[[2]]
##             [,1]        [,2]
## [1,] 8.344484422 8.385717112
## [2,] 8.344484422 8.385717112
## [3,] 8.344484422 8.385717112
## [4,] 8.344484422 8.385717112
## 
## $generalized.weights[[3]]
##            [,1]        [,2]
## [1,] 8.26455367 8.334570734
## [2,] 8.26455367 8.334570734
## [3,] 8.26455367 8.334570734
## [4,] 8.26455367 8.334570734
## 
## $generalized.weights[[4]]
##            [,1]        [,2]
## [1,] 8.60437566 8.707316634
## [2,] 8.60437566 8.707316634
## [3,] 8.60437566 8.707316634
## [4,] 8.60437566 8.707316634
## 
## $generalized.weights[[5]]
##             [,1]        [,2]
## [1,] 8.469996447 8.499115741
## [2,] 8.469996447 8.499115741
## [3,] 8.469996447 8.499115741
## [4,] 8.469996447 8.499115741
## 
## $generalized.weights[[6]]
##            [,1]        [,2]
## [1,] 7.71135423 7.681983027
## [2,] 7.71135423 7.681983027
## [3,] 7.71135423 7.681983027
## [4,] 7.71135423 7.681983027
## 
## $generalized.weights[[7]]
##             [,1]        [,2]
## [1,] 7.723150089 7.700732928
## [2,] 7.723150089 7.700732928
## [3,] 7.723150089 7.700732928
## [4,] 7.723150089 7.700732928
## 
## $generalized.weights[[8]]
##            [,1]        [,2]
## [1,] 8.38786712 8.401340097
## [2,] 8.38786712 8.401340097
## [3,] 8.38786712 8.401340097
## [4,] 8.38786712 8.401340097
## 
## $generalized.weights[[9]]
##            [,1]        [,2]
## [1,] 8.05610648 8.081246298
## [2,] 8.05610648 8.081246298
## [3,] 8.05610648 8.081246298
## [4,] 8.05610648 8.081246298
## 
## $generalized.weights[[10]]
##            [,1]        [,2]
## [1,] 7.76431903 7.766418807
## [2,] 7.76431903 7.766418807
## [3,] 7.76431903 7.766418807
## [4,] 7.76431903 7.766418807
## 
## 
## $result.matrix
##                                 1                2                3
## error               0.04172690808   0.043970973653   0.045370013968
## reached.threshold   0.00939708932   0.009875399079   0.009923244969
## steps             130.00000000000 134.000000000000 126.000000000000
## Intercept.to.AND  -12.82397664615 -12.665922490326 -12.574137985647
## Var1.to.AND         8.45029403863   8.344484422316   8.264553669545
## Var2.to.AND         8.49153812795   8.385717111974   8.334570734373
##                                  4                5               6
## error               0.037853608723   0.041289339635   0.06040774182
## reached.threshold   0.007501829567   0.008640236144   0.00953122099
## steps             144.000000000000 121.000000000000 112.00000000000
## Intercept.to.AND  -13.130777430908 -12.861490991386 -11.73492729243
## Var1.to.AND         8.604375659738   8.469996446841   7.71135422991
## Var2.to.AND         8.707316634293   8.499115740543   7.68198302743
##                                 7                8                9
## error               0.05955786269   0.042989604414   0.050613457477
## reached.threshold   0.00995196152   0.008132854218   0.009959760561
## steps             117.00000000000 131.000000000000 123.000000000000
## Intercept.to.AND  -11.80677996105 -12.747936811436 -12.251140540056
## Var1.to.AND         7.72315008875   8.387867120198   8.056106480307
## Var2.to.AND         7.70073292762   8.401340097113   8.081246297563
##                                 10
## error               0.058283426286
## reached.threshold   0.008561674945
## steps             115.000000000000
## Intercept.to.AND  -11.849334455193
## Var1.to.AND         7.764319029501
## Var2.to.AND         7.766418806916
## 
## attr(,"class")
## [1] "nn"

Now to validate the predictions:

input <- data.frame(expand.grid(c(0,1), c(0,1)))
net.results <- compute(net, input)
cbind(round(net.results$net.result), AND)
##        AND
## [1,] 0   0
## [2,] 0   0
## [3,] 0   0
## [4,] 1   1