使用感知机实现与 非门
# 与门
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)
# 稍加改变 使用偏置 偏置是决定什么时候可以激活
import numpy as np
x = np.array([0,1])
w = np.array([0.5,0.5])
b = -0.7
result = np.sum(w*x) + b
print(result)
# 与非门
def NAND(x1,x2):
x = np.array([x1,x2])
w = np.array([-0.5,-0.5]) # 或门仅权重和偏置不同
b = -0.7
tmp = np.sum(W*x) + b
if tmp<=0:
return 0
elif tmp>0:
return 1
# 通过组合也可实现异或门,不做代码参考了