Python code implementation of the original form of the perceptron

The original form of the perceptron:

The input data is positive example point x1=(3,3)T, x2=(4,3)T, negative example point x3=(1,1)T, use the original form of the perceptron learning algorithm to find the perceptron model f( x)=sign(wx+b), where w=(w1,w2)T, x=(x1,x2)T

The code is implemented as follows:

# 统计学习方法(李航)第二版——第2章 感知机 课本例题2.1 实现代码
# data:2022/10/14

import numpy as np
# 定义数据集 x_data 按列输入 y_data中按行做输出
x_data = np.array([[3, 4, 1],
                   [3, 3, 1]])
y_data = np.array([[1], [1], [-1]])

# 初始化超平面
# 一定要注意w和b的维度问题
w = np.array([[0], [0]])  # 维度是2,因为一个x数据为维的
b = 0  # 维度为1,因为输出y是一维的
lr = 1 # 步长

# 梯度下降法极小化目标函数
total_spisode = 50
episode = 0
while episode < total_spisode:
    loss = 0
    episode += 1
    for i in range(3):
        x = x_data[:, i] # 这样之后x是(2,)
        x = np.expand_dims(x, axis=1)  # x变为(1,2)
        y = y_data[i, :]
        if y * (np.sum(x * w) + b) > 0:
            continue
        else:
            loss += - y * (np.sum(x * w) + b)
            # 这里也得注意向量乘积的维度问题
            w = w + lr * x * y
            b = b + lr * y
        print("x{}:  w: {}, b: {}".format(i, w, b))
    print("Episode: {}, loss: {}".format(episode, loss))
    if loss == 0:
        break
print("End: w = {}, b={}".format(w, b))

Guess you like

Origin blog.csdn.net/Er_Studying_Bai/article/details/127364649