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

均方差

反向传播

相关推荐
狮子座明仔5 小时前
SkillRL:让AI智能体学会“练功升级“的递归技能强化学习框架
人工智能·深度学习·自然语言处理
小雨中_5 小时前
3.5 ReMax:用 Greedy 作为基线的 REINFORCE + RLOO
人工智能·python·深度学习·机器学习·自然语言处理
DeepModel6 小时前
【回归算法】Ridge回归详解
深度学习·机器学习·回归算法
肾透侧视攻城狮6 小时前
《解锁TensorFlow模型潜力:超参数、网络结构、训练过程优化与高级技巧一站式精讲》
人工智能·深度学习·tensorflow 模型调优·静态/动态学习率·批量大小选择·宽/深度调整技巧·dropout/早停法
岱宗夫up7 小时前
从代码模式到智能模式:AI时代的设计模式进化论
开发语言·python·深度学习·神经网络·自然语言处理·知识图谱
吾在学习路7 小时前
AoP-SAM: Automation of Prompts for Efficient Segmentation
人工智能·深度学习·算法·计算机视觉
技术宅学长8 小时前
Router门控网络简单介绍
人工智能·深度学习
冰西瓜6008 小时前
深度学习的数学原理(十二)—— CNN的反向传播
人工智能·深度学习·cnn
冰西瓜6008 小时前
深度学习的数学原理(十一)—— CNN:二维卷积的数学本质与图像特征提取
人工智能·深度学习·cnn
盼小辉丶8 小时前
PyTorch实战(29)——使用TorchServe部署PyTorch模型
人工智能·pytorch·深度学习·模型部署