感知机接收多个输入信号,输出一个信号。
感知机的信号只有1和0两种取值,0对应不传递信号,1对应传递信号。
下图为一个接收两个输入信号的感知机。其中x1、x2是输入信号,y是输出信号,w1、w2是权重(w是weight的首字母)。图中的○称为“神经元”或者“节点”。输入信号被送往神经元时,会被乘以固定的权重,神经元会计算传送过来的信号的总和,只有当这个总和超过了某个阈值θ时,才会输出1。此过程也称为“神经元被激活”
输入信号都有各自固定的权重,权重发挥着控制各个信号的重要性的作用,权重越大,对应该权重的信号的重要性就越高。
权重相当于电路里的电导。电导是描述某一种导体传输电流能力强弱程度,电导越大,通过的电流越大;在感知机中,权重越大,通过的信号就越大。
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
print(AND(0, 0))
print(AND(0, 1))
print(AND(1, 0))
print(AND(1, 1))
0
0
0
1
import numpy as np
x = np.array([0, 1]) # 输入
w = np.array([0.5, 0.5]) # 权重
b = -0.7 # 偏置
print(np.sum(w * x) + b)
-0.19999999999999996
import numpy as np
def AND(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
import numpy as np
def NAND(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
import numpy as np
def OR(x1, x2):
x = np.array([x1, x2])
w = np.array([0.5, 0.5])
b = -0.2
tmp = np.sum(x * w) + b
if tmp <= 0:
return 0
elif tmp > 0:
return 1
import numpy as np
def AND(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
def NAND(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
def OR(x1, x2):
x = np.array([x1, x2])
w = np.array([0.5, 0.5])
b = -0.2
tmp = np.sum(x * w) + b
if tmp <= 0:
return 0
elif tmp > 0:
return 1
def XOR(x1, x2):
s1 = NAND(x1, x2)
s2 = OR(x1, x2)
y = AND(s1, s2)
return y
if __name__ == '__main__':
print(XOR(0, 0))
print(XOR(0, 1))
print(XOR(1, 0))
print(XOR(1, 1))