def AND(x1, x2): w1, w2, theta = .5, .5, .7 # 가능한 값은 수없이 많음 tmp = x1*w1 + x2*w2 if tmp <= theta: return 0 elif tmp > theta: return 1 print(AND(0,0),AND(1,0),AND(0,1),AND(1,1)) # 0 0 0 1 출력됨 def NAND(x1, x2): w1, w2, theta = -0.5, -0.5, -0.7 if x1*w1 + x2*w2 <= theta: return 0 else: return 1 print(NAND(0,0),NAND(0,1),NAND(1,0),NAND(1,1)) # 1 1 1 0 출력됨 def OR(x1, x2): w1, w2, theta = 0.5, 0.5, 0.1 # 책에 연습해보라고 나와 있어서 내가 고안 if x1*w1 + x2*w2 <= theta: return 0 else: return 1 print(OR(0,0),OR(0,1),OR(1,0),OR(1,1)) # 0 1 1 1 출력됨 # XOR은 단층 퍼셉트론으로는 구현 불가, 다층 퍼셉트론(multi-layer perceptron)을 만들어야 함, 다음과 같이. def XOR(x1, x2): s1 = NAND(x1, x2) s2 = OR(x1, x2) y = AND(s1, s2) return y print(XOR(0,0),XOR(0,1),XOR(1,0),XOR(1,1)) # 0 1 1 0 출력됨