-
Notifications
You must be signed in to change notification settings - Fork 69
XOR_Example
A common hello-world test of a neural net is to train it to perform the exclusive-OR function. Here is a description of how to do that with neural2d.
The XOR function has two inputs and one output, all binary. Here is the truth table for XOR:
F F => F
F T => T
T T => T
T F => F
First, we'll define the topology config file for a two-input, one-output net with one hidden layer of four neurons. We'll name this file topology-xor.txt:
input size 2
layer1 size 4 from input
output size 1 from layer1
Next, we'll make an input training data config file, consisting of one training sample for each possible input pattern. We'll use the syntax that specifies the literal input values. We'll name this file inputData-xor.txt. We'll use the values -1 and 1 to represent false and true:
{ -1 -1 } -1
{ -1 1 } 1
{ 1 -1 } 1
{ 1 1 } -1
Next, we'll launch the neural2d program, specifying the toplogy and input data config files, and the file in which we want to save the trained weights::
./neural2d topology-xor.txt inputData-xor.txt weights.txt
If you want to attach the GUI, add a -p option to that invocation line to cause the net to start in paused mode.
In just a few seconds of training, the net will announce that it has solved the problem. The weights file will be written. You have a trained net. The final screen will look something like this:
If you don't get similar results, check that the following parameters are set to reasonable values for training. These can be set in main() in neural.cpp, or you can invoke the neural2d program with the -p option and use the GUI to set these. In main(), these would look like:
myNet.eta = 0.1;
myNet.alpha = 0.0;
myNet.reportEveryNth = 1;
myNet.repeatInputSamples = true;
myNet.shuffleInputSamples = true;
myNet.doneErrorThreshold = 0.01;
Here is how the GUI looks with those parameters: