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

均方差

反向传播

相关推荐
鼎上西瓜刀11 小时前
labelimg在windows上的使用
人工智能·深度学习
茗创科技12 小时前
Cerebral Cortex|工作记忆中α-θ跨频率耦合支持功能分离而非整合的新证据
深度学习·神经网络·脑网络
Ronaldinho Gaúch12 小时前
梯度消失与梯度爆炸
人工智能·深度学习·机器学习
查无此人byebye12 小时前
硬核深度解析:KimiDeltaAttention 源码逐行精读+公式推导+复杂度优化(完整可运行)
人工智能·深度学习·神经网络·自然语言处理
丰海洋12 小时前
Transformer参数量
人工智能·深度学习·transformer
kingcjh9713 小时前
2.1 vLLM-Omni + Wan2.1-T2V-1.3B测试数据
深度学习
龙文浩_13 小时前
AI深度学习中的张量计算理论与实践
人工智能·神经网络
专业发呆业余科研13 小时前
从“炼金术”到“建筑学”:深度学习结构设计的五大范式
人工智能·深度学习·神经网络·机器学习
剑穗挂着新流苏31213 小时前
208_深度学习的鲁棒性之美:暂退法(Dropout)原理与实战
开发语言·pytorch·python·深度学习
数智工坊14 小时前
【深度学习基础】Focal Loss、Dice Loss、组合损失函数
人工智能·深度学习