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,tan(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,5), threshold=0.001,lifesign = "full",lifesign.step = 1000)
hidden: 10, 5 thresh: 0.001 rep: 1/1 steps: 1000 min thresh: 0.005405457351
2000 min thresh: 0.001846320338
3000 min thresh: 0.001846320338
4000 min thresh: 0.001412758764
5000 min thresh: 0.001106107449
5527 error: 0.00011 time: 2.57 secs
#print(n.net)
n1.net <- neuralnet( formula = Output~Input, data = train, hidden=c(10,5), threshold=0.0001,lifesign = "full",lifesign.step = 1000
,startweights=n.net$weights)
hidden: 10, 5 thresh: 0.0001 rep: 1/1 steps: 1000 min thresh: 0.0008768371493
2000 min thresh: 0.0006944589611
3000 min thresh: 0.0003810472056
4000 min thresh: 0.0002721542456
5000 min thresh: 0.0001539876516
6000 min thresh: 0.0001539876516
7000 min thresh: 0.0001539876516
8000 min thresh: 0.0001539876516
9000 min thresh: 0.0001539876516
10000 min thresh: 0.0001539876516
11000 min thresh: 0.0001539876516
12000 min thresh: 0.0001539876516
13000 min thresh: 0.0001539876516
14000 min thresh: 0.0001539876516
15000 min thresh: 0.0001539876516
16000 min thresh: 0.0001539876516
17000 min thresh: 0.0001264070201
17875 error: 0.00001 time: 8.62 secs
#print(n.net)
Let’ see what our network looks like
plot(n1.net)
Generating Test data to validate out network
#test<-as.data.frame(setdiff(1:600,train$Input))#as.data.frame(c(31:60))
test<-as.data.frame(c(1:100))#as.data.frame(c(31:60))
names(test)<-"Input"
head(test)
Now running test data on the generated model
result<-compute(n1.net,test)
head(result$net.result)
[,1]
[1,] 0.00001084349734
[2,] 2.21136635687555
[3,] 1.73199727896773
[4,] 1.00015120849479
[5,] 0.70690106834471
[6,] 0.57688969627637
Combining results
test<-cbind(test,result$net.result,tan(pi/test$Input))
colnames(test)<-c("Input","Neural Output","Computed Output")
test
Lets round the numbers and have a look at them.
head(round(test,2))
SO we have successfully simulated a neural network for a formula f(x) = tan(PI/x). Our accuracy is 99.99994% Althought the accuravy is high, we see an outlier at Input = 2. To avoid this neural network must be trained for all extreme scenarios. Except the outlier the model accurately gives results.