神经网络实战2-损失函数和反向传播


其实就是通过求偏导的方式,求出各个权重大小

loss函数是找最小值的,要求导,在计算机里面计算导数是倒着来的,所以叫反向传播。

c 复制代码
import  torch
from torch.nn import L1Loss

inputs=torch.tensor([1,2,3],dtype=torch.float32)
target=torch.tensor([1,2,5],dtype=torch.float32)

inputs=torch.reshape(inputs,(1,1,1,3))#这里rershape的目的是增加batch_size这一数据
target=torch.reshape(target,(1,1,1,3))
loss=L1Loss()
result=loss(inputs,target)
print(result)

对以上的一个简单设计

loss的默认reduction是mean即平均值

我们需要的是相加

c 复制代码
import  torch
from torch.nn import L1Loss

inputs=torch.tensor([1,2,3],dtype=torch.float32)
target=torch.tensor([1,2,5],dtype=torch.float32)

inputs=torch.reshape(inputs,(1,1,1,3))#这里rershape的目的是增加batch_size这一数据
target=torch.reshape(target,(1,1,1,3))
loss=L1Loss(reduction='sum')
result=loss(inputs,target)
print(result)

均方差

反向传播

相关推荐
knight_9___23 分钟前
大模型project面试4
人工智能·python·深度学习·算法·面试·agent
code_pgf1 小时前
知识蒸馏在 sVLM 中的作用及实现方式
人工智能·深度学习·机器学习
keineahnung23452 小时前
為什麼要有 eval_is_non_overlapping_and_dense?PyTorch 包裝層與調用端解析
人工智能·pytorch·python·深度学习
Hali_Botebie2 小时前
【量化】FQ-ViT: Post-Training Quantization for Fully Quantized Vision Transformer
人工智能·深度学习·transformer
m0_372257022 小时前
parse_model 函数的收尾部分,负责将计算好的参数实例化为真实的 PyTorch 层,并完成元数据的绑定和通道账本的更新
人工智能·pytorch·python
狮子座明仔2 小时前
AggAgent:把并行轨迹当环境来交互,智能体聚合的新范式
人工智能·深度学习·机器学习·交互
pzx_0012 小时前
【论文阅读】SWE-CI: Evaluating Agent Capabilities in Maintaining Codebases via Continuous Integration
论文阅读·人工智能·深度学习·神经网络·ci/cd
摇落露为霜2 小时前
论文笔记DiT:Scalable Diffusion Models with Transformers(含transformer的可扩展扩散模型 )
人工智能·深度学习·transformer·扩散模型·dit
网安INF2 小时前
【论文阅读】-《QUERY EFFICIENT DECISION BASED SPARSE ATTACKS AGAINST BLACK-BOX DEEP LEARNING MODELS》
论文阅读·人工智能·深度学习·计算机视觉
那个,我想吃麦麦2 小时前
【论文阅读 | Advanced Engineering Informatics 2026】融合条件扩散与图学习的 EEG 信号重建与认知负荷识别
人工智能·深度学习·神经网络