当前位置:网站首页>第二讲 Linear Model 线性模型
第二讲 Linear Model 线性模型
2022-08-05 05:13:00 【长路漫漫 大佬为伴】
参考资料
- 一句话解释numpy.meshgrid()
- matplotlib教程之——自定义配置文件和绘图风格(rcParams和style)
- python中zip()函数的用法
- matplotlib之plot()详解
- matplotlib 3D绘图警告
课堂练习
实现线性模型y=wx的平面图
import numpy as np
import matplotlib.pyplot as plt
#保存数据集,相同的索引为一个样本
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
#模型的前馈
def forward(x):
return x * w
#损失函数
def loss(x, y):
y_pred = forward(x) #根据前馈求y_hat
return (y_pred - y) ** 2 #计算损失
# 穷举法
w_list = [] #权重
mse_list = [] #权重对应的损失值
for w in np.arange(0.0, 4.1, 0.1):
print("w=", w)
l_sum = 0
#从x_data, y_data取出x_val, y_val
for x_val, y_val in zip(x_data, y_data):
y_pred_val = forward(x_val)
loss_val = loss(x_val, y_val)
l_sum += loss_val
print('x_val==', x_val, 'y_val==',y_val, 'y_pred_val==',y_pred_val,'loss_val==', loss_val)
print('MSE=', l_sum / 3)
w_list.append(w)
mse_list.append(l_sum / 3)
#调用画图
plt.plot(w_list, mse_list)
plt.ylabel('Loss')
plt.xlabel('w')
plt.show()
图案轨迹
课后练习
实现线性模型(y=wx+b)并输出loss的3D图像
这里存在几个问题需要解决
1.w,b的取值
之前课堂练习中,只需要取一个w,因此可以用for循环取值。课后练习中需要对w,b两个值进行取值操作,因此要使用meshgrid函数
2.图像无法显示中文
在前方加上
from pylab import * mpl.rcParams[‘font.sans-serif’] = [‘SimHei’]
3.matplotlib 3D绘图警告
matplotlib 3D绘图警告
课后习题代码:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
#这里设函数为y=3x+2
x_data = [1.0,2.0,3.0]
y_data = [5.0,8.0,11.0]
def forward(x):
return x * w + b
def loss(x,y):
y_pred = forward(x)
return (y_pred-y)*(y_pred-y)
mse_list = []
W=np.arange(0.0,4.1,0.1)
B=np.arange(0.0,4.1,0.1)
w,b=np.meshgrid(W,B)
# print("w==",w)
# print('b==',b)
l_sum = 0
for x_val, y_val in zip(x_data, y_data):
y_pred_val = forward(x_val)
loss_val = loss(x_val, y_val)
print('x_val==', x_val,'\ny_val==', y_val,'\ny_pred_val==', y_pred_val, '\nloss_val==',loss_val)
l_sum += loss_val
fig = plt.figure()
ax = Axes3D(fig,auto_add_to_figure=False)
fig.add_axes(ax)
ax.plot_surface(w, b, l_sum/3)
ax.set_xlabel("权重 W")
ax.set_ylabel("偏置项 B")
ax.set_zlabel("损失值")
plt.show()
3D图:
边栏推荐
- Cryptography Series: PEM and PKCS7, PKCS8, PKCS12
- 结构光三维重建(二)线结构光三维重建
- "Recursion" recursion concept and typical examples
- C#关于set()和get()方法的理解及使用
- 【解码工具】Bitcoin的一些在线工具
- 基于Web的商城后台管理系统的设计与实现
- Flutter 父子组件如何都能收到点击事件
- 【过一下14】自习室的一天
- Detailed Explanation of Redis Sentinel Mode Configuration File
- 【学习笔记之菜Dog学C】动态内存管理之经典笔试题
猜你喜欢

jvm three heap and stack

The role of DataContext in WPF

Using QR codes to solve fixed asset management challenges

Flutter learning 5-integration-packaging-publish

Basic properties of binary tree + oj problem analysis

【过一下8】全连接神经网络 视频 笔记

ESP32 485光照度
![coppercam primer [6]](/img/d3/a7d44aa19acfb18c5a8cacdc8176e9.png)
coppercam primer [6]

Flutter learning three-Flutter basic structure and principle

MySQL基础(一)---基础认知及操作
随机推荐
[Software Exam System Architect] Software Architecture Design ③ Domain-Specific Software Architecture (DSSA)
Redis - 13、开发规范
How to quickly upgrade your Taobao account to a higher level
ESP32 485光照度
Basic properties of binary tree + oj problem analysis
Detailed Explanation of Redis Sentinel Mode Configuration File
Distributed systems revisited: there will never be a perfect consistency scheme...
u-boot debugging and positioning means
Analysis of Mvi Architecture
UVA10827
数据库 单表查询
1.3 mysql batch insert data
Transformation 和 Action 常用算子
基于Web的商城后台管理系统的设计与实现
HQL语句执行过程
RL强化学习总结(一)
C#关于set()和get()方法的理解及使用
redis 缓存清除策略
WPF中DataContext作用
【记一下1】2022年6月29日 哥和弟 双重痛苦