~1 min read

XOR Gate

Analytical representation

https://upload.wikimedia.org/wikipedia/commons/a/a2/254px_3gate_XOR.jpg

import numpy as np

def AND_gate(x1,x2): 
    x = np.array([x1,x2])    
    weight = np.array([0.5,0.5])    
    bias = -0.7    
    y = np.matmul(x,weight) + bias    
    return Step_Function(y) 

def OR_gate(x1,x2):    
    x = None    
    x = np.array([x1,x2])    
    weight = np.array([0.5,0.5])    
    bias = -0.3    
    y = np.matmul(x,weight) + bias    
    return Step_Function(y) 

def NAND_gate(x1,x2):    
    x = np.array([x1,x2])    
    weight = np.array([-0.5,-0.5])    
    bias = 0.7    
    y = np.matmul(x,weight) + bias    
    return Step_Function(y) 

def Step_Function(y):    
    return 1 if y>=0 else 0

def XOR_gate(x1, x2):    
    y1 = NAND_gate(x1,x2)
    y2 = OR_gate(x1,x2)    
    z = AND_gate(y1,y2)    
    return z

def main():    
    array = np.array([[0,0], [0,1], [1,0], [1,1]])
    print('XOR Gate 출력')    
    for x1, x2 in array:
        print('Input: ',x1, x2, ', Output: ', XOR_gate(x1, x2))

if __name__ == "__main__":
    main()