Created
August 24, 2018 22:42
-
-
Save lukakostic/6a66a3499eb98b8be9c8de24bae67664 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1-neuron N-inputs 1-output perceptron in c++ (Binary classifier) | |
//Training data is for AND but you edit it to what you want the Perceptron to learn | |
//Short and sweet with lots of comments | |
#include <stdio.h> | |
#define dataLenght 4 | |
#define numOfInputs 2 | |
#define trainTimes 100 | |
struct trainingData | |
{ | |
double *inputs; //array of inputs | |
double output; | |
}; | |
int main() { | |
//////////////////////////////////////////////////////////////////////////////////////////////////////// data | |
struct trainingData data[dataLenght]; | |
//Below is training data for an AND operation. | |
//0 && 0 = 0 | |
double inp1[numOfInputs] = {0,0}; | |
data[0].inputs = &(inp1[0]); | |
data[0].output = 0; | |
//0 && 1 = 0 | |
double inp2[numOfInputs] = { 0,1 }; | |
data[1].inputs = &(inp2[0]); | |
data[1].output = 0; | |
//1 && 0 = 0 | |
double inp3[numOfInputs] = { 1,0 }; | |
data[2].inputs = &(inp3[0]); | |
data[2].output = 0; | |
//1 && 1 = 1 | |
double inp4[numOfInputs] = { 1,1 }; | |
data[3].inputs = &(inp4[0]); | |
data[3].output = 1; | |
//////////////////////////////////////////////////////////////////////////////////////////////////////// perceptron parameters | |
double weights[numOfInputs]; | |
// Make all weights 0 | |
for (int i = 0; i < numOfInputs; i++){ weights[i] = 0; } | |
double bias = 0; | |
//////////////////////////////////////////////////////////////////////////////////////////////////////// perceptron training | |
for (int e = 0; e < trainTimes; e++) //How many times to train (epochs) | |
{ | |
double totalError = 0; //Total error for this epoch & each data set | |
for (int d = 0; d < dataLenght; d++) //Go thru each training example | |
{ | |
//////////////////////////////////////////////////////////////////////////////////////////////////////// predict output | |
double output = 0; // End result of input*weight + bias | |
for (int i = 0; i < numOfInputs; i++) //For each input & weight | |
{ | |
output += data[d].inputs[i] * weights[i]; | |
} | |
output += bias; | |
//////////////////////////////////////////////////////////////////////////////////////////////////////// output & error | |
//We have 2 classifications: 0 or 1. (binary classification) | |
if (output < 0) | |
output = 0; | |
else | |
output = 1; | |
double error = data[d].output - output; //How wrong was our prediction? | |
//Same as math.abs(error) | |
if (error < 0) | |
totalError += -error; | |
else | |
totalError += error; | |
//////////////////////////////////////////////////////////////////////////////////////////////////////// update weights based on error | |
for (int i = 0; i < numOfInputs; i++) //Correct each weight based on error | |
{ | |
weights[i] = data[d].inputs[i] * error + weights[i]; | |
} | |
bias = error + bias; | |
//////////////////////////////////////////////////////////////////////////////////////////////////////// fin. | |
} | |
printf("epoch %d, error %f \n", e, totalError); | |
} | |
while (2 > 1) {} // pause | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment