当前位置:网站首页>Reflection on pytorch back propagation
Reflection on pytorch back propagation
2022-07-27 07:06:00 【Mr_ health】
Realize a simple all to condense network , The work done is :

Then the loss function is classical L2 Loss :

The code is as follows , Here we set paranoia to 0:
class network(nn.Module):
def __init__(self):
super().__init__()
self.w = torch.Tensor([1.0])
self.w.requires_grad = True
def forward(self, x):
output = self.w * x
return output
def gradient(x, y, w):
return 2*x*(x*w - y)
if __name__ == '__main__':
model = network()
x_data = torch.Tensor([1.0,2.0,3.0])
y_data = torch.Tensor([2.0,4.0,6.0])
output = model(x_data)
loss = (output-y_data).pow(2).mean()
loss.backward()
grads = gradient(x_data, y_data, model.w).mean()# Manual implementation Back propagation
print(model.w.grad.data) #
print(grads)The results are consistent , All for -8.3333
Specific derivation :

Let's revise it loss In the form of , The weight of each sample is different , Increase the weight of the last sample , You can see that the weight of the two samples is 1/4, The weight of the last sample is 1/2. From this point of view , In fact, the above is in accordance with the standard loss Back propagation , In fact, each sample has equal weight .
model = network()
x_data = torch.Tensor([1.0,2.0,3.0])
y_data = torch.Tensor([2.0,4.0,6.0])
output = model(x_data)
loss = (output-y_data).pow(2)
loss = (loss[0:2].mean() + loss[2])/2 # modify loss
loss.backward()
# grads = gradient(x_data, y_data, model.w).mean()# Manual implementation Back propagation
print(model.w.grad.data) #
The result is -11.5, Now calculate manually :

边栏推荐
- Livox SLAM(带LIO+闭环检测优化)
- Day012 application of one-dimensional array
- Campus news release management system based on SSM
- DNA modified near infrared two region GaAs quantum dots | GaAs DNA QDs | DNA modified GaAs quantum dots
- 强网杯2021 pwn 赛题解析——babypwn
- Sok: the faults in our asrs: an overview of attacks against automatic speech recognition
- The issuing process of individual developers applying for code signing certificates
- How to delete or replace the loading style of easyplayer streaming media player?
- Code random notes_ Hash_ 242 effective letter heterotopic words
- Keras OCR instance test
猜你喜欢

Norms of vectors and matrices

硫化镉CdS量子点修饰脱氧核糖核酸DNA|CdS-DNA QDs|近红外CdS量子点偶联DNA规格信息

PNA modified polypeptide arms PNA PNA DNA suc aapf PNA suc - (ALA) 3 PNA

基于SSM图书借阅管理系统

Dsgan degenerate network

网易云信亮相 GIAC 全球互联网架构大会,解密新一代音视频架构在元宇宙场景的实践...

The vscode run command reported an error: the mark "&" is not a valid statement separator in this version.

Campus news release management system based on SSM

银行业客户体验管理现状与优化策略分析

About the new features of ES6
随机推荐
DNA (deoxyribonucleic acid) supply | carbon nanotube nucleic acid loaded dna/rna material | dna/rna nucleic acid modified magnetic nanoparticles
Peptide nucleic acid oligomer containing azobenzene monomer (nh2-tnt4, n-pnas) Qiyue biological customization
deepsort源码解读(五)
deepsort源码解读(二)
【Latex格式】双栏双图左右并排有小标题、上下并列有小标题
DNA修饰贵金属纳米颗粒|脱氧核糖核酸DNA修饰纳米金(科研级)
Variance and covariance
Norms of vectors and matrices
手机上也能训练BERT和ResNet了?!
ES6 new features (getting started)
Fix the problem that the paging data is not displayed when searching the easycvr device management list page
VScode连接远程服务器开发
PSI | CSI and ROC | AUC and KS - memorandum
R2LIVE代码学习记录(3):对雷达特征提取
Express framework
vscode运行命令报错:标记“&&”不是此版本中的有效语句分隔符。
[latex format] there are subtitles side by side on the left and right of double columns and double pictures, and subtitles are side by side up and down
regular expression
Numpy array and image conversion
MangoDB