第2章
感覺機的實作
使用感覺機實作與門、與非門、或門、異或門
我們給三種電路分别設定的權重和偏置為:
AND | NAND | OR | |
---|---|---|---|
w1 | 0.5 | -0.5 | 0.5 |
w2 | 0.5 | -0.5 | 0.5 |
b | -0.7 | 0.7 | -0.2 |
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
else:
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:
print(0)
else:
print(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:
print(0)
else:
print(1)
但是單層感覺機隻能表示線性空間,異或門就不能被表示。于是出現了多層感覺器。
經過 與非+或 => 與 就可以實作異或:
def XOR(x1, x2):
s1 = NAND(x1, x2)
s2 = OR(x1, x2)
y = AND(s1, s2)
return y