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

均方差

反向传播

相关推荐
Omics Pro1 分钟前
免费!糖蛋白质组学数据分析
开发语言·深度学习·数据挖掘·数据分析·r语言·excel·知识图谱
MediaTea6 分钟前
DL:深度学习的主要任务
人工智能·深度学习
水木流年追梦13 分钟前
大模型入门-Pre-Training、SFT、RLHF
人工智能·深度学习·机器学习
malog_22 分钟前
PyTorch图像数据加载实战指南
图像处理·人工智能·pytorch·python
Yunzenn27 分钟前
深度分析字节最新研究cola-DLM第 01 章:语言生成的三次范式之争 —— 从 RNN 到 AR 到扩散
linux·人工智能·rnn·深度学习·机器学习·架构·transformer
盼小辉丶30 分钟前
PyTorch强化学习实战——Atari游戏包装器
pytorch·深度学习·强化学习
子榆.33 分钟前
CANN PyTorch适配器深度拆解:从.cuda()到.npu()到底发生了什么
人工智能·pytorch·python
renke336439 分钟前
写给前端的 CANN-torchtitan-npu:昇腾PyTorch Titan适配到底是啥?
前端·人工智能·pytorch·cann
多年小白41 分钟前
今日A股 拉
大数据·人工智能·深度学习·microsoft·ai
初心未改HD11 小时前
深度学习之CNN卷积层详解
人工智能·深度学习·cnn