概念

Rosenblatt感知器是由Frank Rosenblatt在1957年提出的一种人工神经网络模型,主要用于二元分类任务。它是早期神经网络和机器学习的重要基础之一。

主要特点

  1. 结构简单
    • 输入层:接收特征向量。
    • 输出层:一个神经元,输出二元结果(如+1或-1)。
  2. 学习规则
    • 采用感知器学习算法,通过调整权重来最小化分类误差。
    • 权重更新公式:wi=wi+η(y−y^)xiwi​=wi​+η(y−y^​)xi​
      • wi​:权重
      • η:学习率
      • y:真实标签
      • y^​:预测输出
      • xi​:输入特征
  3. 激活函数
    • 使用阶跃函数,输出为+1或-1。

优点

  • 简单易实现,适合线性可分问题。

局限性

  • 只能处理线性可分数据,无法解决非线性问题(如异或问题)。

历史意义

  • 为后续神经网络(如多层感知器)的发展奠定了基础。

总结

Rosenblatt感知器是早期的二元分类模型,尽管功能有限,但在神经网络和机器学习的发展中具有重要地位。

尝试

线性散点图实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import numpy as np  
import matplotlib.pyplot as plt

def dots(counts):
xs = np.random.rand(counts)
xs = np.sort(xs)
ys = [1.2*x+np.random.rand()/10 for x in xs]
return xs, ys

xs, ys = dots(100)
print(xs)
print(ys)

plt.title("STF", fontsize=12)
plt.xlabel("B")
plt.ylabel("T")
plt.scatter(xs, ys)
plt.show()

利用权重更新公式wi=wi+η(y−y^)xiwi​=wi​+η(y−y^​)xi​,使用for循环不断更新权重,其中alpha值即学习率,设置小是为了防止幅度太大错过最佳拟合线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import numpy as np  
import matplotlib.pyplot as plt

def dots(counts):
xs = np.random.rand(counts)
xs = np.sort(xs)
ys = [1.2*x+np.random.rand()/10 for x in xs]
return xs, ys

xs, ys = dots(100)
print(xs)
print(ys)

plt.title("STF", fontsize=12)
plt.xlabel("B")
plt.ylabel("T")
plt.scatter(xs, ys)

w=0.5

for qz in range(100):
for i in range(100):
x=xs[i]
y=ys[i]
y_pre= w*x
e= y-y_pre
alpha =0.05
w=w+alpha*e*x
y_pre=w*xs
print(y_pre)
plt.plot(xs, y_pre)
plt.show()

截屏2025-03-12 01.11.56.png

可以得到一个拟合线

方差代价函数

概念

方差代价函数是一种用于衡量模型预测值与真实值之间差异的代价函数,它通常被用于机器学习和统计学中的回归问题。以下是对方差代价函数的详细解释:

截屏2025-03-12 01.12.22.png

作用

衡量拟合程度:方差代价函数的值越小,说明模型的预测值与真实值越接近,模型对数据的拟合程度越好。通过最小化方差代价函数,可以找到一组最优的模型参数,使得模型能够最好地拟合训练数据

优化模型参数:在训练过程中,方差代价函数是优化的目标。通过不断地调整模型的参数,使得方差代价函数的值逐渐减小,最终达到一个较小的值,从而得到一个性能较好的模型

尝试

方差代价函数是一个二次函数,首先可以尝试输出该函数的图形,因为方差代价函数的值越小,说明模型的预测值与真实值越接近,故而需要去使用最小二乘法去寻找到方差代价函数最小值,然后带回求得最佳拟合回归线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import matplotlib.pyplot as plt  
import numpy as np

def dots(counts):
xs = np.random.rand(counts)
xs = np.sort(xs)
ys = [1.2*x+np.random.rand()/10 for x in xs]
return xs, ys

xs,ys=dots(100)

plt.title("STF",fontsize=20)
plt.xlabel("B")
plt.ylabel("T")
plt.scatter(xs,ys)

w=0.1 #任意取
y_pre=w*xs
plt.plot(xs,y_pre)
plt.show() # 默认曲线,用于对照寻找到最小权重时的

e_sqaure=(ys-y_pre)
sum_e=np.sum(e_sqaure)
sum_e=(1/100)*sum_e
print(sum_e)


ws=np.arange(0,3,0.1) # 0到3,每次增加0.1
es=[]
for w in ws:
y_pre=w*xs
e=(1/100)*np.sum(ys-y_pre)**2
es.append(e)
# 遍历权重并重新计算代价函数



# 设定代价函数的坐标系,可以看到误差和权重的关系呈现二次函数
plt.title("Cost",fontsize=20)
plt.xlabel("W")
plt.ylabel("E")
plt.plot(ws,es)
plt.show()


w_min=np.sum(xs*ys)/np.sum(xs*xs) # 最小二乘法寻找最佳权重
print("Best w is" + str(w_min))

y_pre=w_min*xs
plt.title("Best_Line",fontsize=20)
plt.xlabel("B")
plt.ylabel("T")
plt.scatter(xs,ys)
plt.plot(xs,y_pre)
plt.show()

代价函数的图像

截屏2025-03-12 01.12.44.png

权重更新前后

截屏2025-03-12 01.12.57.png

截屏2025-03-12 01.13.12.png