Simulating Neural network to build a model for basic Sinosudal function
Loading Neural Network library
library(neuralnet)
Generating test data & Assigining names to test data
train<-as.data.frame(cbind((sample(1:600, 300, replace=FALSE))))
train<-cbind(train,pi/train$V1)
colnames(train) <- c("Input","Output")
train
Simulating neural network with a single hidden layer and ten neurons.
n.net <- neuralnet( formula = Output~Input, data = train, hidden=c(10), threshold=0.0001,lifesign = "full",lifesign.step = 1000)
hidden: 10 thresh: 0.0001 rep: 1/1 steps: 1000 min thresh: 0.003012459516
2000 min thresh: 0.001127379782
3000 min thresh: 0.0004023896783
4000 min thresh: 0.0004023896783
5000 min thresh: 0.0004023896783
6000 min thresh: 0.0004023896783
7000 min thresh: 0.0004023896783
8000 min thresh: 0.000186737906
8608 error: 0.00008 time: 2.55 secs
#print(n.net)
Let’ see what our network looks like
plot(n.net)
Generating Test data to validate out network
test<-as.data.frame(setdiff(1:60,train$Input))#as.data.frame(c(31:60))
names(test)<-"Input"
test
Now running test data on the generated model
result<-compute(n.net,test)
result$net.result
[,1]
[1,] 0.79256867601
[2,] 0.52393927586
[3,] 0.26404453350
[4,] 0.22776063038
[5,] 0.21272152542
[6,] 0.18710782998
[7,] 0.17614411018
[8,] 0.15727134043
[9,] 0.14186415219
[10,] 0.13526470155
[11,] 0.12392301739
[12,] 0.11905084781
[13,] 0.11060730070
[14,] 0.10355302790
[15,] 0.09753993527
[16,] 0.09483592317
[17,] 0.08544573495
[18,] 0.08136613685
[19,] 0.07578268698
[20,] 0.07072133073
[21,] 0.06760261519
[22,] 0.06328296308
[23,] 0.06193547841
[24,] 0.06063303070
[25,] 0.05816034718
[26,] 0.05372056030
Combining results
test<-cbind(test,result$net.result,(pi/test$Input))
colnames(test)<-c("Input","Neural Output","Computed Output")
test
Lets round the numbers and have a look at them.
round(test,2)
SO we have successfully simulated a neural network for a formula f(x) = PI/x. Our accuracy is 99.9919%