感知器学习不正确

我尝试进行基础的机器学习。因此,这里是我用于二元分类的感知器类。

class perceptron():    def __init__(self, x, y, threshold=0.5, learning_rate=0.1, max_epochs=10):        self.threshold = threshold        self.learning_rate = learning_rate        self.x = x        self.y = y        self.max_epochs = max_epochs            def initialize(self):        self.weights = np.random.rand(len(self.x[0]))                    def train(self):        epoch = 0        while True:            error_count = 0            epoch += 1            for (x,y) in zip(self.x, self.y):                error_count += self.train_observation(x, y, error_count)            print('Epoch: {0} Error count: {1}'.format(epoch, error_count))            if error_count == 0:                print('Training successful')                break            if epoch >= self.max_epochs:                print('Reached max epochs')                break                    def train_observation(self, x, y, error_count):        result = np.dot(x, self.weights) > self.threshold        error = y - result        if error != 0:            error_count += 1            for index, value in enumerate(x):                self.weights[index] += self.learning_rate * error * value        return error_count            def predict(self, x):        return int(np.dot(x, self.weights) > self.threshold)

我想对列表值的总和进行分类,如果总和大于等于0(表示1),否则(表示0)。因此,我创建了50个长度为10的数组,每个数组包含随机整数值,范围为[-3, 3]:

def sum01(x):    if sum(x) >= 0:        return 1    else:        return 0x = np.random.randint(low=-3, high=3, size=(50,10))y = [sum01(z) for z in a]

然后我初始化并训练模型:

p = perceptron(x, y)p.initialize()p.train()

然后我进行检查,发现很多预测结果不正确,我做错了什么?

predics = [(p.predict(i), sumab(i)) for i in np.random.randint(low=-3, high=3, size=(10, 10))]print(predics)

回答:

在进行了小错误修复后重新运行你的代码,我看到损失减少到0,并且输出了正确的结果 –

p = perceptron(x, y)p.initialize()p.train()
Epoch: 1 Error count: 196608Epoch: 2 Error count: 38654836736Epoch: 3 Error count: 268437504Epoch: 4 Error count: 0Training successful
predics = [(p.predict(i), sum01(i)) for i in np.random.randint(low=-3, high=3, size=(10, 10))]print(predics)
[(1, 1), (0, 0), (0, 0), (0, 0), (1, 1), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)]

解决方案

你的代码需要进行几个快速修改 –

  1. 定义x和y时:
x = np.random.randint(low=-3, high=3, size=(50,10))y = [sum01(z) for z in x] #将这里的a改为x
  1. 获取预测结果时:
#将sumab改为sum01predics = [(p.predict(i), sum01(i)) for i in np.random.randint(low=-3, high=3, size=(10, 10))] 

这样应该就可以工作了。你的完整代码变为 –

class perceptron():    def __init__(self, x, y, threshold=0.5, learning_rate=0.1, max_epochs=10):        self.threshold = threshold        self.learning_rate = learning_rate        self.x = x        self.y = y        self.max_epochs = max_epochs            def initialize(self):        self.weights = np.random.rand(len(self.x[0]))                    def train(self):        epoch = 0        while True:            error_count = 0            epoch += 1            for (x,y) in zip(self.x, self.y):                error_count += self.train_observation(x, y, error_count)            print('Epoch: {0} Error count: {1}'.format(epoch, error_count))            if error_count == 0:                print('Training successful')                break            if epoch >= self.max_epochs:                print('Reached max epochs')                break                    def train_observation(self, x, y, error_count):        result = np.dot(x, self.weights) > self.threshold        error = y - result        if error != 0:            error_count += 1            for index, value in enumerate(x):                self.weights[index] += self.learning_rate * error * value        return error_count            def predict(self, x):        return int(np.dot(x, self.weights) > self.threshold)        def sum01(x):    if sum(x) >= 0:        return 1    else:        return 0    x = np.random.randint(low=-3, high=3, size=(50,10))y = [sum01(z) for z in x]p = perceptron(x, y)p.initialize()p.train()predics = [(p.predict(i), sum01(i)) for i in np.random.randint(low=-3, high=3, size=(10, 10))]print(predics)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注