Skip to content

Instantly share code, notes, and snippets.

@deep5050
Created September 27, 2019 03:00
Show Gist options
  • Save deep5050/d7ca84aeb08716a4c2e9d3223b6a6b05 to your computer and use it in GitHub Desktop.
Save deep5050/d7ca84aeb08716a4c2e9d3223b6a6b05 to your computer and use it in GitHub Desktop.
A simple single layer ANN ( perceptron) to realize logic gates function
import numpy as np
import matplotlib.pyplot as plt
class Perceptron(object):
"""
This class defines a single layer perceptron with 3 inputs ;
initial weights will be set to 0;
one extra bias w[0] will be added;
Params
------
eta : float
the learning rate to the model ( between 0 to 1)
max_iter : int
maximum number of iterration allowed for the prediction . default values is set to 50
Attributes:
---------
weights : array
stores the weights to the percpetron
len(weoights) willl be 1 extra to the len(inputs)
errors_ : array
stores the error in each epoch for simple ploting purpose
"""
def __init__(self,eta=0.1,max_iter = 50):
self.eta = eta
self.max_iter = max_iter
def net_input(self,x):
"""
cal culate the net input
:return:
"""
return np.dot(x,self.weights[1:]) + self.weights[0]
def predict(self,x):
# return np.where(self.net_input(x) >= 0.0 ,1 ,0)
# if traing set contains 0 the use this
# if traing set contain -1 instead of 0 use the below
return np.where(self.net_input(x) >= 0.0, 1, -1)
def fit(self,X,Y):
"""
:param X:
the input training set to the model
:param Y:
input output training set to the model
:return: self(object)
"""
# self.weights = np.array([0.001,0.5,0.9,0.3])
self.weights = np.zeros(1+ X.shape[1]) # initialze weights to 0 including the bias
self.errors_ = []
for iter in range(self.max_iter):
print("----------------------------------------------------")
print(" In iter no.:-> ",iter)
print()
errors = 0
for x_i,target in zip(X,Y):
predicted = self.predict(x_i)
update = self.eta * (target- predicted)
print("Target:->",target," Predicted:->",predicted," Update:->",update)
self.weights[1:] += update * x_i
self.weights[0] += update
if update !=0.0:
errors += update
print("Total error:->",errors)
self.errors_.append(errors)
print("Weights:->", self.weights)
if errors == 0.0 :break
plt.plot(self.errors_,c='r')
plt.ylabel("Error in each epoch")
plt.xlabel("Epoch no.")
# plt.show()
# train_x = np.array([
# [0, 0, 0],
# [0, 0, 1],
# [0, 1, 0],
# [0, 1, 1],
# [1, 0, 0],
# [1, 0, 1],
# [1, 1, 0],
# [1, 1, 1]
# ])
#
# train_y = np.array([0, 0, 0, 0, 0, 0, 0, 1])
train_x = np.array([
[-1, -1, -1],
[-1, -1, 1],
[-1, 1, -1],
[-1, 1, 1],
[1, -1, -1],
[1, -1, 1],
[1, 1, -1],
[1, 1, 1]
])
And_Y = np.array([-1, -1, -1, -1, -1, -1, -1, 1])
OR_Y = np.array([-1, 1, 1, 1, 1, 1, 1, 1])
NAND_Y = np.array([1, 1, 1, 1, 1, 1, 1, -1])
NOR_y = np.array([1, -1, -1, -1, -1, -1, -1, -1])
AND = Perceptron(0.3, 100)
NAND = Perceptron(0.3, 100)
OR = Perceptron(0.3, 100)
NOR = Perceptron(0.3, 100)
print("\n############# LOGIC GATE : AND ####################\n")
AND.fit(train_x, And_Y)
print("\n################# LOGIC GATE : NAND #####################\n")
NAND.fit(train_x,NAND_Y)
print("\n################## LOGIC GATE : OR ########################\n")
OR.fit(train_x,OR_Y)
print("\n################## LOGIC GATE : NOR ########################\n")
NOR.fit(train_x,NOR_y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment