神经网络实战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 小时前
【轻量化复现大模型 / KIMI K3】MTP / EAGLE‑3 模块交互式演示
人工智能·深度学习·算法
Be for thing13 小时前
Pytorch【12】transforms进阶|图像数据增强实战,提升模型泛化能力
人工智能·pytorch·机器学习
tellmewhoisi15 小时前
深度学习0:数学知识:(雅可比矩阵)
人工智能·深度学习·矩阵
谙弆悕博士19 小时前
从“预测下一个词”到“预测世界下一状态”:世界模型是不是下一个AGI叙事?
人工智能·gpt·深度学习·神经网络·agi·jepa·genie 4
咕泡科技19 小时前
关于机器学习,如何通俗理解全连接层(fully connected layer)?
人工智能·深度学习·机器学习
电科_银尘1 天前
【深度学习项目实战·22】灰度图像着色:用小型U-Net学习从灰度到RGB
人工智能·深度学习·学习
猿人谷2 天前
遗传编程的未来:从进化搜索到自主发现求解方法
人工智能·深度学习·神经网络·ai-native
泓博2 天前
Android仿写苹果缩放窗口
人工智能·gpt·深度学习
在所不辞兄2 天前
【人工智能驱动的电催化研究-07】CSV文件、路径与可追溯记录
人工智能·深度学习·神经网络·机器学习·有限元·电化学·电催化
jay神2 天前
基于 YOLOv8 的传送带异物检测系统(Web 版)
前端·人工智能·深度学习·yolo·计算机视觉·计算机毕业设计