I Solutions ch. 10 - Artificial neural networks

Solutions to exercises of chapter 10.

I.1 Exercise 1

library("neuralnet")
 
#To create a neural network to perform square root

#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
#Will 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.519227239
## 2  7.902907242
## 3  7.494675786
## 4  9.646949579
## 5  6.579184589
## 6  4.582635404
## 7  1.777047916
## 8  6.882721190
## 9  8.519532611
## 10 8.550083802
## 11 8.925858724
## 12 5.758551186
## 13 2.751831781
## 14 4.532574265
## 15 7.319822149
## 16 9.230921316
## 17 9.266570062
## 18 6.689722959
## 19 8.184185473
## 20 3.202338849
## 21 6.225150465
## 22 4.912382788
## 23 8.220251152
## 24 9.037634872
## 25 8.678084647
## 26 6.617461345
## 27 8.059943112
## 28 6.600743688
## 29 6.815436656
## 30 8.735389548
## 31 8.453906284
## 32 8.761491596
## 33 9.588735514
## 34 7.422184235
## 35 7.834412718
## 36 5.272249390
## 37 5.898309070
## 38 9.158202848
## 39 6.761068055
## 40 4.782685313
## 41 9.702739429
## 42 8.859197856
## 43 1.470391560
## 44 3.636398481
## 45 9.314476130
## 46 5.423897050
## 47 2.673099228
## 48 7.711626079
## 49 5.935216632
## 50 7.646074653
## 
## $covariate
##               [,1]
##  [1,] 30.461869319
##  [2,] 62.455942878
##  [3,] 56.170165143
##  [4,] 93.063636171
##  [5,] 43.285669852
##  [6,] 21.000547241
##  [7,]  3.157899296
##  [8,] 47.371850978
##  [9,] 72.582435911
## [10,] 73.103933013
## [11,] 79.670953960
## [12,] 33.160911757
## [13,]  7.572578150
## [14,] 20.544229471
## [15,] 53.579796292
## [16,] 85.209908336
## [17,] 85.869320715
## [18,] 44.752393267
## [19,] 66.980891861
## [20,] 10.254974104
## [21,] 38.752498315
## [22,] 24.131504656
## [23,] 67.572529009
## [24,] 81.678844080
## [25,] 75.309153134
## [26,] 43.790794653
## [27,] 64.962682966
## [28,] 43.569817231
## [29,] 46.450176812
## [30,] 76.307030558
## [31,] 71.468531457
## [32,] 76.763734990
## [33,] 91.943848762
## [34,] 55.088818818
## [35,] 61.378022633
## [36,] 27.796613635
## [37,] 34.790049889
## [38,] 83.872679411
## [39,] 45.712041249
## [40,] 22.874078806
## [41,] 94.143152423
## [42,] 78.485386656
## [43,]  2.162051341
## [44,] 13.223393913
## [45,] 86.759465584
## [46,] 29.418659210
## [47,]  7.145459484
## [48,] 59.469176782
## [49,] 35.226796474
## [50,] 58.462457592
## 
## $model.list
## $model.list$response
## [1] "Output"
## 
## $model.list$variables
## [1] "Input"
## 
## 
## $err.fct
## function (x, y) 
## {
##     1/2 * (y - x)^2
## }
## <bytecode: 0x20a8170>
## <environment: 0x20a9478>
## attr(,"type")
## [1] "sse"
## 
## $act.fct
## function (x) 
## {
##     1/(1 + exp(-x))
## }
## <bytecode: 0x20b84c8>
## <environment: 0x20a9478>
## attr(,"type")
## [1] "logistic"
## 
## $linear.output
## [1] TRUE
## 
## $data
##           Input      Output
## 1  30.461869319 5.519227239
## 2  62.455942878 7.902907242
## 3  56.170165143 7.494675786
## 4  93.063636171 9.646949579
## 5  43.285669852 6.579184589
## 6  21.000547241 4.582635404
## 7   3.157899296 1.777047916
## 8  47.371850978 6.882721190
## 9  72.582435911 8.519532611
## 10 73.103933013 8.550083802
## 11 79.670953960 8.925858724
## 12 33.160911757 5.758551186
## 13  7.572578150 2.751831781
## 14 20.544229471 4.532574265
## 15 53.579796292 7.319822149
## 16 85.209908336 9.230921316
## 17 85.869320715 9.266570062
## 18 44.752393267 6.689722959
## 19 66.980891861 8.184185473
## 20 10.254974104 3.202338849
## 21 38.752498315 6.225150465
## 22 24.131504656 4.912382788
## 23 67.572529009 8.220251152
## 24 81.678844080 9.037634872
## 25 75.309153134 8.678084647
## 26 43.790794653 6.617461345
## 27 64.962682966 8.059943112
## 28 43.569817231 6.600743688
## 29 46.450176812 6.815436656
## 30 76.307030558 8.735389548
## 31 71.468531457 8.453906284
## 32 76.763734990 8.761491596
## 33 91.943848762 9.588735514
## 34 55.088818818 7.422184235
## 35 61.378022633 7.834412718
## 36 27.796613635 5.272249390
## 37 34.790049889 5.898309070
## 38 83.872679411 9.158202848
## 39 45.712041249 6.761068055
## 40 22.874078806 4.782685313
## 41 94.143152423 9.702739429
## 42 78.485386656 8.859197856
## 43  2.162051341 1.470391560
## 44 13.223393913 3.636398481
## 45 86.759465584 9.314476130
## 46 29.418659210 5.423897050
## 47  7.145459484 2.673099228
## 48 59.469176782 7.711626079
## 49 35.226796474 5.935216632
## 50 58.462457592 7.646074653
## 
## $net.result
## $net.result[[1]]
##           [,1]
## 1  5.517169398
## 2  7.899573434
## 3  7.493741496
## 4  9.641570856
## 5  6.581765992
## 6  4.583214348
## 7  1.777910876
## 8  6.885163582
## 9  8.517989976
## 10 8.548846798
## 11 8.929065970
## 12 5.757436258
## 13 2.750873399
## 14 4.533490638
## 15 7.320117610
## 16 9.235886561
## 17 9.271398693
## 18 6.692393931
## 19 8.180608483
## 20 3.202105919
## 21 6.226534439
## 22 4.910962181
## 23 8.216767394
## 24 9.041935875
## 25 8.678284493
## 26 6.620091375
## 27 8.056272299
## 28 6.603354730
## 29 6.818014959
## 30 8.736289119
## 31 8.451767666
## 32 8.762715272
## 33 9.586241401
## 34 7.421766922
## 35 7.831359571
## 36 5.269870092
## 37 5.897923979
## 38 9.163163806
## 39 6.763712266
## 40 4.781970104
## 41 9.694023815
## 42 8.861628161
## 43 1.469962493
## 44 3.638959940
## 45 9.318954236
## 46 5.421624685
## 47 2.672485693
## 48 7.709231672
## 49 5.935035256
## 50 7.644093624
## 
## 
## $weights
## $weights[[1]]
## $weights[[1]][[1]]
##               [,1]          [,2]           [,3]          [,4]
## [1,] -1.0143442592 -0.3108134549  4.48147360541 -0.6803028026
## [2,] -0.5251534515  0.1793417287 -0.05084709319  0.0428372541
##                [,5]          [,6]          [,7]          [,8]
## [1,]  2.03882035742 0.43572772952 0.31855674101 0.13532461807
## [2,] -0.04266953073 0.04422025402 0.04759056023 0.04796222314
##                [,9]         [,10]
## [1,] -0.43644174351 1.53363355573
## [2,] -0.03847716565 0.03463005836
## 
## $weights[[1]][[2]]
##                [,1]
##  [1,]  0.9323227234
##  [2,] -2.9985588758
##  [3,]  1.8218821789
##  [4,] -2.7774092409
##  [5,]  3.5400471485
##  [6,] -1.2357288637
##  [7,]  1.2339769529
##  [8,]  1.6586583299
##  [9,]  1.2611211125
## [10,] -1.7699072451
## [11,]  0.7682671344
## 
## 
## 
## $startweights
## $startweights[[1]]
## $startweights[[1]][[1]]
##               [,1]          [,2]         [,3]          [,4]          [,5]
## [1,] -1.0151710299 -0.5342538799  1.217319457 -0.3785569589  0.5730526014
## [2,] -0.8857900817  2.2311265794 -1.094952553 -0.0405489265 -0.3162993204
##              [,6]          [,7]          [,8]         [,9]        [,10]
## [1,] 1.3251399953  2.4838023968  0.8135800074 -1.657374847 1.9515712911
## [2,] 0.4585721091 -0.1081072498 -0.1057220534 -1.161110638 0.4302669937
## 
## $startweights[[1]][[2]]
##                  [,1]
##  [1,] -0.179535625899
##  [2,]  1.421304912112
##  [3,]  0.710020721636
##  [4,] -1.330598721355
##  [5,]  0.701840030541
##  [6,] -0.215225888872
##  [7,] -0.030688419065
##  [8,]  0.546797615920
##  [9,] -0.004226540943
## [10,] -0.555436985997
## [11,] -0.040842801160
## 
## 
## 
## $generalized.weights
## $generalized.weights[[1]]
##                [,1]
## 1  -0.0036452136764
## 2  -0.0011566625080
## 3  -0.0013611829827
## 4  -0.0005880788349
## 5  -0.0020717546346
## 6  -0.0065989122131
## 7  -0.2053310243372
## 8  -0.0017884073486
## 9  -0.0009253764487
## 10 -0.0009154852311
## 11 -0.0008000234895
## 12 -0.0031854391135
## 13 -0.0375912464845
## 14 -0.0068400650122
## 15 -0.0014664847729
## 16 -0.0007103415454
## 17 -0.0006999048278
## 18 -0.0019621287100
## 19 -0.0010422839336
## 20 -0.0222724885184
## 21 -0.0024801316225
## 22 -0.0052737797123
## 23 -0.0010288359109
## 24 -0.0007670035668
## 25 -0.0008750398564
## 26 -0.0020329117029
## 27 -0.0010905996196
## 28 -0.0020497595097
## 29 -0.0018465417144
## 30 -0.0008573725047
## 31 -0.0009469857961
## 32 -0.0008493996326
## 33 -0.0006052509646
## 34 -0.0014033963232
## 35 -0.0011874502495
## 36 -0.0042133725530
## 37 -0.0029505615258
## 38 -0.0007316310210
## 39 -0.0018953883305
## 40 -0.0057460875669
## 41 -0.0005716168802
## 42 -0.0008198997544
## 43 -0.4881119168066
## 44 -0.0143921626508
## 45 -0.0006858731315
## 46 -0.0038519115348
## 47 -0.0416313937285
## 48 -0.0012459860555
## 49 -0.0028921774440
## 50 -0.0012791282179
## 
## 
## $result.matrix
##                                         1
## error                      0.000198491342
## reached.threshold          0.009766732701
## steps                  18838.000000000000
## Intercept.to.1layhid1     -1.014344259179
## Input.to.1layhid1         -0.525153451492
## Intercept.to.1layhid2     -0.310813454855
## Input.to.1layhid2          0.179341728698
## Intercept.to.1layhid3      4.481473605407
## Input.to.1layhid3         -0.050847093193
## Intercept.to.1layhid4     -0.680302802642
## Input.to.1layhid4          0.042837254098
## Intercept.to.1layhid5      2.038820357423
## Input.to.1layhid5         -0.042669530733
## Intercept.to.1layhid6      0.435727729516
## Input.to.1layhid6          0.044220254019
## Intercept.to.1layhid7      0.318556741011
## Input.to.1layhid7          0.047590560226
## Intercept.to.1layhid8      0.135324618069
## Input.to.1layhid8          0.047962223137
## Intercept.to.1layhid9     -0.436441743513
## Input.to.1layhid9         -0.038477165647
## Intercept.to.1layhid10     1.533633555732
## Input.to.1layhid10         0.034630058360
## Intercept.to.Output        0.932322723379
## 1layhid.1.to.Output       -2.998558875821
## 1layhid.2.to.Output        1.821882178934
## 1layhid.3.to.Output       -2.777409240869
## 1layhid.4.to.Output        3.540047148500
## 1layhid.5.to.Output       -1.235728863749
## 1layhid.6.to.Output        1.233976952915
## 1layhid.7.to.Output        1.658658329933
## 1layhid.8.to.Output        1.261121112522
## 1layhid.9.to.Output       -1.769907245101
## 1layhid.10.to.Output       0.768267134439
## 
## attr(,"class")
## [1] "nn"
#Plot the neural network
plot(net.sqrt)
 
#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
 
#See what properties net.sqrt has
ls(net.results)
## [1] "net.result" "neurons"
#see the results
print(net.results$net.result)
##              [,1]
##  [1,] 1.032496298
##  [2,] 2.002426250
##  [3,] 2.998899895
##  [4,] 4.003370235
##  [5,] 4.998197260
##  [6,] 6.000180311
##  [7,] 7.002066164
##  [8,] 7.996402383
##  [9,] 9.003971910
## [10,] 9.962105141
#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       1.032496298
## 2      4               2       2.002426250
## 3      9               3       2.998899895
## 4     16               4       4.003370235
## 5     25               5       4.998197260
## 6     36               6       6.000180311
## 7     49               7       7.002066164
## 8     64               8       7.996402383
## 9     81               9       9.003971910
## 10   100              10       9.962105141

Acknowledgement: this example excercise was from http://gekkoquant.com/2012/05/26/neural-networks-with-r-simple-example/