diff --git a/Neural Networks/src/delta_learning.py b/Neural Networks/src/delta_learning.py new file mode 100644 index 00000000..25971c5f --- /dev/null +++ b/Neural Networks/src/delta_learning.py @@ -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}') diff --git a/Neural Networks/src/perceptron_learning.py b/Neural Networks/src/perceptron_learning.py new file mode 100644 index 00000000..c3e673d7 --- /dev/null +++ b/Neural Networks/src/perceptron_learning.py @@ -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 \ No newline at end of file