Perceptron

The Perceptron is one of the fundamental concepts of Neural Networks. Being based off a single neuron, the perceptron is an algorithm for supervised learning of binary classifiers. It is a type of linear classifier, i.e. a classification algorithm that makes its predictions based on a linear predictor function combining a set of weights with the feature vector. The algorithm allows for online learning, in that it processes elements in the training set one at a time.

My first experience of programming ML concepts was the perceptron, training it to take in two binary inputs and predict the output for AND and OR gates.

Python code for Perceptron

TYPE=input("AND/OR") TYPE=TYPE.upper() sampleData=[] if TYPE=="AND": sampleData=[[0,0,0],[0,1,0],[1,0,0],[1,1,1]]#each item includes input1, input2 and the expected result for an AND gate elif TYPE=="OR": sampleData=[[0,0,0],[0,1,1],[1,0,1],[1,1,1]]#each item includes input1, input2 and the expected result for an OR gate else: print("Unrecognised value for TYPE.") exit() threshold=1 weights=[0,0]#intial weights learningRate=0.1 changeMade=True while changeMade==True: changeMade=False for item in sampleData: total=item[0]*weights[0]+item[1]*weights[1]#calculates dot product of two vectors print("dot product:",total) print("expected=",item[2]) modifier=0 if total>=threshold and item[2]==0: modifier=-1 #if dot product>threshold, modifier used to lower weightings changeMade=True elif total<threshold and item[2]==1: modifier=1 #if dot product<threshold, modifier used to increase weightings changeMade=True weights[0]+=modifier*item[0]*learningRate weights[1]+=modifier*item[1]*learningRate print("weight 1:",weights[0]) print("weight 2:",weights[1]) print("") for item in sampleData:#iterates through sampledata using trained weights to see if they produce the same result as the expected result total=item[0]*weights[0]+item[1]*weights[1] output=999 if total>=threshold: output=1 else: output=0 print("input1: "+str(item[0])+" input2: "+str(item[1])+" --> "+str(output)+" (should be "+str(item[2])+")")