C/C++ Perceptron
Introduction
This project was created in order to enable Perceptron creation and training API for C programmers.
This library enables training the perceptron step by step and enables to check its state at each step.
API & Usage Example
The text printed to the shell
Results for 'OR' perceptron, BEFORE training:
Input: (0,0). Result: 0
Input: (0,1). Result: 0
Input: (1,0). Result: 0
Input: (1,1). Result: 0
Results for 'AND' perceptron, BEFORE training:
Input: (0,0). Result: 1
Input: (0,1). Result: 0
Input: (1,0). Result: 1
Input: (1,1). Result: 0
Results for 'OR' perceptron, AFTER training:
Input: (0,0). Result: 0
Input: (0,1). Result: 1
Input: (1,0). Result: 1
Input: (1,1). Result: 1
Results for 'AND' perceptron, AFTER training:
Input: (0,0). Result: 0
Input: (0,1). Result: 0
Input: (1,0). Result: 0
Input: (1,1). Result: 1
The code
#include <stdio.h>
#include "perceptron.h"
int main() {
// Constants
const int TRAINING_ITERATIONS = 16;
const int NUM_OF_INPUTS = 2;
const double TRAINING_RATE = 0.2;
const double ZERO_ZERO[] = {0, 0};
const double ZERO_ONE[] = {0, 1};
const double ONE_ZERO[] = {1, 0};
const double ONE_ONE[] = {1, 1};
int i;
// Creating Perceptron instances
Perceptron *pAND = Perceptron_new(NUM_OF_INPUTS, TRAINING_RATE);
Perceptron *pOR = Perceptron_new(NUM_OF_INPUTS, TRAINING_RATE);
// Printing the results of the randomly generated perceptrons (BEFORE TRAINING)
printf("Results for 'OR' perceptron, BEFORE training:\n");
printf("Input: (0,0). Result: %d\n", Perceptron_getResult(pOR, ZERO_ZERO));
printf("Input: (0,1). Result: %d\n", Perceptron_getResult(pOR, ZERO_ONE));
printf("Input: (1,0). Result: %d\n", Perceptron_getResult(pOR, ONE_ZERO));
printf("Input: (1,1). Result: %d\n", Perceptron_getResult(pOR, ONE_ONE));
printf("Results for 'AND' perceptron, BEFORE training:\n");
printf("Input: (0,0). Result: %d\n", Perceptron_getResult(pAND, ZERO_ZERO));
printf("Input: (0,1). Result: %d\n", Perceptron_getResult(pAND, ZERO_ONE));
printf("Input: (1,0). Result: %d\n", Perceptron_getResult(pAND, ONE_ZERO));
printf("Input: (1,1). Result: %d\n", Perceptron_getResult(pAND, ONE_ONE));
// Training each of the perceptrons
for (i = 0 ; i < TRAINING_ITERATIONS ; i++) {
Perceptron_train(pAND, ZERO_ZERO, 0);
Perceptron_train(pAND, ZERO_ONE, 0);
Perceptron_train(pAND, ONE_ZERO, 0);
Perceptron_train(pAND, ONE_ONE, 1);
Perceptron_train(pOR, ZERO_ZERO, 0);
Perceptron_train(pOR, ZERO_ONE, 1);
Perceptron_train(pOR, ONE_ZERO, 1);
Perceptron_train(pOR, ONE_ONE, 1);
}
// Printing the results of the trained perceptrons (AFTER TRAINING)
printf("Results for 'OR' perceptron, AFTER training:\n");
printf("Input: (0,0). Result: %d\n", Perceptron_getResult(pOR, ZERO_ZERO));
printf("Input: (0,1). Result: %d\n", Perceptron_getResult(pOR, ZERO_ONE));
printf("Input: (1,0). Result: %d\n", Perceptron_getResult(pOR, ONE_ZERO));
printf("Input: (1,1). Result: %d\n", Perceptron_getResult(pOR, ONE_ONE));
printf("Results for 'AND' perceptron, AFTER training:\n");
printf("Input: (0,0). Result: %d\n", Perceptron_getResult(pAND, ZERO_ZERO));
printf("Input: (0,1). Result: %d\n", Perceptron_getResult(pAND, ZERO_ONE));
printf("Input: (1,0). Result: %d\n", Perceptron_getResult(pAND, ONE_ZERO));
printf("Input: (1,1). Result: %d\n", Perceptron_getResult(pAND, ONE_ONE));
return 0;
}
API
// Create a new perceptron with specified size of input-vector and a specified training rate
// The size of the input vector will be changed to "1" in case that 0 or negative number is passed
Perceptron *Perceptron_new(unsigned numOfInputs, double trainingRate);
// Get the sum of (input(0) * weight(0) + input(1) * weight(1) + ... + input(n) * weight(n))
double Perceptron_getValue(const Perceptron *perceptron, const double inputs[]);
// Change the training rate
void Perceptron_setTrainingRate(Perceptron *perceptron, double trainingRate);
// Train perceptron according to an input vector and its expected result (result should be 0 or 1)
void Perceptron_train(Perceptron *perceptron, const double inputs[], int expectedResult);
// Get the result for an input vector (result is 0 or 1)
// Returns 1 in case that **Perceptron_getValue(perceptron, inputs)** is higher or equals to the thresholds. Returns 0 otherwise
int Perceptron_getResult(const Perceptron *perceptron, const double inputs[]);
// Get the weight at a specific index in the weights vector
double Perceptron_getWeightAt(const Perceptron *perceptron, unsigned index);
// Get the weights vector
const double *Perceptron_getWeights(const Perceptron *perceptron);
// Get the number of the inputs in the perceptron
unsigned Perceptron_getNumOfInputs(const Perceptron *perceptron);
// Get the threshold
double Perceptron_getThreshold(const Perceptron *perceptron);
// Get the training rate
double Perceptron_getTrainingRate(const Perceptron *perceptron);
// Change the weight at a specific index
void Perceptron_setWeightAt(Perceptron *perceptron, unsigned index, double weight);
// Change the weights vector (copying, not replacing pointers)
void Perceptron_setWeights(Perceptron *perceptron, const double *weights);
// Change the threshold
void Perceptron_setThreshold(Perceptron *perceptron, double threshold);