-
Notifications
You must be signed in to change notification settings - Fork 0
/
AND_OR_Perceptron.py
160 lines (118 loc) · 3.2 KB
/
AND_OR_Perceptron.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import numpy as np
import matplotlib.pyplot as plt
#Activation function, we do not use step function becauze it is not differentiable
def Sigmoid(z):
return 1/(1+np.exp(-z))
# dSigmoid/dz
def d_Sigmoid(z):
return z*(1-z)
# Binary Ceoss Entropy loss
def Criterion(Yhat, Y):
return -Y*np.log(Yhat)-(1-Y)*np.log(1-Yhat)
# dCriterion/dYhat
def d_Criterion(Yhat , Y):
return -(Y/Yhat)+((1-Y)/(1-Yhat))
def Train(X,Y,W_1,W_2,b, lr , LOSS):
for epoch in range(EPOCH):
# Shuffle the training data
random_index = np.arange(X.shape[0])
np.random.shuffle(random_index)
# loss list
L = []
# loop over data
for i in random_index:
x = X[i]
# Forward pass
Yhat = Sigmoid(W_1*x[0]+W_2*x[1]+b)
loss = Criterion(Yhat, Y[i])
L.append(loss)
# Derivatives using chain rule (Back-propagation)
dW_1 = d_Criterion(Yhat , Y[i])*d_Sigmoid(Yhat)*x[0]
dW_2 = d_Criterion(Yhat , Y[i])*d_Sigmoid(Yhat)*x[1]
db = d_Criterion(Yhat , Y[i])*d_Sigmoid(Yhat)
# Updating
W_1 = W_1 - lr*dW_1
W_2 = W_2 - lr*dW_2
b = b - lr*db
LOSS.append(np.mean(L))
return W_1, W_2,b, LOSS
"""#input for AND
X = np.array([[0,0],[0,1],[1,0],[1,1]])
Y = np.array([0,0,0,1])
"""
#input for OR
X = np.array([[0,0],[0,1],[1,0],[1,1]])
Y = np.array([0,1,1,1])
"""#input for NAND
X = np.array([[0,0],[0,1],[1,0],[1,1]])
Y = np.array([1,1,1,0])
"""
"""#input for NOR
X = np.array([[0,0],[0,1],[1,0],[1,1]])
Y = np.array([1,0,0,0])
"""
"""#input for XOR
X = np.array([[0,0],[0,1],[1,0],[1,1]])
Y = np.array([0,1,1,0])
"""
# weights initialization
W_1 = np.random.uniform(-0.01,0.01, size = (1,))
W_2 = np.random.uniform(-0.01,0.01, size = (1,))
b = np.random.uniform(-0.01,0.01, size = (1,))
# training parameters
EPOCH = 4000
lr = 0.01
LOSS = []
W_1, W_2,b, LOSS = Train(X,Y,W_1,W_2,b, lr , LOSS)
print("W1 = " , W_1 , "W2 = ", W_2 , "b = ", b)
# plot geometric data and boundary
plt.figure(figsize = (3,3))
plt.grid()
"""# AND
plt.title("AND GATE")
plt.scatter(0,0, c= 'r', label = "class 0")
plt.scatter(0,1, c= 'r', label = "class 0")
plt.scatter(1,0, c= 'r', label = "class 0")
plt.scatter(1,1, c= 'b', label = "class 1")
"""
# OR
plt.title("OR GATE")
plt.scatter(0,0, c= 'r', label = "class 0")
plt.scatter(0,1, c= 'b', label = "class 1")
plt.scatter(1,0, c= 'b', label = "class 1")
plt.scatter(1,1, c= 'b', label = "class 1")
"""# NAND
plt.title("NAND GATE")
plt.scatter(0,0, c= 'b', label = "class 1")
plt.scatter(0,1, c= 'b', label = "class 1")
plt.scatter(1,0, c= 'b', label = "class 1")
plt.scatter(1,1, c= 'r', label = "class 0")
"""
"""# NOR
plt.title("NOR GATE")
plt.scatter(0,0, c= 'b', label = "class 1")
plt.scatter(0,1, c= 'r', label = "class 0")
plt.scatter(1,0, c= 'r', label = "class 0")
plt.scatter(1,1, c= 'r', label = "class 0")
"""
"""# XOR
plt.title("NOR GATE")
plt.scatter(0,0, c= 'b', label = "class 0")
plt.scatter(0,1, c= 'r', label = "class 1")
plt.scatter(1,0, c= 'r', label = "class 1")
plt.scatter(1,1, c= 'r', label = "class 0")
"""
X_1 = np.arange(-1,2, 0.1)
X_2 = -(W_1/W_2)*X_1-(b/W_2)
plt.xlabel("X1")
plt.ylabel("X2")
plt.xlim((-1,2))
plt.ylim((-1,2))
plt.plot(X_2, X_1, c="g")
# plot loss
plt.figure()
plt.grid()
plt.xlabel("Epoch")
plt.ylabel("training loss")
plt.plot(LOSS , c = 'g')
plt.show()