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

均方差

反向传播

相关推荐
智算菩萨7 小时前
【How Far Are We From AGI】3 AGI的边界扩张——数字、物理与智能三重接口的技术实现与伦理困境
论文阅读·人工智能·深度学习·ai·agi
剑穗挂着新流苏3127 小时前
Pytorch加载数据
python·深度学习·transformer
智算菩萨11 小时前
【How Far Are We From AGI】6 AGI的进化论——从胚胎到终极的三级跃迁与发展路线图
论文阅读·人工智能·深度学习·ai·agi
梦醒过后说珍重11 小时前
【超分实战】拒绝灾难性遗忘!记一次原生4K医疗影像(SurgiSR4K)的模型微调踩坑实录
深度学习
梦醒过后说珍重11 小时前
拒绝显存溢出!手把手教你写原生 4K 超分辨率数据集 (SurgiSR4K) 的 PyTorch DataLoader
深度学习
junjunzai12312 小时前
设置cuda:1但是cuda:0在波动的问题
人工智能·深度学习
罗罗攀12 小时前
PyTorch学习笔记|张量的广播和科学运算
人工智能·pytorch·笔记·python·学习
智算菩萨14 小时前
多目标超启发式算法系统文献综述:人机协同大语言模型方法论深度精读
论文阅读·人工智能·深度学习·ai·多目标·综述
简单光学14 小时前
ISDM: 基于生成扩散模型的散射介质成像重建技术报告
深度学习·扩散模型·散射成像·分数匹配·随机微分方程
IT阳晨。14 小时前
PyTorch深度学习实践
人工智能·pytorch·深度学习