Simple example: AND
x1,x2∈x_1, x_2 \inx1,x2∈{0, 1}
y=x1ANDx2y = x_1 AND x_2y=x1ANDx2
hθ(x)=g(−30+20x1+20x2)h_\theta(x) = g(-30 + 20x_1+20x_2)hθ(x)=g(−30+20x1+20x2)
激活函数:
g(x)=11+e−xg(x) = \frac1{1+e^{-x}}g(x)=1+e−x1
x1x_1x1 | x2x_2x2 | hθ(x)h_{\theta}(x)hθ(x) |
---|---|---|
0 | 0 | g(−30)≈0g(-30) \approx0g(−30)≈0 |
0 | 1 | g(−10)≈0g(-10)\approx 0g(−10)≈0 |
1 | 0 | g(−10)≈0g(-10)\approx0g(−10)≈0 |
1 | 1 | g(10)≈1g(10)\approx1g(10)≈1 |
激活函数:
def sigmoid(x):
return 1/(1+np.exp(-x))
权重矩阵:
w = np.array([-30, 20, 20])
w = np.mat(w)
输入函数:
def input(x1, x2):
data = np.array([1, x1, x2])
data = np.mat(data.reshape(3, 1))
return w*data
输出函数:
def out(data): # eg: data为input(1, 1)、input(1, 0)、 input(0, 1)、input(0, 0)
result = sigmoid(data)
if result<0.5:
return 0
else:
return 1
测试: