퍼셉트론 복습 겸 AND, NAND, OR 구현 정리
# 1) x1과 x2를 인수로 받는 AND
def AND(x1, x2):
w1, w2, theta = 0.5, 0.5, 0.7
tmp = x1*w1 + x2*w2
if tmp <= theta:
return 0
elif tmp > theta:
return 1
print(AND(0, 0))
print(AND(0, 1))
print(AND(1, 0))
print(AND(1, 1))
결과값
# 2) 가중치와 편향을 numpy로 구현
import numpy as np
x = np.array([0, 1]) # 입력
w = np.array([0.5, 0.5]) # 가중치
b = -0.7 # 편향
np.sum(w*x) + b # 부동소수점 수에 의한 연산 오차
결과값
# 1) + 2) 둘을 합친 가중치와 편향을 도입한 AND
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(0, 1))
print(AND(1, 0))
print(AND(1, 1))
결과값
import numpy as np
# 가중치와 편향을 도입한 NAND, OR
def NAND(x1, x2):
x = np.array([x1, x2])
w = np.array([-0.5, -0.5]) # AND와 가중치만 다름
b = 0.7 # 편향
tmp = np.sum(w*x) + b
if tmp <= 0:
return 0
else:
return 1
def OR(x1, x2):
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
결과값
AND, NAND, OR은 모두 선형 영역
당신의 시간이 헛되지 않는 글이 되겠습니다.
I'll write something that won't waste your time.