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

均方差

反向传播

相关推荐
布吉岛的石头2 小时前
Java 程序员第 50 阶段2:预训练的目标函数:自回归语言建模本质
java·开发语言·人工智能·深度学习·回归·transformer
田里的水稻4 小时前
FA_CNN卷积神经网络七---优化器PyTorch
人工智能·pytorch·深度学习·神经网络·cnn
LXT7129 小时前
每日英语一读|为什么夏天的雷雨,总爱在傍晚出现?
深度学习·学习
海带紫菜菠萝汤10 小时前
MemT 1.0 开源:128K 历史装进 40 MiB,记忆从外挂搬进模型里
人工智能·深度学习·ai·开源·大模型
goehou12 小时前
LoRA 微调实战 - 显存、标签掩码与合并部署的踩坑记录(附清单)
深度学习·lora·大模型·模型微调
Smoothcloud润云12 小时前
GPU算力平台跑知识库问答,模型回答总跑偏?先排查检索链路
人工智能·python·深度学习·gpu算力·gpu
AI人工智能+13 小时前
通用表格识别技术如何通过五步AI流程,实现对复杂真实表格的智能解析
人工智能·深度学习·ocr·表格识别
LaughingZhu13 小时前
Product Hunt 热榜 2026-10-20:Beam.win、Figma Specs、readywhen
人工智能·深度学习·神经网络·搜索引擎·百度
frankzch14 小时前
HybridAgent 拆解:在潜空间想,动手时才出字
人工智能·深度学习·语言模型
小李同学_LHY14 小时前
大白话模型第三章:深度学习
人工智能·深度学习