感知器:每个样本数据的权重,或一个共同的权重

Perceptron : Weight for each sample data , or one common weight

通过感知学习,我对初始化和更新权重真的很困惑。如果我有一个包含 2 个输入 x0 和 x1 的示例数据,并且我有这 2 个输入的 80 行,因此矩阵为 80x2。

我是否需要将权重初始化为 80x2 矩阵或仅 2 个值 w0 和 w1?感知器学习的最终目标是找到适合所有 80 个输入样本行的 2 个权重 w0 和 w1 吗?

我有以下代码,尽管进行了多达 10,000 次迭代,但我的错误从未达到 0。

x=input matrix of 80x2
y=output matrix of 80x1
n = number of iterations
w=[0.1,0.1]  
learningRate = 0.1
for i in range(n): 
    expectedT = y.transpose();
    xT = x.transpose()
    prediction =  np.dot (w,xT) 

    for i in range (len(x)):    
        if prediction[i] >= 0:                               
                 ypred[i] = 1                               
        else:                                   
                ypred[i] = 0

    error = expectedT - ypred

    # updating the weights
    w = np.add(w,learningRate*(np.dot(error,x)))
    globalError = globalError + np.square(error)

对于每个特征,您将有一个权重。因此你有两个特征和两个权重。它还有助于引入增加另一个权重的偏差。有关偏差的更多信息,请查看此 Role of Bias in Neural Networks. The weights indeed should learn how to fit the sample data best. Depending on the data this can mean that you will never reach error of 0. For example a single layer perceptron can not learn an XOR gate when using a monotonic activation function. (solving XOR with single layer perceptron)。

对于您的示例,我会推荐两件事。引入偏差并在误差低于某个阈值或误差为 0 时停止训练。

我完成了您的示例以学习逻辑与门:

# AND input and output
x = np.array([[0,0],[0,1],[1,0],[1,1]])
y = np.array([0,1,1,1])

n = 1000
w=[0.1,0.1,0.1]  
learningRate = 0.01
globalError = 0

def predict(X):
    prediction =  np.dot(w[0:2],X) + w[2] 
    ypred = np.zeros(len(y))
    for i in range (len(y)):    
        if prediction[i] >= 0:                               
                ypred[i] = 1                               
        else:                                   
                ypred[i] = 0
    return ypred

for i in range(n): 
    expectedT = y.transpose();
    xT = x.transpose()
    ypred = predict(xT)

    error = expectedT - ypred
    if sum(error) == 0:
        break

    # updating the weights
    w[0:2] = np.add(w[0:2],learningRate*(np.dot(error,x)))
    w[2] += learningRate*sum(error)
    globalError = globalError + np.square(error)

训练后误差为0

print(error)
# [0. 0. 0. 0.]

而权重如下

print(w)
#[0.1, 0.1, -0.00999999999999999]

感知器现在可以用作与门:

predict(x.transpose())
#array([0., 1., 1., 1.])

希望对您有所帮助