def AND (x1, x2):
w1, w2, theta = 0.5, 0.5, 0.7
temp = x1*w1 + x2*w2
if temp <= theta: return 0
elif temp > theta: return 1
print(AND(0, 0))
print(AND(1, 0))
print(AND(0, 1))
print(AND(1, 1))
0 0 0 1
$ y = \begin{cases}0 \;if\;b + w_1x_1 + w_2x_2 <= 0\\ 1 \;if\; b + w_1x_1 + w_2x_2 > 0\end{cases} $
import numpy as np
x = np.array([0, 1]) # input
w = np.array([0.5, 0.5]) # weight
b = -0.7 # bias
print(w*x)
print(np.sum(w*x))
print(np.sum(w*x) + b)
[0. 0.5] 0.5 -0.19999999999999996
def AND(x1, x2):
x = np.array([x1, x2])
w = np.array([0.5, 0.5])
b = -0.7
tmp = np.sum(w*x) + b
if tmp <= 0:
return 0
else:
return 1
print(AND(0, 0))
print(AND(1, 0))
print(AND(0, 1))
print(AND(1, 1))
0 0 0 1
단층 퍼셉트론으로는 XOR gate 구현 불가능
## ✅ 다층 퍼셉트론 ### ▶ XOR gate (O)
def OR(x1, x2): # AND와 weight 만 다름
x = np.array([x1, x2])
w = np.array([0.5, 0.5])
b = -0.2
tmp = np.sum(w*x) + b
if tmp <= 0:
return 0
else:
return 1
def NAND(x1, x2):
x = np.array([x1, x2])
w = np.array([-0.5, -0.5])
b = 0.7
tmp = np.sum(w*x) + b
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
print(XOR(0, 0))
print(XOR(1, 0))
print(XOR(0, 1))
print(XOR(1, 1))
0 1 1 0