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

均方差

反向传播

相关推荐
材料科学研究15 小时前
深度学习实战:5天掌握神经网络与PINN
深度学习·神经网络·pinn
X在敲AI代码15 小时前
女娲补天系列--深度学习
人工智能·深度学习
剑穗挂着新流苏31215 小时前
201_深度学习的数学底座:PyTorch 线性代数与范数实战
pytorch·深度学习·线性代数
盼小辉丶15 小时前
PyTorch实战(38)——深度学习模型可解释性
人工智能·pytorch·深度学习
温九味闻醉16 小时前
Meta | HSTU:生成式推荐工业级方案
人工智能·深度学习·机器学习
香芋超新星16 小时前
服务器根目录爆满导致 PyTorch 安装失败(Errno 28 No space left on device)
服务器·pytorch·深度学习
sin°θ_陈16 小时前
前馈式3D Gaussian Splatting 研究地图(路线一):像素对齐高斯的起点——pixelSplat 与 latentSplat 在解决什么
python·深度学习·3d·aigc·webgl·3dgs·空间智能
junior_Xin16 小时前
机器学习深度学习beginning3
深度学习·机器学习
放下华子我只抽RuiKe516 小时前
深度学习学习路径规划
人工智能·深度学习·学习·github·ai agent·openclaw·development
金融小师妹16 小时前
局势边际缓和下的AI定价重构:金价4500关口面临路径选择与约束机制
大数据·深度学习·能源