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

均方差

反向传播

相关推荐
未知违规用户10 分钟前
大模型项目:RAG项目实战与FlagEmbedding模型
人工智能·windows·python·深度学习
李燚6 小时前
RAG 流水线设计:Eino 的 Loader → Transformer → Indexer → Retriever(第60篇-E46)
人工智能·深度学习·transformer·agent·rag·aiagent·eino
hoLzwEge12 小时前
CLAUDE.md:为 Claude Code 注入项目记忆
深度学习·程序员·代码规范
金斗潼关14 小时前
使用MLP神经网络模型预测质数
人工智能·深度学习·神经网络
魔镜er15 小时前
03-张量
人工智能·pytorch·python
Black_Rock_br18 小时前
打通 PyTorch Monarch 与 ROCm:单 Controller 架构的异构算力实战
人工智能·pytorch·python·开源
风痕天际18 小时前
Pytorch开发教程1——CUDA安装
人工智能·pytorch·python
心运软件18 小时前
基于深度学习的IMDB电影评论情感分析完整实现
人工智能·pytorch·深度学习·数据分析
魔镜er19 小时前
04-pytorch构建线性回归
人工智能·pytorch·线性回归
wuling12919 小时前
李沐《动手学深度学习》(Dive into Deep Learning)d2l 安装记录
人工智能·深度学习