I am in need of help. I am trying to code a perceptron cable in C++ for my Machine Learning course. I understand the table itself as well as the equations that go along with it.
My question is why are my values getting returned to me as an extremely small number in some instances? In the first picture will be my output, where you'll notice that I am getting -3E-27 for some of my weight values.
OutPut Window If the image did not load, here it is in text format
Perceptron acting as an OR Gate
-------------------------------------------------------------
Epoch Inputs Desired Initial Actual Error Final
-------------------------------------------------------------
1 0.,0. 0. 0.3,-0.1 0 0. 0.3,-0.1
1 0.,1. 1. 0.3,-0.1 0 -1. 0.2,-0.2
1 1.,0. 1. 0.2,-0.2 0 -1. 0.1,-0.3
1 1.,1. 1. 0.1,-0.3 0 -1. -3.e-17,-0.4
Epoch Inputs Desired Initial Actual Error Final
-------------------------------------------------------------
2 0.,0. 0. -3.e-17,-0.4 0 -1. -3.e-17,-0.4
2 0.,1. 1. -3.e-17,-0.4 0 -1. -0.1,-0.5
2 1.,0. 1. -0.1,-0.5 0 -1. -0.2,-0.6
2 1.,1. 1. -0.2,-0.6 0 -1. -0.3,-0.7
Epoch Inputs Desired Initial Actual Error Final
-------------------------------------------------------------
3 0.,0. 0. -0.3,-0.7 0 -1. -0.3,-0.7
3 0.,1. 1. -0.3,-0.7 0 -1. -0.4,-0.8
3 1.,0. 1. -0.4,-0.8 0 -1. -0.5,-0.9
3 1.,1. 1. -0.5,-0.9 0 -1. -0.6,-1.
Epoch Inputs Desired Initial Actual Error Final
-------------------------------------------------------------
4 0.,0. 0. -0.6,-1. 0 -1. -0.6,-1.
4 0.,1. 1. -0.6,-1. 0 -1. -0.7,-1.
4 1.,0. 1. -0.7,-1. 0 -1. -0.8,-1.
4 1.,1. 1. -0.8,-1. 0 -1. -0.9,-1.
Epoch Inputs Desired Initial Actual Error Final
-------------------------------------------------------------
5 0.,0. 0. -0.9,-1. 0 -1. -0.9,-1.
5 0.,1. 1. -0.9,-1. 0 -1. -1.,-1.
5 1.,0. 1. -1.,-1. 0 -1. -1.,-2.
5 1.,1. 1. -1.,-2. 0 -1. -1.,-2.
These two images are of the entire program.
Top of Program Bottom of Program
If the images did not load, here is the code in text format.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
double threshold = .2;
double learningRate = .1;
int thresholdResult = 0;
double x1Inputs[4] = { 0, 0, 1, 1 };
double x2Inputs[4] = { 0, 1, 0, 1 };
double DesiredOutputs[4] = { 0,1,1,1 };
double sum = 0;
double weightOne = .30;
double weightTwo = -.10;
double weightOneFinal = 0;
double weightTwoFinal = 0;
double error = 0;
bool isError = false;
cout << " Perceptron acting as an OR Gate " << endl;
cout << "-------------------------------------------------------------" << endl;
for (int j = 0; j < 5; j++) // iterates through each epoch
{
for (int i = 0; i < 4; i++) // iterates through each array
{
isError = false; // resets the Error check bool to false
sum = (x1Inputs[i] * weightOne) + (x2Inputs[i] * weightTwo); // Does initial weight and input comparison and sets threshold value for comparison
if (sum < threshold)
thresholdResult = 0;
else if (sum >= threshold)
thresholdResult = 1;
if (thresholdResult != DesiredOutputs[i]) // if these don't match caclulate error and set error checker to true
{
error = thresholdResult - DesiredOutputs[i];
isError = true;
}
if (isError == true) // !!!THIS IS WHERE THE ERROR OCCURS!!! calculates new weights if error checker is true otherwise sets the final weights to starting weights
{
weightOneFinal = weightOne + (learningRate * error * 1.0);
weightTwoFinal = weightTwo + (learningRate * error * 1.0);
}
else if (isError == false)
{
weightOneFinal = weightOne;
weightTwoFinal = weightTwo;
}
if (i == 0)
{
cout << " Epoch Inputs Desired Initial Actual Error Final " << endl;
cout << "-------------------------------------------------------------" << endl;
}
cout << setprecision(1) << showpoint << setw(4) << j + 1 << setw(8) << x1Inputs[i] << "," << x2Inputs[i] << setw(8) << DesiredOutputs[i] << setw(8) << weightOne << "," << weightTwo << setw(5) << thresholdResult << setw(10) << error << setw(8) << weightOneFinal << "," << weightTwoFinal << "\n\n";
weightOne = weightOneFinal; // sets main weights to final weights
weightTwo = weightTwoFinal;
}
}
return 0;
}
I know that this code is probably not good, I am learning better etiquette at the moment. Any advice is greatly appreciated!
I have tried stepping through the entire program multiple times, but for some reason, on the third iteration of the nested for loop, the value of WeightFinalOne changes to -3E-27, and I have no explanation for it.