본문 바로가기
python/deep learning

NAND NOR

by Falto 2021. 8. 9.

NAND 게이트의 조합만으로 모든 게이트를 만들 수 있다. 아래는 NAND 게이트의 퍼셉트론만으로 모든 게이트를 구현한 python 코드이다.

def NAND(x1, x2):
    a0 = -0.5 * (x1 + x2) + 0.7
    if a0 > 0:
        return 1
    else:
        return 0

def NOT(x):
    return NAND(x, x)

def AND(x1, x2):
    return NOT(NAND(x1, x2))

def OR(x1, x2):
    return NAND(NOT(x1), NOT(x2))

def NOR(x1, x2):
    return NOT(OR(x1, x2))

def XOR(x1, x2):
    return AND(NAND(x1, x2), OR(x1, x2))

def XNOR(x1, x2):
    return NOT(XOR(x1, x2))

for func in [NAND, AND, OR, NOR, XOR, XNOR]:
    print(func.__name__, end=' ')
    for x1, x2 in [[0, 0], [0, 1], [1, 0], [1, 1]]:
        print(func(x1, x2), end=' ')
    print('')

# 출력
NAND 1 1 1 0 
AND 0 0 0 1 
OR 0 1 1 1
NOR 1 0 0 0
XOR 0 1 1 0
XNOR 1 0 0 1

NOR 게이트도 마찬가지다.

def NOR(x1, x2):
    a0 = -0.5 * (x1 + x2) + 0.2
    if a0 > 0:
        return 1
    else:
        return 0

def NOT(x):
    return NOR(x, x)

def OR(x1, x2):
    return NOT(NOR(x1, x2))

def AND(x1, x2):
    return NOR(NOT(x1), NOT(x2))

def NAND(x1, x2):
    return NOT(AND(x1, x2))

def XOR(x1, x2):
    return AND(NAND(x1, x2), OR(x1, x2))

def XNOR(x1, x2):
    return NOT(XOR(x1, x2))

for func in [NOR, OR, AND, NAND, XOR, XNOR]:
    print(func.__name__, end=' ')
    for x1, x2 in [[0, 0], [0, 1], [1, 0], [1, 1]]:
        print(func(x1, x2), end=' ')
    print('')

# 출력
NOR 1 0 0 0 
OR 0 1 1 1 
AND 0 0 0 1
NAND 1 1 1 0
XOR 0 1 1 0
XNOR 1 0 0 1

NAND 게이트와 NOR 게이트의 공통점은 무엇일까? 둘다 입력이 모두 0일 때 1을 출력하면서 입력이 모두 1일 때 0을 출력한다. 그래서 NAND(x, x) = NOR(x, x) = NOT(x) 이 성립하는 것이다. NOT 게이트를 만들 수 있기 때문에 NAND 게이트는 자기 자신의 부정인 AND 게이트, NOR 게이트는 자기 자신의 부정인 OR 게이트를 만들 수 있다.

'python > deep learning' 카테고리의 다른 글

softmax 함수의 장점  (0) 2021.08.10
python 경사 하강법  (0) 2021.08.06

댓글