当前位置:网站首页>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
边栏推荐
- Game push image / table /cv/nlp, multi-threaded start
- 初识数据库
- Jvxetable用slot植入j-popup
- Unity Vector3. Use and calculation principle of reflect
- Self built DNS server, the client opens the web page slowly, the solution
- Selective parameters in MATLAB functions
- 应用安全系列之三十七:日志注入
- HAC cluster modifying administrator user password
- Unity gets the width and height of Sprite
- How can large websites choose better virtual machine service providers?
猜你喜欢
How can large websites choose better virtual machine service providers?
类和对象(一)this指针详解
Promise summary
剑指 Offer II 039. 直方图最大矩形面积
Vulhub vulnerability recurrence 67_ Supervisor
初识数据库
Zoom and pan image in Photoshop 2022
A master in the field of software architecture -- Reading Notes of the beauty of Architecture
js Array 列表 实战使用总结
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
随机推荐
[email protected]树莓派
Unity gets the width and height of Sprite
Web Security (VI) the use of session and the difference between session and cookie
Note the various data set acquisition methods of jvxetable
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
Fluent implements a loadingbutton with loading animation
How to use PHP string query function
【经验】win11上安装visio
Vulhub vulnerability recurrence 69_ Tiki Wiki
The digital economy has broken through the waves. Is Ltd a Web3.0 website with independent rights and interests?
01. 开发博客项目之项目介绍
Game push: image / table /cv/nlp, multi-threaded start!
[string] palindrome string of codeup
A master in the field of software architecture -- Reading Notes of the beauty of Architecture
Analysis of grammar elements in turtle Library
Sword finger offer II 039 Maximum rectangular area of histogram
03. 开发博客项目之登录
Pytorch代码注意的细节,容易敲错的地方
UCF(暑期团队赛二)
什么是独立IP,独立IP主机怎么样?