-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxnor.py
More file actions
77 lines (68 loc) · 1.8 KB
/
xnor.py
File metadata and controls
77 lines (68 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#XNOR GATE implementation using Combination of Perceptrons
import numpy as np
import random
# function to predict output value
# if calculated value is less than 0, predict 0
# if calculated value is greater than or equal to 0, predict 1
def predict(X, w, b):
p = np.sign(X.dot(w) + b)
if p == -1:
p = 0
else:
p = 1
return p
# function to train the perceptron
def fit(learning_rate, epoch):
#initialize weights and bias
w1 = np.array([1,1])
b1 = -1
w2 = np.array([1,1])
b2 = -1
w3 = np.array([1,1])
b3 = -1
#loop to update weights based on predictions and actual value
for e in range(epoch):
X1 = random.choice([0,1])
X2 = random.choice([0,1])
Y1 = X1 and X2
Y2 = not (X1 or X2)
Y = Y1 or Y2
Xa = np.array([X1,X2])
Y1p = predict(Xa, w1, b1)
Y2p = predict(Xa, w2, b2)
X = np.array([Y1p,Y2p])
Yp = predict(X, w3, b3)
if Y1p == Y1:
pass
else:
w1 = w1 - learning_rate*(Xa.dot(Y1p-Y1))
b1 = b1 - learning_rate*(Y1p-Y1)
if Y2p == Y2:
pass
else:
w2 = w2 - learning_rate*(Xa.dot(Y2p-Y2))
b2 = b2 - learning_rate*(Y2p-Y2)
if Y == Yp:
continue
else:
w3 = w3 - learning_rate*(X.dot(Yp-Y))
b3 = b3 - learning_rate*(Yp-Y)
return w1, b1, w2, b2, w3, b3
#initialize learning rate and epoch
learning_rate = 0.499
epoch = 100
#call the fit function for training
w1, b1, w2, b2, w3, b3 = fit(learning_rate, epoch)
print("Weights for AND - w1 = ",w1[0], "; w2 = ",w1[1],"; b = ",b1)
print("Weights for NOR - w1 = ",w2[0], "; w2 = ",w2[1],"; b = ",b2)
print("Weights for OR - w1 = ",w3[0], "; w2 = ",w3[1],"; b = ",b3)
print("X1 X2 Y")
X1 = [0,0,1,1]
X2 = [0,1,0,1]
for i in range(4):
Xa = np.array([X1[i],X2[i]])
Y1p = predict(Xa, w1, b1)
Y2p = predict(Xa, w2, b2)
X = np.array([Y1p,Y2p])
Yp = predict(X, w3, b3)
print(X1[i]," ",X2[i]," ",Yp)