//Author: Ralph Heymsfeld
//28/06/2018
#include <math.h>
/**********************
* Network Configuration - customized per network
**********************/
const int PatternCount = 10;
const int InputNodes = 7;
const int HiddenNodes = 8;
const int OutputNodes = 4;
const float LearningRate = 0.3;
const float Momentum = 0.9;
const float InitialWeightMax = 0.5;
const float Success = 0.0004;
const byte Input[PatternCount][InputNodes] = {
{ 1, 1, 1, 1, 1, 1, 0 }, // 0
{ 0, 1, 1, 0, 0, 0, 0 }, // 1
{ 1, 1, 0, 1, 1, 0, 1 }, // 2
{ 1, 1, 1, 1, 0, 0, 1 }, // 3
{ 0, 1, 1, 0, 0, 1, 1 }, // 4
{ 1, 0, 1, 1, 0, 1, 1 }, // 5
{ 0, 0, 1, 1, 1, 1, 1 }, // 6
{ 1, 1, 1, 0, 0, 0, 0 }, // 7
{ 1, 1, 1, 1, 1, 1, 1 }, // 8
{ 1, 1, 1, 0, 0, 1, 1 } // 9
};
const byte Target[PatternCount][OutputNodes] = {
{ 0, 0, 0, 0 },
{ 0, 0, 0, 1 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 1 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 1 },
{ 0, 1, 1, 0 },
{ 0, 1, 1, 1 },
{ 1, 0, 0, 0 },
{ 1, 0, 0, 1 }
};
Run the code
output is
Training cycle
each time it changes and reduce the error giving the output and finally It get solved.
change the code and bring the XOR table

#include <math.h>
/**********************
* Network Configuration - customized per network
**********************/
const int PatternCount = 4;
const int InputNodes = 2;
const int HiddenNodes = 3;
const int OutputNodes = 1;
const float LearningRate = 0.3;
const float Momentum = 0.9;
const float InitialWeightMax = 0.5;
const float Success = 0.0004;
const byte Input[PatternCount][InputNodes] = {
{ 0,0 }, // 0
{ 0,1 }, // 1
{ 1,0 }, // 2
{ 1,1 }, // 3
};
const byte Target[PatternCount][OutputNodes] = {
{ 0 },
{ 1 },
{ 1 },
{ 0 },
};
and the result you get
Training cycle is very fast when optimising.
You get solved very quick.
we change the pattern count, Input and output nodes from the previous code.
First cycle output
Initial/Untrained Outputs:
Training Pattern: 0
Input 0 0 Target 0 Output 0.27673
Training Pattern: 1
Input 0 1 Target 1 Output 0.27028
Training Pattern: 2
Input 1 0 Target 1 Output 0.26796
Training Pattern: 3
Input 1 1 Target 0 Output 0.26167
TrainingCycle: 1 Error = 0.60655
Training Pattern: 0
Input 0 0 Target 0 Output 0.30410
Training Pattern: 1
Input 0 1 Target 1 Output 0.29860
Training Pattern: 2
Input 1 0 Target 1 Output 0.29505
Training Pattern: 3
Input 1 1 Target 0 Output 0.28964
TrainingCycle: 1000 Error = 0.00135
Training Pattern: 0
Input 0 0 Target 0 Output 0.01486
Training Pattern: 1
Input 0 1 Target 1 Output 0.97724
Training Pattern: 2
Input 1 0 Target 1 Output 0.96900
Training Pattern: 3
Input 1 1 Target 0 Output 0.03147
TrainingCycle: 2000 Error = 0.00051
Training Pattern: 0
Input 0 0 Target 0 Output 0.00857
Training Pattern: 1
Input 0 1 Target 1 Output 0.98563
Training Pattern: 2
Input 1 0 Target 1 Output 0.98122
Training Pattern: 3
Input 1 1 Target 0 Output 0.01961
TrainingCycle: 2432 Error = 0.00040
Training Pattern: 0
Input 0 0 Target 0 Output 0.00748
Training Pattern: 1
Input 0 1 Target 1 Output 0.98719
Training Pattern: 2
Input 1 0 Target 1 Output 0.98339
Training Pattern: 3
Input 1 1 Target 0 Output 0.01742
Training Set Solved!
--------
Second Cycle
Initial/Untrained Outputs:
Training Pattern: 0
Input 0 0 Target 0 Output 0.25949
Training Pattern: 1
Input 0 1 Target 1 Output 0.26417
Training Pattern: 2
Input 1 0 Target 1 Output 0.26299
Training Pattern: 3
Input 1 1 Target 0 Output 0.26827
TrainingCycle: 1 Error = 0.62150
Training Pattern: 0
Input 0 0 Target 0 Output 0.30798
Training Pattern: 1
Input 0 1 Target 1 Output 0.31376
Training Pattern: 2
Input 1 0 Target 1 Output 0.31220
Training Pattern: 3
Input 1 1 Target 0 Output 0.31843
TrainingCycle: 1000 Error = 0.00131
Training Pattern: 0
Input 0 0 Target 0 Output 0.02251
Training Pattern: 1
Input 0 1 Target 1 Output 0.97564
Training Pattern: 2
Input 1 0 Target 1 Output 0.97570
Training Pattern: 3
Input 1 1 Target 0 Output 0.03037
TrainingCycle: 2000 Error = 0.00052
Training Pattern: 0
Input 0 0 Target 0 Output 0.01446
Training Pattern: 1
Input 0 1 Target 1 Output 0.98465
Training Pattern: 2
Input 1 0 Target 1 Output 0.98470
Training Pattern: 3
Input 1 1 Target 0 Output 0.01886
TrainingCycle: 2476 Error = 0.00040
Training Pattern: 0
Input 0 0 Target 0 Output 0.01277
Training Pattern: 1
Input 0 1 Target 1 Output 0.98650
Training Pattern: 2
Input 1 0 Target 1 Output 0.98654
Training Pattern: 3
Input 1 1 Target 0 Output 0.01652
Training Set Solved !
---------
The XOR problem was a big challenge in early AI. It showed we needed more complex models like multi-layer perceptrons (MLPs) with hidden layers. These hidden layers allow neural networks to understand non-linear patterns between inputs and outputs. This was a big step forward for AI. Training neural networks involves adjusting weights and biases to reduce errors over many cycles. AI is becoming more common in everyday life, helping with decisions, automating tasks, and giving personalized recommendations. As AI technology improves, it will continue to make life easier and more innovative.
Reason for No of training cycles different each time
Initialization
learning rate
Network Architecture
Data variability
Stopping criteria
Comments
Post a Comment