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

均方差

反向传播

相关推荐
伞伞悦读8 小时前
【第83期】特征选择中的验证泄漏排查的实验与排查
人工智能·深度学习·机器学习
一只废狗狗狗狗狗狗狗狗狗9 小时前
自监督式学习
人工智能·深度学习·机器学习·bert
aifannihao11 小时前
AI 学习第 14 课:CNN 训 6 轮卡在 0.5352,数据没打乱
pytorch·python·深度学习·cnn·学习路线
萝卜小白12 小时前
深度学习基础面经
人工智能·深度学习
高洁0118 小时前
数据同化与动态状态估计:让孪生持续贴合现实
人工智能·python·深度学习·transformer·迁移学习
通信仿真爱好者18 小时前
第【139】期--基于深度学习的OFDM信道估计方法研究--python完整代码
python·深度学习·ofdm·信道估计
IT古董18 小时前
AI 资讯日报 | 2026年10月25日:Kimi K3.1 Pro开源逼近前沿,OpenAI改离线训练,Claude合并Cowork,神经网络黑箱获新解,具身智能竞技场落幕
人工智能·神经网络·开源
科研邪修法19 小时前
混合方法文献矩阵:先拆字段,再确定分类
人工智能·深度学习
电科_银尘19 小时前
【深度学习项目实战·30】PM2.5短期预测:用LSTM构造没有未来信息的滑动窗口
人工智能·深度学习·lstm
汤姆宝库19 小时前
LoRA的低秩数学与QLoRA实现
人工智能·深度学习·机器学习