Binary Perceptron in C#

Download Perceptron C# Project

Perceptron is a machine learning algorithm used for classification of data. It classifies a number of inputs into several possible non-binary outputs. To read more about perceptrons please visit the Perceptron Wikipedia entry.

SimplePerceptronDiagram


Above figure is a visual representation of a perceptron. Inputs (X0 to Xn) are multiplied by a corresponding scalar weights (W0 to Wn) and the results are summed up. The weighted input sum is passed through an activation function which may be as simple as comparing the sum to a scalar threshold (ex. sum > 0.5). If the sum is greater than the threshold perceptron returns 1 otherwise it returns 0.

Training

Set of data called training set with a number of inputs and expected outputs is used to train a perceptron. When a perceptron is created all its weights may be set to 0 or may be randomized. Either way, the calculated results of a perceptron will most likely not match the expected outputs of the training set. In order to get the expected output from a perceptron the weights need to be adjusted if the calculated result does not match expected result.

Weights are adjusted by:

  1. Calculating the error by subtracting expected from calculated result error = expectedResult - calculatedResult;
  2. To each weight add error multiplied by the input related to the weight multiplied by a learning rate Weights[i] += LearningRate * error * inputs[i]; Learning rate is a scalar between 0 and 1.

Pseudo code for weight adjustment is located below:

Limitations

PerceptronANDChartAND Function

Simple perceptrons only work linearly separable data. This type of perceptron cannot learn XOR function since the groups are not linearly separable.

Code

Download Perceptron C# Project

Example usage for training a perceptron to simulate a NAND gate is located below. The learning method is perceptron.Learn(item.Output, item.Inputs); and is called for as long as the calculated result does not match the expected result. After the learning is done perceptron.GetResult(...); will return the correct result.

Binary perceptron code is located below.