感知器中的错误分类错误
Misclassification error in perceptron
我正在使用 Python 和 Numpy 实现感知器。我正在使用 this Wikipedia article 中描述的算法对其进行训练,但生成的权重向量并未正确 class 验证样本向量,甚至训练集中的样本向量也不正确。
我写的这段训练代码:
epochs = 50
unit_step = lambda x: 0 if x < center else (0.5 if x == center else 1)
def train(data_set, labels):
number_of_samples, dimension = data_set.shape
# Generate the augmented data set, adding a column of '1's
augmented_data_set = np.ones((number_of_samples, dimension + 1))
augmented_data_set[:,:-1] = data_set
w = 1e-6 * np.random.rand(dimension + 1)
for _ in xrange(epochs):
for sample, target in zip(augmented_data_set, labels):
predicted_output = unit_step(np.dot(w, sample))
update = (target - predicted_output) * sample
w += update
return w
在此之后,我将学习 AND 逻辑函数所需的向量设置为训练集:
training_set = np.array([[0,0],[0,1],[1,0],[1,1]])
及其对应的class标签为:
labels = np.array([-1,-1,-1,1])
其中 -1 表示 False,1 表示 True。
在 运行 w = train(training_set, labels)
之后,我测试了生成的权重向量并得到了错误的结果:
np.dot(w, [0,0,1]) = -1.0099996334232431
np.dot(w, [0,1,1]) = -1.0099991616719257
np.dot(w, [1,0,1]) = -1.009999277692496
np.dot(w, [1,0,1]) = -1.009999277692496
这里的错误是最后一个案例应该return一个大于0接近1的值。我看不清楚这里发生了什么。我错过了什么?
提前致谢
最明显的错误是训练集标签(-1 和 1)与模型输出(0、0.5 和 1.0)之间缺乏统一。将两者都更改为 0 和 1。
我正在使用 Python 和 Numpy 实现感知器。我正在使用 this Wikipedia article 中描述的算法对其进行训练,但生成的权重向量并未正确 class 验证样本向量,甚至训练集中的样本向量也不正确。
我写的这段训练代码:
epochs = 50
unit_step = lambda x: 0 if x < center else (0.5 if x == center else 1)
def train(data_set, labels):
number_of_samples, dimension = data_set.shape
# Generate the augmented data set, adding a column of '1's
augmented_data_set = np.ones((number_of_samples, dimension + 1))
augmented_data_set[:,:-1] = data_set
w = 1e-6 * np.random.rand(dimension + 1)
for _ in xrange(epochs):
for sample, target in zip(augmented_data_set, labels):
predicted_output = unit_step(np.dot(w, sample))
update = (target - predicted_output) * sample
w += update
return w
在此之后,我将学习 AND 逻辑函数所需的向量设置为训练集:
training_set = np.array([[0,0],[0,1],[1,0],[1,1]])
及其对应的class标签为:
labels = np.array([-1,-1,-1,1])
其中 -1 表示 False,1 表示 True。
在 运行 w = train(training_set, labels)
之后,我测试了生成的权重向量并得到了错误的结果:
np.dot(w, [0,0,1]) = -1.0099996334232431
np.dot(w, [0,1,1]) = -1.0099991616719257
np.dot(w, [1,0,1]) = -1.009999277692496
np.dot(w, [1,0,1]) = -1.009999277692496
这里的错误是最后一个案例应该return一个大于0接近1的值。我看不清楚这里发生了什么。我错过了什么?
提前致谢
最明显的错误是训练集标签(-1 和 1)与模型输出(0、0.5 和 1.0)之间缺乏统一。将两者都更改为 0 和 1。