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

均方差

反向传播

相关推荐
沪漂阿龙18 小时前
面试题:神经网络的优化怎么讲?梯度消失、Adam、BN、Dropout、权重初始化一文讲透
人工智能·深度学习·神经网络
mingo_敏19 小时前
深度学习论文: Per-Pixel Classification is Not All You Need for Semantic Segmentation
人工智能·深度学习
深度学习lover20 小时前
<数据集>yolo 交通违规标志识别<目标检测>
人工智能·深度学习·yolo·目标检测·计算机视觉·交通违规标志识别
LaughingZhu1 天前
Product Hunt 每日热榜 | 2026-05-12
大数据·人工智能·经验分享·神经网络·产品运营
MediaTea1 天前
人工智能通识课:机器学习之无监督学习
人工智能·深度学习·学习·机器学习
一切皆是因缘际会1 天前
2026实战:AI可解释性落地全指南
人工智能·深度学习·机器学习·架构
keineahnung23451 天前
PyTorch SymNode 為何找不到方法實作?──sizes_strides_methods 動態安裝機制解析
人工智能·pytorch·python·深度学习
沪漂阿龙1 天前
面试题:卷积神经网络(CNN)是什么?核心层、卷积核、池化、1×1 卷积、VGG、ResNet 一文讲透
人工智能·神经网络·cnn
老鱼说AI1 天前
现代 LangChain 开发指南:从 LCEL 原理到企业级 RAG 与 Agent 实战
java·开发语言·人工智能·深度学习·神经网络·算法·机器学习
咕噜咕噜啦啦1 天前
RTX5090配置DGL
pytorch·python·conda·pip