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

均方差

反向传播

相关推荐
隔壁大炮17 分钟前
Day02-13.张量的拼接操作
人工智能·pytorch·深度学习·神经网络·numpy
数智工坊21 分钟前
DeepLabv3+:融合空洞可分离卷积的编码器-解码器语义分割架构
人工智能·深度学习·cnn
盼小辉丶35 分钟前
PyTorch强化学习实战(3)——Gymnasium API扩展功能
人工智能·pytorch·深度学习·强化学习
AI木马人9 小时前
3.【Prompt工程实战】如何设计一个可复用的Prompt系统?(避免每次手写提示词)
linux·服务器·人工智能·深度学习·prompt
ydmy10 小时前
transformer超参数配置(个人理解)
人工智能·深度学习
AI木马人13 小时前
6.深度学习入门:神经网络是如何“思考”的?
人工智能·深度学习·神经网络
小鱼~~14 小时前
TensorDataset简介
深度学习
毕胜客源码16 小时前
卷积神经网络的农作物识别系统(有技术文档)深度学习 图像识别 卷积神经网络 Django python 人工智能
人工智能·python·深度学习·cnn·django
小鱼~~16 小时前
GRU模型简介
人工智能·深度学习
小鱼~~17 小时前
DataLoader简介
人工智能·深度学习