这是我第一次从头开始编写感知器学习算法。我以前使用过现成的ML解决方案,但我想真正理解它并自己编写。由于某些原因,我的错误率一直在增加而不是减少。看来我的算法是发散的而不是收敛的。我写在一个宽容的范围内,因为它有时会接近,但从来没有完全击中目标。在
我有三个权重:1代表偏差,2代表X和Y。在
我发现我的区别在于:
D = (weight0 + weight1 * Xi) + (weight2 * Yi)
如果判别式与预期输出不匹配,则我使用以下内容更新权重:
注:假设c和k是预定义常数,d=预期输出
w0 = w0 + cdk
,w1 = w1 + cdXi
,w2 = w2 + cdYi
下面是我在Python中的实现:
def weightsUpdate(weights, constantC, constantK, classificationd, x, y):
weights[0] = weights[0] + constantC * classificationd * constantK # w0 = w0 + cdk
weights[1] = weights[1] + constantC * classificationd * x #w1 = w1 + cdx
weights[2] = weights[2] + constantC * classificationd * y #w2 = w2 + cdy
return weights
def trainModel(df, weights, constantC, constantK, maxIter, threshHold):
#grab the values in a list
x = df['X'].values
y = df['Y'].values
d = df['Class'].values
#define some variables to keep track
numTurns = 0
while numTurns < maxIter:
errorRate = 0
falsePosNeg = 0
truePosNeg = 0
'''assign som threshhold values. must accomodate for slight variance.'''
posThreshHoldCeiling = 1 + threshHold
posThreshHoldFloor = 1 - threshHold
negThreshHoldFloor = -1 - threshHold
negThreshHoldCeiling = -1 + threshHold
for i in range(len(x)):
''' calculate the discriminant D = w0 + w1*xi + w2*yi '''
discriminant = weights[0] + (weights[1] * x[i]) + (weights[2] * y[i])
'''if the discriminant is not correct when compared to the correct output'''
if ((discriminant >= posThreshHoldFloor and discriminant <= posThreshHoldCeiling) or
(discriminant >= negThreshHoldFloor and discriminant <= negThreshHoldCeiling)):
truePosNeg += 1
#weights = weightsUpdate(weights, constantC, constantK, d[i], x[i], y[i])
else:
'''update the weights'''
weights = weightsUpdate(weights, constantC, constantK, d[i], x[i], y[i])
falsePosNeg += 1
numTurns += 1 #increase number of turns by 1 iteration
print("Number of False Positive/Negative: " + str(falsePosNeg))
print("Number of True Positive/Negative: " + str(truePosNeg))
errorRate = falsePosNeg / len(x) * 100
print("Error rate: " + str(errorRate) + "%")
'''add stop conditions'''
if (errorRate < 25):
break
else:
continue
谢谢你的帮助。在
感知器学习算法(PLA)不需要阈值。{e>只需要收敛于PLA}的实际输出。下面是
trainModel()
的修改版本。在相关问题 更多 >
编程相关推荐