基于LMS的双月数据python训练感知器模型

2024-05-13 13:00:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用LMS(最小均方)算法创建一个在线版本(不是批处理)的感知器模型。LMS也被称为阿达林(我猜)。我使用的是双月数据集,其中包括-1(不是0)和+1类用于培训:

Double Moon seems like that:

我从Haykin的书中实现了LMS的python代码:

LMS description from Haykin's Neural Networks and Learning Machines book :

下面是我的python代码

# add 1 for bias
xtrain = np.column_stack(([float(1) for i in x],x))
weights = np.array([0.0 for k in range(len(np.transpose(xtrain)))])
#learning rate
lr = 0.01
epoch=100
def getlineer(weights,xi):
    y = dot(np.transpose(xi),weights)
    #y = np.sign(y)
    return y

for i in range(epoch):
    for k in range(len(xtrain)):
        y = getlineer(weights,xtrain[k])
        e = desired[k]-y;
        weights = weights + lr  * dot(xtrain[k],e)
        print(weights)

但当我训练算法时,模型无法100%学习,错误/权重变得太高:

enter image description here

而且,在第二个纪元之后,我得到了nan值。 我怎样才能获得最佳体重?我的代码是错的还是关于数据集的?在

我知道有一个最佳的重量可以分离100%的准确度。 例如,我为每个权重更新添加了一些代码查找精度:

^{pr2}$

我在19岁的时候。迭代我得到了100%的分隔器重量。在

非常感谢。在


Tags: 数据代码in模型算法forlennp