当前位置:网站首页>Pytorch deep learning practice-b station Liu erden-day1
Pytorch deep learning practice-b station Liu erden-day1
2022-07-22 17:25:00 【Watermelon that loves programming】
The first 2 speak linear_model Source code
B standing Lord Liu , Portal PyTorch Deep learning practice —— Linear model
Code instructions :
1、 function forward() in , There is a variable w. The final value of this variable is from for Passed in the loop .
2、for In circulation , Used np.arange.
3、python in zip() Function usage :
zip The prototype of the function is :
zip([iterable, …])
Parameters iterable For objects that can be iterated , And can have multiple parameters . This function returns a list with tuples as elements , Among them the first i Tuples contain the... Of each parameter sequence i Elements . The length of the returned list is truncated to the length of the shortest parameter sequence . When there is only one sequence parameter , It returns a 1 A list of tuples . Without parameters , It returns an empty list .
Specific code learned today :
import numpy as np
import matplotlib.pyplot as plt
x_data = [1.0, 2.0, 3.0] # This is a x Value
y_data = [2.0, 4.0, 6.0]# This is a y True value of
def forward(x):
return x*w
def loss(x, y): # Calculate the loss value
y_pred = forward(x)
return (y_pred - y)**2
# Exhaustive method
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 out the values one by one
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()
The assignments are as follows :
Implementation code :
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
# y = x*2.5-1 Construct training data
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) # Regulations W,B The range of
w, b = np.meshgrid(W, B, indexing='ij') # Construction matrix coordinates
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)
# Definition figure
fig = plt.figure(figsize=(10,10), dpi=300)
# take figure Turn into 3d
ax = Axes3D(fig)
# mapping ,rstride: The span between rows cstride: The span between columns
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)
# Set axis labels
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()
Running results ( Can't say like , It's as like as two peas. )
边栏推荐
- Abaqus实现二自由度振动系统模态计算
- Recommendation of selected topics for completed topics of Electronic Information Engineering
- 牛客网 Fibonacci数列
- How to reinstall win11 system online with one click?
- GMT 0009-2012 data format go language operation
- 论文阅读【6】Autoaugment: Learning augmentation strategies from data
- "New capabilities" of rongyun Super Group
- UE4 lifts the elevator by pressing the key
- Playbook 介绍
- 浅谈不可转让的声誉积分NFT SBTs面临的困境
猜你喜欢
UE4 lifts the elevator by pressing the key
浏览器页面的渲染流程
matlab混频器的实现
MVC mode and three-tier architecture
Write a sequencer plug-in sequence subtitle (1)
了解 5 种流行的 NFT 投放方式及其优缺点
会议OA项目
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
UE4 writes the blueprint in the actor class to realize reuse
MATLAB函数:filtfilt——零相位数字滤波
随机推荐
"New capabilities" of rongyun Super Group
【LeetCode】814. 二叉树剪枝
MATLAB函数:filtfilt——零相位数字滤波
《PyTorch深度学习实践》-B站 刘二大人-day2
会议OA项目
How to implement Apache's built-in AB stress testing tool
【矩阵乘法】外部矩阵乘法
What if only the mouse displays when win11 is turned on?
js 判断链接图片是否存在
Spark advanced features, 220720,
注意力机制的分类
How much do you know about the basic features of SolidWorks| Four methods of extruding features
vivo官网APP全机型UI适配方案
es6相关面试题2
Xshell Plus6下载及安装使用的方法
【外部排序】快排思想完成外部排序
LVS load balancing cluster
马斯克“变脸”﹕Q2出售75%BTC 套现近10亿美元
Recommendation of selected topics for completed topics of Electronic Information Engineering
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example