J Solutions ch. 10 - Artificial neural networks

Solutions to exercises of chapter 11.

J.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  8.930844
## 2  8.560692
## 3  9.332153
## 4  2.225081
## 5  7.708607
## 6  8.801227
## 7  4.457719
## 8  4.515305
## 9  7.486919
## 10 8.670440
## 11 7.165398
## 12 6.887311
## 13 9.893510
## 14 8.459845
## 15 8.019395
## 16 9.229572
## 17 5.651959
## 18 6.953218
## 19 9.349654
## 20 4.591209
## 21 9.822524
## 22 5.394505
## 23 1.773235
## 24 4.463503
## 25 5.880014
## 26 8.424823
## 27 6.427239
## 28 3.446573
## 29 7.849548
## 30 8.383501
## 31 5.289092
## 32 1.775427
## 33 9.736097
## 34 9.911645
## 35 4.183869
## 36 8.552112
## 37 1.724901
## 38 6.402549
## 39 8.964615
## 40 4.204111
## 41 8.745063
## 42 7.350497
## 43 4.044558
## 44 5.760175
## 45 8.402363
## 46 5.170573
## 47 9.693512
## 48 4.130986
## 49 6.296208
## 50 9.285023
## 
## $covariate
##                
##  [1,] 79.759970
##  [2,] 73.285452
##  [3,] 87.089073
##  [4,]  4.950986
##  [5,] 59.422624
##  [6,] 77.461595
##  [7,] 19.871258
##  [8,] 20.387982
##  [9,] 56.053956
## [10,] 75.176532
## [11,] 51.342933
## [12,] 47.435047
## [13,] 97.881532
## [14,] 71.568974
## [15,] 64.310695
## [16,] 85.185006
## [17,] 31.944641
## [18,] 48.347236
## [19,] 87.416035
## [20,] 21.079204
## [21,] 96.481982
## [22,] 29.100688
## [23,]  3.144364
## [24,] 19.922859
## [25,] 34.574566
## [26,] 70.977639
## [27,] 41.309399
## [28,] 11.878863
## [29,] 61.615399
## [30,] 70.283092
## [31,] 27.974498
## [32,]  3.152142
## [33,] 94.791593
## [34,] 98.240712
## [35,] 17.504763
## [36,] 73.138616
## [37,]  2.975285
## [38,] 40.992639
## [39,] 80.364325
## [40,] 17.674546
## [41,] 76.476124
## [42,] 54.029801
## [43,] 16.358447
## [44,] 33.179611
## [45,] 70.599697
## [46,] 26.734821
## [47,] 93.964183
## [48,] 17.065044
## [49,] 39.642241
## [50,] 86.211658
## 
## $model.list
## $model.list$response
## [1] "Output"
## 
## $model.list$variables
## [1] "Input"
## 
## 
## $err.fct
## function (x, y) 
## {
##     1/2 * (y - x)^2
## }
## <bytecode: 0x356c410>
## <environment: 0x3521470>
## attr(,"type")
## [1] "sse"
## 
## $act.fct
## function (x) 
## {
##     1/(1 + exp(-x))
## }
## <bytecode: 0x27aa648>
## <environment: 0x277a3b8>
## attr(,"type")
## [1] "logistic"
## 
## $linear.output
## [1] TRUE
## 
## $data
##        Input   Output
## 1  79.759970 8.930844
## 2  73.285452 8.560692
## 3  87.089073 9.332153
## 4   4.950986 2.225081
## 5  59.422624 7.708607
## 6  77.461595 8.801227
## 7  19.871258 4.457719
## 8  20.387982 4.515305
## 9  56.053956 7.486919
## 10 75.176532 8.670440
## 11 51.342933 7.165398
## 12 47.435047 6.887311
## 13 97.881532 9.893510
## 14 71.568974 8.459845
## 15 64.310695 8.019395
## 16 85.185006 9.229572
## 17 31.944641 5.651959
## 18 48.347236 6.953218
## 19 87.416035 9.349654
## 20 21.079204 4.591209
## 21 96.481982 9.822524
## 22 29.100688 5.394505
## 23  3.144364 1.773235
## 24 19.922859 4.463503
## 25 34.574566 5.880014
## 26 70.977639 8.424823
## 27 41.309399 6.427239
## 28 11.878863 3.446573
## 29 61.615399 7.849548
## 30 70.283092 8.383501
## 31 27.974498 5.289092
## 32  3.152142 1.775427
## 33 94.791593 9.736097
## 34 98.240712 9.911645
## 35 17.504763 4.183869
## 36 73.138616 8.552112
## 37  2.975285 1.724901
## 38 40.992639 6.402549
## 39 80.364325 8.964615
## 40 17.674546 4.204111
## 41 76.476124 8.745063
## 42 54.029801 7.350497
## 43 16.358447 4.044558
## 44 33.179611 5.760175
## 45 70.599697 8.402363
## 46 26.734821 5.170573
## 47 93.964183 9.693512
## 48 17.065044 4.130986
## 49 39.642241 6.296208
## 50 86.211658 9.285023
## 
## $exclude
## NULL
## 
## $net.result
## $net.result[[1]]
##           [,1]
##  [1,] 8.931924
##  [2,] 8.559944
##  [3,] 9.335960
##  [4,] 2.225642
##  [5,] 7.708689
##  [6,] 8.801464
##  [7,] 4.458088
##  [8,] 4.515923
##  [9,] 7.487218
## [10,] 8.670040
## [11,] 7.165537
## [12,] 6.886980
## [13,] 9.888458
## [14,] 8.458923
## [15,] 8.018873
## [16,] 9.232852
## [17,] 5.652160
## [18,] 6.953012
## [19,] 9.353522
## [20,] 4.592127
## [21,] 9.820458
## [22,] 5.395522
## [23,] 1.772179
## [24,] 4.463897
## [25,] 5.879540
## [26,] 8.423871
## [27,] 6.426217
## [28,] 3.446734
## [29,] 7.849379
## [30,] 8.382533
## [31,] 5.290389
## [32,] 1.774267
## [33,] 9.736705
## [34,] 9.905704
## [35,] 4.182998
## [36,] 8.551344
## [37,] 1.726687
## [38,] 6.401513
## [39,] 8.965941
## [40,] 4.203324
## [41,] 8.744998
## [42,] 7.350794
## [43,] 4.043187
## [44,] 5.760036
## [45,] 8.401400
## [46,] 5.172108
## [47,] 9.695099
## [48,] 4.129904
## [49,] 6.295151
## [50,] 9.288621
## 
## 
## $weights
## $weights[[1]]
## $weights[[1]][[1]]
##           [,1]       [,2]        [,3]        [,4]        [,5]      [,6]
## [1,]  0.851746  0.1565844  0.02188730 -1.42114331 -0.17954951 1.1986313
## [2,] -1.041321 -0.1116762 -0.04432291  0.04281873  0.04463685 0.1000529
##             [,7]      [,8]        [,9]       [,10]
## [1,] -0.03684711 0.1634353 -1.30723962  5.35932831
## [2,] -1.36294001 0.3771781  0.02862289 -0.05583851
## 
## $weights[[1]][[2]]
##               [,1]
##  [1,]  1.495554324
##  [2,] -0.306581457
##  [3,] -2.948048285
##  [4,] -1.092214108
##  [5,]  2.577747479
##  [6,]  2.111221703
##  [7,]  0.003160103
##  [8,]  1.674168046
##  [9,]  1.950411917
## [10,]  3.437438295
## [11,] -1.814634907
## 
## 
## 
## $generalized.weights
## $generalized.weights[[1]]
##                [,1]
##  [1,] -0.0007958773
##  [2,] -0.0009047143
##  [3,] -0.0006910291
##  [4,] -0.0842365350
##  [5,] -0.0012523137
##  [6,] -0.0008321241
##  [7,] -0.0073079671
##  [8,] -0.0070033735
##  [9,] -0.0013744337
## [10,] -0.0008706124
## [11,] -0.0015814932
## [12,] -0.0017940364
## [13,] -0.0005476333
## [14,] -0.0009377347
## [15,] -0.0011054810
## [16,] -0.0007171781
## [17,] -0.0033534965
## [18,] -0.0017405350
## [19,] -0.0006865875
## [20,] -0.0066264615
## [21,] -0.0005660563
## [22,] -0.0038968616
## [23,] -0.1962291130
## [24,] -0.0072766130
## [25,] -0.0029563465
## [26,] -0.0009496156
## [27,] -0.0022323914
## [28,] -0.0170514902
## [29,] -0.0011824272
## [30,] -0.0009639245
## [31,] -0.0041549285
## [32,] -0.1954204670
## [33,] -0.0005883420
## [34,] -0.0005429101
## [35,] -0.0090130069
## [36,] -0.0009074581
## [37,] -0.2146967192
## [38,] -0.0022596304
## [39,] -0.0007866895
## [40,] -0.0088706562
## [41,] -0.0008483909
## [42,] -0.0014576751
## [43,] -0.0100760443
## [44,] -0.0031563873
## [45,] -0.0009573533
## [46,] -0.0044741916
## [47,] -0.0005992649
## [48,] -0.0093988897
## [49,] -0.0023820580
## [50,] -0.0007030140
## 
## 
## $startweights
## $startweights[[1]]
## $startweights[[1]][[1]]
##            [,1]       [,2]       [,3]       [,4]        [,5]      [,6]
## [1,]  1.3653738 -0.4566848 -0.4513698 -0.5276323 -0.01948716 0.7244477
## [2,] -0.9350547 -1.2971079 -0.4906967  0.6293893 -0.01211604 0.5846814
##            [,7]     [,8]       [,9]       [,10]
## [1,] -0.9928345 1.636541 -0.4190859  2.57441289
## [2,] -1.7645406 1.585323  0.7671283 -0.03213535
## 
## $startweights[[1]][[2]]
##             [,1]
##  [1,]  0.3904085
##  [2,]  0.3050366
##  [3,] -2.1046561
##  [4,] -0.1316618
##  [5,]  0.4124189
##  [6,] -0.0177959
##  [7,] -1.0983625
##  [8,]  0.9561778
##  [9,]  0.8438717
## [10,]  0.8478274
## [11,] -0.3921739
## 
## 
## 
## $result.matrix
##                                 [,1]
## error                   7.510490e-05
## reached.threshold       8.256243e-03
## steps                   3.506000e+03
## Intercept.to.1layhid1   8.517460e-01
## Input.to.1layhid1      -1.041321e+00
## Intercept.to.1layhid2   1.565844e-01
## Input.to.1layhid2      -1.116762e-01
## Intercept.to.1layhid3   2.188730e-02
## Input.to.1layhid3      -4.432291e-02
## Intercept.to.1layhid4  -1.421143e+00
## Input.to.1layhid4       4.281873e-02
## Intercept.to.1layhid5  -1.795495e-01
## Input.to.1layhid5       4.463685e-02
## Intercept.to.1layhid6   1.198631e+00
## Input.to.1layhid6       1.000529e-01
## Intercept.to.1layhid7  -3.684711e-02
## Input.to.1layhid7      -1.362940e+00
## Intercept.to.1layhid8   1.634353e-01
## Input.to.1layhid8       3.771781e-01
## Intercept.to.1layhid9  -1.307240e+00
## Input.to.1layhid9       2.862289e-02
## Intercept.to.1layhid10  5.359328e+00
## Input.to.1layhid10     -5.583851e-02
## Intercept.to.Output     1.495554e+00
## 1layhid1.to.Output     -3.065815e-01
## 1layhid2.to.Output     -2.948048e+00
## 1layhid3.to.Output     -1.092214e+00
## 1layhid4.to.Output      2.577747e+00
## 1layhid5.to.Output      2.111222e+00
## 1layhid6.to.Output      3.160103e-03
## 1layhid7.to.Output      1.674168e+00
## 1layhid8.to.Output      1.950412e+00
## 1layhid9.to.Output      3.437438e+00
## 1layhid10.to.Output    -1.814635e+00
## 
## 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.322269
##  [2,] 1.996146
##  [3,] 3.005018
##  [4,] 3.998514
##  [5,] 5.001685
##  [6,] 5.999254
##  [7,] 6.999880
##  [8,] 7.999519
##  [9,] 9.001591
## [10,] 9.988901
#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.322269
## 2      4               2          1.996146
## 3      9               3          3.005018
## 4     16               4          3.998514
## 5     25               5          5.001685
## 6     36               6          5.999254
## 7     49               7          6.999880
## 8     64               8          7.999519
## 9     81               9          9.001591
## 10   100              10          9.988901

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