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

均方差

反向传播

相关推荐
Gigavision3 小时前
基于BUAA-MIHR数据集的噪声解耦对比学习算法
人工智能·python·深度学习·算法
计算机编程-吉哥4 小时前
YOLO26 vs YOLO11 vs YOLOv8:深度学习咖啡果实成熟度分割系统【计算机毕业设计选题推荐】
人工智能·python·深度学习·yolo·django·毕业设计
金色印象9 小时前
【论文解读】DRSN:把软阈值去噪“内化”进残差网络的强噪声故障诊断方法
深度学习·残差网络·故障诊断·软阈值·收缩函数·强噪声·drsn
Ivanqhz11 小时前
数据搬运是性能关键
java·服务器·网络·人工智能·深度学习
阳明山水11 小时前
校准分位数驱动智能库存决策
人工智能·深度学习·算法·机器学习·架构
xx_xxxxx_12 小时前
论文阅读-Search-R1
人工智能·深度学习·机器学习·强化学习
Ivanqhz12 小时前
Slope One 算法详解:与矩阵分解、共现矩阵的异同
java·服务器·网络·人工智能·深度学习
Ivanqhz13 小时前
Ping-Pong 双缓冲
开发语言·人工智能·python·深度学习·mlir
三十岁老牛再出发13 小时前
9月18日总结
python·深度学习·机器学习
就叫你天选之人啦13 小时前
安装torch+vllm+flash_attn的prompt
人工智能·pytorch·python