Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Perceptron Learning and Delta Learning Rule Algorithm #190

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Neural Networks/src/delta_learning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from math import exp


def mul(l1, l2):
return round(sum(a*b for a,b in zip(l1, l2)), 3)

def sgn(x):
return 1 if x > 0 else -1
def imul(x, a):
return [round(a*xi,3) for xi in x]
def add(l1, l2):
return [round(a+b,3) for a,b in zip(l1, l2)]
def func(net):
# bipolar continuous
return 2 / (1 + exp(-net)) - 1
def funcdash(o):
return (1 - o*o)/2

if __name__ == '__main__':
c = 0.1
n = int(input('Enter no of input:'))
xn, dn = [], []
for i in range(n):
xi = list(map(float, input(f'Enter x{i}: ').strip().split(' ')))
di = int(input('Enter desired output:'))
xn.append(xi); dn.append(di)
w = list(map(float, input('Enter initial weights:').split(' ')))
for xi, di in zip(xn, dn):
# print(f'Input: {xi}')
net = mul(w, xi)
# print(f'Expected output: {di}, Actual output: {net}')

oi = round(func(net), 3)
fnetdash = round(funcdash(oi), 3)
print(f'oi = {oi}, fnetdash = {fnetdash}')

xi = imul(xi, c * (di - oi) * fnetdash)
w = add(w, xi)
print(f'Updated weight: {w}')
39 changes: 39 additions & 0 deletions Neural Networks/src/perceptron_learning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import math


def sgn(x):
return 1 if x>0 else -1

def add(a, b):
return [round(ai+bi, 2) for ai, bi in zip(a, b)]

def var_mul(x, a):
return [round(x*ai, 2) for ai in a]

def mult(a, b):
return sum([ai*bi for ai, bi in zip(a, b)])

if __name__ == '__main__':
c = 0.1
n = int(input('Enter no of input:'))
x, d = [],[]
for i in range(n):
xi = list(map(float, input(f'Enter x{i}: ').strip().split(' ')))
di = int(input('Enter desired output:'))
x.append(xi); d.append(di)

w = list(map(float, input('Enter initial weights:').split(' ')))


for xi, di in zip(x, d):
net = mult(xi, w)
if(sgn(net) != sgn(di)):
print("Correction Needed...")
w = add(w, var_mul(c*(di - sgn(net)), xi))
print("X: ", xi)
print(f"W: {w}\n")
else:
print("Correction Not Needed..")
print("X: ", xi)
print(f"W: {w}\n")
continue