当前位置:网站首页>《PyTorch深度学习实践》-B站 刘二大人-day1
《PyTorch深度学习实践》-B站 刘二大人-day1
2022-07-22 06:04:00 【爱编程的西瓜】
第2讲 linear_model 源代码
B站 刘二大人 ,传送门 PyTorch深度学习实践——线性模型
代码说明:
1、函数forward()中,有一个变量w。这个变量最终的值是从for循环中传入的。
2、for循环中,使用了np.arange。
3、python中zip()函数的用法:
zip函数的原型为:
zip([iterable, …])
参数iterable为可迭代的对象,并且可以有多个参数。该函数返回一个以元组为元素的列表,其中第 i 个元组包含每个参数序列的第 i 个元素。返回的列表长度被截断为最短的参数序列的长度。只有一个序列参数时,它返回一个1元组的列表。没有参数时,它返回一个空的列表。
今天学习的具体代码:
import numpy as np
import matplotlib.pyplot as plt
x_data = [1.0, 2.0, 3.0] # 这是x的值
y_data = [2.0, 4.0, 6.0]# 这是y的真实值
def forward(x):
return x*w
def loss(x, y): #计算损失值
y_pred = forward(x)
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
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('\t', x_val, y_val, y_pred_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()
布置的作业如下:
实现代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
# y = x*2.5-1 构造训练数据
x_data = [1.0, 2.0, 3.0]
y_data = [1.5, 4.0, 6.5]
W, B = np.arange(0.0, 4.1, 0.1), np.arange(-2.0, 2.1, 0.1) # 规定 W,B 的区间
w, b = np.meshgrid(W, B, indexing='ij') # 构建矩阵坐标
def forward(x):
return x*w+b
def loss(y_pred, y):
return (y_pred-y)*(y_pred-y)
# Make data.
mse_lst = []
l_sum = 0.
for x_val, y_val in zip(x_data, y_data):
y_pred_val = forward(x_val)
loss_val = loss(y_pred_val, y_val)
l_sum += loss_val
mse_lst.append(l_sum/3)
# 定义figure
fig = plt.figure(figsize=(10,10), dpi=300)
# 将figure变为3d
ax = Axes3D(fig)
# 绘图,rstride:行之间的跨度 cstride:列之间的跨度
surf = ax.plot_surface(w, b, np.array(mse_lst[0]), rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
# Customize the z axis.
ax.set_zlim(0, 40)
# 设置坐标轴标签
ax.set_xlabel("w")
ax.set_ylabel("b")
ax.set_zlabel("loss")
ax.text(0.2, 2, 43, "Cost Value", color='black')
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
运行结果(不能说像,只能说一模一样)
边栏推荐
- 稀疏数组(sparse)
- Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
- Pytoch deep learning practice lesson 11 (CNN)
- The most detailed conversion of Base64, blob and file
- 一文带你了解redux的工作流程——action/reducer/store
- @The difference between resource and @autowired
- 从0到1建设智能灰度数据体系:以vivo游戏中心为例
- GMT 0009-2012数据格式-GO语言操作
- Angr principle and Practice (I) -- principle
- UE4 set night (update skysphere according to directionallight direction)
猜你喜欢
随机推荐
mysql约束之_非空约束
14 multithread 1
Realization of a springboard machine
1312. 让字符串成为回文串的最少插入次数
This article introduces you to the workflow of Redux - action/reducer/store
NFTFi赛道版图概览
UE4 merge static mesh body
利用二分法查找数组的元素
【机器学习】pytorch如何加载自定义数据集并进行数据集划分
UE4 use of vegetation tools
浏览器页面的渲染流程
线程和进程
NVIDIA hardware architecture
Reading papers [6] autoassembly: learning augmentation strategies from data
UE4 level blueprint realizes door opening and closing
Getting started with VIM
Nvidia 硬件架构
[machine learning] how pytorch loads custom datasets and divides them
写一个定序器插件 sequence 字幕(一)
论文阅读【6】Autoaugment: Learning augmentation strategies from data