python-3.x - 如何从一个简单的感知器绘制斜率
问题描述
我想在权重变化时绘制斜率 y = mx+b 但我不知道如何获得 m 和 b 的值
这是代码:
import numpy as np
import matplotlib.pyplot as plt
def sign(x):
return 1 if x > 0 else 0
def get_mse(predicted_data, targets):
error = predicted_data - targets
return np.square(error).sum()/len(targets)
fit_data = np.array([[1,0,0],
[1,0,1],
[1,1,0],
[1,1,1],])
targets = np.array([0,0,0,1])
weights = np.array([0,0,0])
lr = 1
epochs = 10
mse_hist = []
for _ in range(epochs):
model_outputs = []
for i in range(len(targets)):
y = (fit_data[i] * weights).sum()
y = sign(y)
error = targets[i] - y
weights = weights + fit_data[i] * error * lr
model_outputs.append(y)
mse = get_mse(model_outputs, targets)
mse_hist.append(mse)
这里应该是斜坡的情节,至少我是这么认为的
print("Weights:", weights)
print("Mse:", mse_hist[-1])
plt.plot(mse_hist)
plt.xlabel('Iterations')
plt.ylabel('Mean Squared Error')
plt.pause(0.5)
plt.show()
input_data = ([1,1,1])
prediction = (input_data * weights).sum()
prediction = sign(prediction)
print("Prediction:", prediction)
谢谢您的帮助
解决方案
推荐阅读
- javascript - PayPal JavaScript SDK:获取基本卡信息 onApprove()
- sorting - micronaut 项目中的 DB 查询结果类型不正确
- javascript - 如何在nestjs中保存多对多关系
- nginx - Nginx proxy_pass 无需重写 url
- python - 数据框通过分隔符将列分隔为多列
- python - pygame中的弹跳功能
- ckeditor - 如果同一个对话框注册了两次,则上下文菜单不正确
- jquery-select2 - 使用 select2 的 html 标签选项
- r - 如何更改为 Times New Roman 字体?
- javascript - 用反应实时填充搜索