当前位置:网站首页>Station B, Master Liu Er - back propagation
Station B, Master Liu Er - back propagation
2022-07-06 05:41:00 【Ning Ranye】
Series articles :
B Stand up, Mr. Liu er - Linear regression and gradient descent
List of articles
Code
import matplotlib.pyplot as plt
import torch
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
# w Is a tensor with gradient tracking set
w = torch.Tensor([1.0])
w.requires_grad = True
print("w=",w)
def forward(x):
return x*w
# Loss function of single data
def loss(x, y):
y_pred = forward(x)
return (y_pred - y)*(y_pred-y)
# Learning rate
alpha = 0.001
epoch_list = []
w_list = []
loss_list = []
for epoch in range(100):
l = 0
loss_sum = 0
for (x ,y) in zip(x_data, y_data):
# l, loss_sum It's all tensors , Gradient free tracking
l = loss(x,y)
loss_sum += l.data
# My question : Back propagation 、 Gradient update is carried out epoch*len(x_data),
# Why not epoch Time .
l.backward()
w.data = w.data - alpha*w.grad.data
w.grad.data.zero_()
w_list.append(w.data)
epoch_list.append(epoch)
# To get the value on the tensor, you need to convert it to numpy
loss_list.append(loss_sum.data.numpy()[0])
plt.plot(epoch_list, loss_list)
plt.xlabel("epoch")
plt.ylabel("loss_sum")
plt.show()
Back propagation 、 Gradient update lepoch Code for the next time
import matplotlib.pyplot as plt
import torch
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
# w Is a tensor with gradient tracking set
w = torch.Tensor([1.0])
w.requires_grad = True
print("w=",w)
def forward(x):
return x*w
# Loss function of single data
def loss(xs, ys):
# y_pred = forward(x)
# return (y_pred - y)*(y_pred-y)
loss_sum = 0
for(x, y) in zip(xs, ys):
y_pred = forward(x)
loss_sum += (y_pred-y)*(y_pred-y)
return loss_sum/len(xs)
# Learning rate
alpha = 0.001
epoch_list = []
w_list = []
loss_list = []
# Conduct epoch Sub gradient update 、 Loss function calculation
for epoch in range(100):
# Calculate the loss function of all data
l = loss(x_data, y_data)
l.backward()
# Gradient update
w.data = w.data - alpha * w.grad.data
w.grad.data.zero_()
w_list.append(w.data)
epoch_list.append(epoch)
loss_list.append(l.data.numpy()[0])
plt.plot(epoch_list, loss_list)
plt.xlabel("epoch")
plt.ylabel("loss_sum")
plt.show()
The pictures that come out in two ways are Loss reduction rate 、 The results are different .
At present, I also have questions , I don't know which kind is suitable .
B The station teacher wrote the first code
边栏推荐
- [string] palindrome string of codeup
- 27io stream, byte output stream, OutputStream writes data to file
- Solution of QT TCP packet sticking
- PDK工艺库安装-CSMC
- [SQL Server fast track] - authentication and establishment and management of user accounts
- [QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
- [QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
- Vulhub vulnerability recurrence 72_ uWSGI
- [Tang Laoshi] C -- encapsulation: classes and objects
- How to download GB files from Google cloud hard disk
猜你喜欢
注释、接续、转义等符号
Pointer classic written test questions
[Jiudu OJ 08] simple search x
02. Develop data storage of blog project
03. Login of development blog project
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
应用安全系列之三十七:日志注入
Application Security Series 37: log injection
B站刘二大人-线性回归 Pytorch
Vulhub vulnerability recurrence 71_ Unomi
随机推荐
UCF (2022 summer team competition I)
Garbage collector with serial, throughput priority and response time priority
[Jiudu OJ 08] simple search x
[experience] install Visio on win11
Algorithm -- climbing stairs (kotlin)
【torch】|torch. nn. utils. clip_ grad_ norm_
Self built DNS server, the client opens the web page slowly, the solution
03. 开发博客项目之登录
SequoiaDB湖仓一体分布式数据库2022.6月刊
指針經典筆試題
Unity gets the width and height of Sprite
AUTOSAR从入门到精通番外篇(十)-嵌入式S19文件解析
SQLite queries the maximum value and returns the whole row of data
Improve jpopup to realize dynamic control disable
Vulhub vulnerability recurrence 67_ Supervisor
剑指 Offer II 039. 直方图最大矩形面积
Questions d'examen écrit classiques du pointeur
【经验】win11上安装visio
Qt TCP 分包粘包的解决方法
进程和线程