Learning by training: Artificial Neural Networks

Parallel_Universe_by_pavel89l

Akin to the way our brain functions, we can try to replicate the processing with a rather liberal degree of simplicity, artificially.
Below I have trained a network to complete a rather simple task using backpropagation algorithm. R provides depth in terms of its packages and I opt for ‘neuralnet’.


#The objective is to train the network to perform a cube root transformation
#We start by randomly choosing 50 numbers within a range from 0 to 100

> traininginput <- data.frame(runif(50, min=0, max=100))
> trainingoutput <- (traininginput)^(1/3)

 

#Bind the data

> trainingdata <- cbind(traininginput,trainingoutput)
> colnames(trainingdata) <- c(“Input”,”Output”)


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

> net.sq <- neuralnet(Output~Input,trainingdata, hidden=10, threshold=0.01)
> print(net.sq)
Call: neuralnet(formula = Output ~ Input, data = trainingdata, hidden = 10, threshold = 0.01) 1 repetition was calculated. Error Reached Threshold Steps 1 0.001006152137 0.008555332512 940


#A view of the neural network

> plot(net.sq)

Rplot


#We test the neural network on the training data

> testdata <- data.frame((1:5)^3)
> net.results <- compute(net.sq, testdata)

 

#A preview of the results

> print(net.results$net.result)
[,1] [1,] 0.9018869094 [2,] 1.9922174192 [3,] 3.0012337351 [4,] 4.0025262073 [5,] 4.9005253347

#Results in format of actual vs neural net predictions

> cleanoutput <- cbind(testdata,(testdata)^(1/3),
+ 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.9018869094 2 8 2 1.9922174192 3 27 3 3.0012337351 4 64 4 4.0025262073 5 125 5 4.9005253347

© 2013 Tanya Rawat. By posting content to and from this blog, you agree to transfer copyright to blog owner.

© Pavel Photography