Training for Errors
After you have obtained the neural network output and calculated the error for an input pattern, you can train the neural network to better recognize this pattern next time. This is done very simply by calling the learn method. Before you call the learn method, however, you must ensure that you have calculated the output and errors. The signature for the learn method is as follows:
public void learn()
The neural network begins with a completely random weight matrix. You may wish to completely reset the neural network back to this state, which can be done at any time by calling the reset method. The signature for the reset method is shown here:
public void reset()
As you can see, you must use the output calculation, error calculation, and training methods in a specific order. The following code segment shows how this is done:
double input[] = { { 0,0 }, { 0,1 }, { 1,0 }, { 1,1 } }; double ideal[] = { {0}, {1}, {1}, {0 }}; for( int i=0;i<input.length; i++) { neural.calcOutputs( input[i] ); neural.calcError(ideal[i]); neural.learn(); } System.out.println("Total error: " + neural.getError(input.length) );
As you can see from the above code, the training set used is for the XOR operator. This training set is then presented to the "calcOutputs" method. Though the "calcOutputs" calculates the output for the neural network, these outputs are discarded because they are not needed. At this point, we are only calling the "calcOutputs" method to prepare to call the "calcError" method. This is because at this point we are simply training; we do not care about the actual and outputs of the neural network. Once the "calcOutputs" method has been called, the "calcError" method is called. The ideal outputs, which are the outputs we expected from the neural network, are passed into the "calcError" method. The "calcError" method then determines how close the actual outputs from the neural network match these ideal outputs. With this completed, we can finally we call the "learn" method of the neural network. The "learn" method will make any adjustments to the weight matrix to allow a network to better recognize this training set and the ideal out and produce the a deal outputs.
The above code loops through all four of the possibilities for the XOR problem. Just as humans often do not learn a new skill by trying it only once, so, too, the neural network learns through repetition. The example program that you will see in the next section runs through a similar look up to 10,000 times to properly train the neural network. Generally, you'll write neural network programs to evaluate the overall error and continue looping through the training set as long as the error is above a desired amount, such as 10 percent.
To see all of this in action, an example program is provided that learns the XOR problem. This program is shown in Listing 2. Sample output from the program is shown here:
Trial #9988,Error:0.4244% Trial #9989,Error:0.4244% Trial #9990,Error:0.4244% Trial #9991,Error:0.4243% Trial #9992,Error:0.4243% Trial #9993,Error:0.4243% Trial #9994,Error:0.4243% Trial #9995,Error:0.4242% Trial #9996,Error:0.4242% Trial #9997,Error:0.4242% Trial #9998,Error:0.4242% Trial #9999,Error:0.4242% Recall: 0.0:0.0:=0.0015390535415879141 1.0:0.0:=0.9959570991994623 0.0:1.0:=0.9960039600916047 1.0:1.0:=0.006105436252615929
Here, you can see that the error rate has fallen to well below one percent. Once the program has looped through 10,000 trials, the neural network is tested. As you can see, the XOR problem is properly recalled. For the binary patterns 10 and 01, the network recalls a number that is very close to one.