McCulloch-Pitts模型 及Rosenblatt感知模型代码实现
文章介绍
- 文章配图来自网络
- 个人学习笔记,推荐直接买课去学习
- 得懂python,本文代码可直接跑,环境基于[ PyCharm 2022.2 ]
1.McCulloch-Pitts模型
Y = X1W1 + X2W2 + X3W3 + … + XnWn ;
2.Rosenblatt感知模型
3.编写一个Rosenblatt感知模型
- 问题抽象
一个深海生物叫小蓝,它需要吃一种豆豆,但是豆豆有毒,豆豆的毒性和大小正相关,即:毒性=w*大小 (豆豆越大毒性越大) ,我们需要去找w。
- 代码实现
import matplotlib
import numpy as np
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
# 生产测试数据
def get_beans(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 = get_beans(100)
# print(xs) # 豆豆大小
# print(ys) # 豆豆毒性
# 配置图像
plt.title("toxicity-size", fontsize=12)
plt.xlabel("size")
plt.ylabel("toxicity")
plt.scatter(xs, ys)
# 去找w
w = 0.5
for m in range(100):
for i in range(100):
x = xs[i]
y = ys[i]
y_pre = w * x # numpy的广播机制
e = y - y_pre
alpha = 0.05
w = w + alpha * e * x
# 最后一次输出
y_pre = w * xs # numpy的广播机制
plt.plot(xs, y_pre)
plt.show()
- 代码效果