월레스와 그로밋: 코딩의 날

퍼셉트론(Perceptron _ AND, OR, NAND, XOR) 본문

Python/Deep Learning

퍼셉트론(Perceptron _ AND, OR, NAND, XOR)

구운 감자 2025. 2. 7. 19:53
import numpy as np

def AND(x1, x2):
    x = np.array([x1, x2])
    w = np.array([1.0, 1.0])
    b = -1.5
    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([1.0, 1.0])
    b = -0.5
    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([-1.0, -1.0])
    b = 1.5
    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
    
if __name__ == '__main__':
    for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:
        y = XOR(xs[0], xs[1])   # AND, OR, NAND, XOR
        print(str(xs) + "->" + str(y))

output

[AND_Gate]

(0, 0)->0
(1, 0)->0
(0, 1)->0
(1, 1)->1

[OR_Gate]

(0, 0)->0
(1, 0)->1
(0, 1)->1
(1, 1)->1

[NAND_Gate]

(0, 0)->1
(1, 0)->1
(0, 1)->1
(1, 1)->0

[XOR_Gate]

(0, 0)->0
(1, 0)->1
(0, 1)->1
(1, 1)->0