当前位置:网站首页>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
边栏推荐
- 【SQL server速成之路】——身份驗證及建立和管理用戶賬戶
- What is independent IP and how about independent IP host?
- Codeforces Round #804 (Div. 2) Editorial(A-B)
- Anti shake and throttling are easy to understand
- [force buckle]43 String multiplication
- Game push image / table /cv/nlp, multi-threaded start
- 【华为机试真题详解】检查是否存在满足条件的数字组合
- 移植InfoNES到STM32
- 清除浮动的方式
- Analysis of grammar elements in turtle Library
猜你喜欢

Vulhub vulnerability recurrence 73_ Webmin

PDK工藝庫安裝-CSMC

Safe mode on Windows

应用安全系列之三十七:日志注入

ArcGIS应用基础4 专题图的制作

01. 开发博客项目之项目介绍

【torch】|torch. nn. utils. clip_ grad_ norm_

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

Vulhub vulnerability recurrence 67_ Supervisor

Questions d'examen écrit classiques du pointeur
随机推荐
数字经济破浪而来 ,LTD是权益独立的Web3.0网站?
【云原生】3.1 Kubernetes平台安装KubeSpher
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
[experience] install Visio on win11
【经验】UltralSO制作启动盘时报错:磁盘/映像容量太小
[cloud native] 3.1 kubernetes platform installation kubespher
注释、接续、转义等符号
SQLite add index
毕业设计游戏商城
YYGH-11-定时统计
29io stream, byte output stream continue write line feed
[detailed explanation of Huawei machine test] check whether there is a digital combination that meets the conditions
Vulhub vulnerability recurrence 71_ Unomi
B站刘二大人-Softmx分类器及MNIST实现-Lecture 9
大型网站如何选择比较好的云主机服务商?
Promotion hung up! The leader said it wasn't my poor skills
Algorithm -- climbing stairs (kotlin)
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
How to use PHP string query function
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