当前位置:网站首页>Lecture 2 Linear Model Linear Model
Lecture 2 Linear Model Linear Model
2022-08-05 05:23:00 【A long way to go】
参考资料
- 一句话解释numpy.meshgrid()
- matplotlib教程之——Custom profiles and drawing styles(rcParams和style)
- python中zip()函数的用法
- matplotlib之plot()详解
- matplotlib 3D绘图警告
课堂练习
实现线性模型y=wx的平面图
import numpy as np
import matplotlib.pyplot as plt
#保存数据集,The same index is a sample
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
#Feedforward of the model
def forward(x):
return x * w
#损失函数
def loss(x, y):
y_pred = forward(x) #According to the feedforward requirementy_hat
return (y_pred - y) ** 2 #计算损失
# 穷举法
w_list = [] #权重
mse_list = [] #The loss value corresponding to the weight
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()
pattern trace
课后练习
实现线性模型(y=wx+b)并输出loss的3D图像
There are several issues that need to be addressed here
1.w,b的取值
in previous class practice,只需要取一个w,因此可以用for循环取值.Correction is required in the exercises after classw,bTwo values for value operation,因此要使用meshgrid函数
2.Images cannot be displayed in Chinese
Add in front
from pylab import * mpl.rcParams[‘font.sans-serif’] = [‘SimHei’]
3.matplotlib 3D绘图警告
matplotlib 3D绘图警告
Code for homework exercises:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
#Here the function is set to 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图:
边栏推荐
- How to quickly upgrade your Taobao account to a higher level
- 【过一下6】机器视觉视频 【过一下2被挤掉了】
- span标签和p标签的区别
- 1068找到更多的硬币
- WPF中DataContext作用
- 【过一下14】自习室的一天
- [Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management
- Flex layout frog game clearance strategy
- 有用番茄来监督自己的同道中人吗?加一下我的自习室,一起加油
- Flutter learning 2-dart learning
猜你喜欢

Flutter learning 5-integration-packaging-publish

多线程查询结果,添加List集合

第四讲 back propagation 反向传播

span标签和p标签的区别

Basic properties of binary tree + oj problem analysis

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

Flutter real machine running and simulator running

【过一下7】全连接神经网络视频第一节的笔记

Error creating bean with name 'configDataContextRefresher' defined in class path resource
![[Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management](/img/0b/f7d9205c616f7785519cf94853d37d.png)
[Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management
随机推荐
uva1325
[Software Exam System Architect] Software Architecture Design ③ Domain-Specific Software Architecture (DSSA)
【过一下6】机器视觉视频 【过一下2被挤掉了】
Flutter real machine running and simulator running
uboot开启调试打印信息
Qt produces 18 frames of Cupid to express his love, is it your Cupid!!!
human weakness
"PHP8 Beginner's Guide" A brief introduction to PHP
Requests the library deployment and common function
DOM及其应用
Algorithms - ones and zeros (Kotlin)
Opencv中,imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) 报错:error:!_src.empty() in function ‘cv::cvtColor‘
[Go through 3] Convolution & Image Noise & Edge & Texture
【过一下7】全连接神经网络视频第一节的笔记
Geek卸载工具
数据库 单表查询
Flex layout frog game clearance strategy
OFDM 十六讲 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
2022牛客多校第四场C.Easy Counting Problem(EGF+NTT)
【过一下11】随机森林和特征工程