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

均方差

反向传播

相关推荐
汤姆宝库1 小时前
视觉语言模型的实现:LLaVA式投影与指令微调
python·深度学习·语言模型
严肃小白兔17 小时前
第 14/24 期:正则化方法及其对神经网络的影响
人工智能·神经网络·机器学习
江润舟18 小时前
万物|炼器:从零手搓工业级旋转目标检测网络·卷7 —— OBB检测头与DFL解码(三)
人工智能·python·深度学习
天树星智源1 天前
语言模型分得出 AI 文本却答反了:后训练改的是读法,不是表征(Δd′ = −0.34)
人工智能·深度学习·语言模型
橘子131 天前
神经网络基础
人工智能·深度学习·神经网络
伞伞悦读2 天前
【第92期】逻辑回归的分数概率与阈值的实验与排查
人工智能·python·深度学习·机器学习
2601_967979122 天前
【一】tensorflow安装、常用python镜像源、tensorflow 深度学习强化学习教学
深度学习·tensorflow·安装教程·anaconda·gpu安装
tellmewhoisi2 天前
深度学习0:数学知识:最大似然推导过程
人工智能·深度学习·概率论
在所不辞兄3 天前
为什么数字孪生驱动的产线标定需要在线更新机制,而不能一次标定后长期沿用?
人工智能·神经网络·其他·算法·机器学习
Oiiouui3 天前
原始CNN识别与训练模型实现
神经网络·机器学习·cnn