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

均方差

反向传播

相关推荐
心易行者4 分钟前
基于自然语言处理的智能客服系统构建:中文AI的实践智慧
人工智能·深度学习·transformer
艾卡西亚丶暴雨L9 分钟前
【pytorch11】高阶操作
pytorch
Cpdr16 分钟前
pytorch自适应的调整特征图大小
pytorch·python·深度学习
图灵追慕者1 小时前
深度学习之OpenCV的DNN模块
深度学习·opencv·dnn
悟兰因w2 小时前
Pytorch实战(二):VGG神经网络
人工智能·pytorch·神经网络
无水先生2 小时前
AlphaGo 背后的人工智能:机器学习和神经网络
人工智能·神经网络·机器学习
拉达曼迪斯II2 小时前
14-21 人工智能的历史以及简单神经网络的工作原理
人工智能·深度学习·神经网络·搜索引擎·ai·aigc·创业创新
傻啦嘿哟2 小时前
TensorFlow与PyTorch的对比与选择(Python深度学习)
python·深度学习·tensorflow
组学之心4 小时前
Diffusion模型的微调和引导
深度学习·stable diffusion
大霸王龙5 小时前
深度学习中,模型的构建和训练过程中会用到多种函数
人工智能·深度学习