[DL] XOR gate

SeungHyun·2023년 10월 9일
0

DL

목록 보기
1/5


출처: https://roseline.oopy.io/dev/logic-gate-xor

0. 개요

딥러닝 학습시 초기에 다층 퍼셉트론과 함께 배우는 XOR_gate에 대한 간단한 정리임.

  • XOR = AND(NAND, OR) 으로 정리할 수 있음.

1. OR, AND gate

1-1. OR_gate

코드

def OR(X1, X2):
    X = np.array([X1, X2])
    W = np.array([0.5, 0.5])
    b = -0.4

    output = np.sum(X*W) + b
    return 1 if output>0 else 0
    

print("OR_gate")
print(OR(1,1))
print(OR(1,0))
print(OR(0,1))
print(OR(0,0))

결과

X1X2output
111
101
011
000

1-2. AND_gate

코드

def AND(X1, X2):
    X = np.array([X1, X2])
    W = np.array([0.5, 0.5])
    b = -0.6

    output = np.sum(X*W) + b
    return 1 if output>0 else 0
    
    
print("AND_gate")
print(AND(1,1))
print(AND(1,0))
print(AND(0,1))
print(AND(0,0))

결과

X1X2output
111
100
010
000

2. NOR, NAND gate

2-1. NOR_gate

코드

def NOR(X1, X2):
    X = np.array([X1, X2])
    W = np.array([-0.5, -0.5])
    b = 0.4

    output = np.sum(X*W) + b
    return 1 if output>0 else 0
    
   
print("NOR_gate")
print(NOR(1,1))
print(NOR(1,0))
print(NOR(0,1))
print(NOR(0,0))

결과

X1X2output
110
100
010
001

2-2. NAND_gate

코드

def NAND(X1, X2):
    X = np.array([X1, X2])
    W = np.array([0.5, 0.5])
    b = -0.9

    output = np.sum(X*W) + b
    return 0 if output>0 else 1
    
    
print("NAND_gate")
print(NAND(1,0))
print(NAND(0,1))
print(NAND(0,0))
print(NAND(1,1))

결과

X1X2output
110
101
011
001

3. XOR gate

3-1. 코드

def XOR(X1, X2):
    X1_ = NAND(X1, X2)
    X2_ = OR(X1, X2)

    return AND(X1_, X2_)
    
    
print("XOR_gate")
print(XOR(1,1))
print(XOR(1,0))
print(XOR(0,1))
print(XOR(0,0))

3-2. 결과

XOR_gate

X1X2output
110
101
011
000
profile
어디로 가야하오

0개의 댓글