-
Notifications
You must be signed in to change notification settings - Fork 0
/
perceptron.py
90 lines (74 loc) · 1.88 KB
/
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
# a simple implementation
import numpy as np
def AND1(x1, x2):
w1, w2, theta = 0.5, 0.5, 0.7
tmp = w1 * x1 + w2 * x2
if tmp >= theta:
return 1
else:
return 0
def OR1(x1, x2):
w1, w2, theta = 0.5, 0.5, 0.4
tmp = w1 * x1 + w2 * x2
if tmp >= theta:
return 1
else:
return 0
def NAND1(x1, x2):
w1, w2, theta = 0.5, 0.5, 0.7
tmp = w1 * x1 + w2 * x2
if tmp >= theta:
return 0
else:
return 1
# thr implementation use weight and bias
def AND(x1, x2):
x = np.array([x1, x2])
w = np.array([0.5, 0.5])
theta = -0.7
tmp = np.sum(w * x) + theta
if tmp >= 0:
return 1
else:
return 0
def OR(x1, x2):
x = np.array([x1, x2])
w = np.array([0.5, 0.5])
theta = -0.4
tmp = np.sum(w * x) + theta
if tmp >= 0:
return 1
else:
return 0
def NAND(x1, x2):
x = np.array([x1, x2])
w = np.array([0.5, 0.5])
theta = -0.7
tmp = np.sum(w * x) + theta
if tmp >= 0:
return 0
else:
return 1
def XOR(x1, x2):
s1 = NAND(x1, x2)
s2 = OR(x1, x2)
y = AND(s1, s2)
return y
if __name__ == "__main__":
invars = np.array([[0, 1], [1, 0], [1, 1], [0, 0]])
for invar in invars:
x1, x2 = invar
out_and1 = AND1(x1, x2)
out_and = AND(x1, x2)
out_or1 = OR1(x1, x2)
out_or = OR(x1, x2)
out_nand1 = NAND1(x1, x2)
out_nand = NAND(x1, x2)
out_xor = XOR(x1, x2)
print(
"****************************************************************")
print(f"the input is {x1,x2}")
print(f"tht output of AND and AND1 is {out_and, out_and1}")
print(f"the output of OR and OR1 is {out_or, out_or1}")
print(f"the output of out_nand and out_nand1 is {out_nand, out_nand1}")
print(f"the output of out_xor is {out_xor}")