神经网络实战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)

均方差

反向传播

相关推荐
XiaoMu_00129 分钟前
多场景头盔佩戴检测
人工智能·python·深度学习
努力毕业的小土博^_^1 小时前
【AI课程领学】基于SmolVLM2与Qwen3的多模态模型拼接实践:从零构建视觉语言模型(一)
人工智能·深度学习·神经网络·机器学习·语言模型·自然语言处理
JOYCE_Leo162 小时前
Learning Diffusion Texture Priors for Image Restoration(DTPM)-CVPR2024
深度学习·扩散模型·图像复原
Dev7z2 小时前
基于神经网络的风电机组齿轮箱故障诊断研究与设计
人工智能·深度学习·神经网络
算法熔炉2 小时前
深度学习面试八股文(1)——训练
人工智能·深度学习·面试
算法熔炉2 小时前
深度学习面试八股文(2)——训练
人工智能·深度学习·算法
weixin_423196173 小时前
# Python 深度学习 初始化(超参数、权重、函数输入列表)避坑指南:None 占位、可变共享与工厂函数
人工智能·深度学习
西南胶带の池上桜12 小时前
1.Pytorch模型应用(线性与非线性预测)
人工智能·pytorch·python
杀生丸学AI12 小时前
【无标题】VGGT4D:用于4D场景重建的视觉Transformer运动线索挖掘
人工智能·深度学习·3d·aigc·transformer·三维重建·视觉大模型
sali-tec13 小时前
C# 基于halcon的视觉工作流-章67 深度学习-分类
开发语言·图像处理·人工智能·深度学习·算法·计算机视觉·分类