A long time ago, I got interested in neural networks. Seeing the power they gave me , I tried coding one in Lua. In vain. It didn't work, so I gave it up. Some days ago I tried it again, and because I did not use the right approach, I failed. However, today my third attempt started. After working with pauses for 3-4 hours, it worked!
I can finally and proudly present you AI, a fully working neural network! This version has the ability to learn. Isn't that amazing. Some of the best suited uses would be recognizing handwriting, training it to emulate a XOR gate, finding the verb in a sentence, and other totally useful stuff.
You're dying to use it? Here are the different functions it provides you with:
Spoiler
Loading it into memory:
os.loadAPI("AI")
Constructing a net:
AI.Net(topology)
@topology: a table that holds the topology of the network.Example:
AI.Net({2,4,1})
This network will have 2 input neurons, 1 hidden layer with 4 neurons, and one output neuron.Analyzing information:
myNet = AI.Net({2,4,1})
myNet.feedForward({1,0}) --this is the interesting line.
The argument given to feedForward is a table holding the information for the input layer. The number at index 1 will be provided to the first input neuron, the second to the second, and so on.Training it:
myNet = AI.Net({2,4,1})
myNet.feedForward({1,0})
myNet.backProp({1}) --this is the interesting line.
This means that that the network will modify the weights in order to make the previously given input output the numbers you gave as an ardument now.Getting the results:
myNet = AI.Net({2,4,1})
myNet.feedForward({1,0})
myNet.backProp({1})
myNet.getResults()--this is the interesting line.
This just returns a table with the results!Serialize:
myNet = AI.Net({2,4,1})
myNet.feedForward({1,0})
myNet.backProp({1})
myNet.getResults()
serialized = myNet.serialize() --this is the interesting line.
Serialized is a table that can be written to a file.Unserialize:
myNet = AI.Net({2,4,1})
myNet.feedForward({1,0})
myNet.backProp({1})
myNet.getResults()
serialized = myNet.serialize()
myNet = nil
myNet = AI.unserialize(serialized) -- this is the interesting line
myNet is a valid and fully working neural network.Here is a GIF, even if there ain't much to see!
The GIF shows a XOR gate.
Download:
pastebin get GHHCma5U AI
An example program:
pastebin get LRKaMh54 NN
Ideas for improvements:
- Add a way to save a neural network to a file.