与门、与非门、或门具有相同结构的感知机,区别只在于权重参数的值
# -*- coding: utf-8 -*-
def AND(x1,x2):
w1, w2, theta = 0.5, 0.5, 0.7
tmp = x1*w1 + x2*w2
if tmp < theta:
return 0
elif tmp >= theta:
return 1
AND(0,0) #0
AND(1,0) #0
AND(0,1) #0
AND(1,1) #1
'''引入numpy的各种门'''
import numpy as np
def PAND(x1,x2):
x = np.array([x1,x2])
w = np.array([0.5,0.5])
b = -0.7
tmp = np.sum(x*w) + b
if tmp <= 0:
return 0
elif tmp > 0:
return 1
PAND(0,0) #0
PAND(1,0) #0
PAND(0,1) #0
PAND(1,1) #1
def NOAND(x1,x2):
x = np.array([x1,x2])
w = np.array([-0.5,-0.5])
b = 0.7
tmp = np.sum(x*w) + b
if tmp <= 0:
return 0
elif tmp > 0:
return 1
NOAND(0,0) #1
NOAND(1,0) #1
NOAND(0,1) #1
NOAND(1,1) #0
def OR(x1,x2):
x = np.array([x1,x2])
w = np.array([0.5,0.5])
b = -0.2 #只要小于0.5就可以啊
tmp = np.sum(x*w) + b
if tmp <= 0:
return 0
elif tmp > 0:
return 1
OR(0,0) #0
OR(1,0) #1
OR(0,1) #1
OR(1,1) #1
异或门的实现
def XOR(x1,x2):
s1 = NOAND(x1,x2)
s2 = OR(x1,x2)
y = PAND(s1,s2)
return y
XOR(0,0) #0
XOR(1,0) #1
XOR(0,1) #1
XOR(1,1) #0