当前位置: 首页 > news >正文

pytorch -- torch.nn下的常用损失函数

1.基础

loss function损失函数:预测输出与实际输出 差距 越小越好

- 计算实际输出和目标之间的差距
- 为我们更新输出提供依据(反向传播

1. L1
torch.nn.L1Loss(size_average=None, reduce=None, reduction=‘mean’)
2. 平方差(L2)
torch.nn.MSELoss(size_average=None, reduce=None, reduction=‘mean’)
3. 交叉熵
torch.nn.CrossEntropyLoss(weight=None, size_average=None, ignore_index=-100, reduce=None, reduction=‘mean’, label_smoothing=0.0)

2. 例子

代码:

import torch
from torch import nninput = torch.tensor([1,2,3],dtype=torch.float32)
input = torch.reshape(input,[1,1,1,3])
target = torch.tensor([1,2,5],dtype=torch.float32)
target = torch.reshape(target,[1,1,1,3])
# L1
l1 = nn.L1Loss(reduction='sum')
result1 = l1(input,target)
print(result1)
# L2
l2 = nn.MSELoss()
result2 = l2(input,target)
print(result2)# 交叉熵损失
x = torch.tensor([0.1,0.2,0.3])
y = torch.tensor([1])
x = torch.reshape(x,[1,3])
loss_cross = nn.CrossEntropyLoss()
result = loss_cross(x,y)
print(result)

输出
在这里插入图片描述

http://www.lryc.cn/news/306839.html

相关文章:

  • daydayEXP: 支持自定义Poc文件的图形化漏洞利用工具
  • 无法访问云服务器上部署的Docker容器(二)
  • 在Pycharm中运行Django项目如何指定运行的端口
  • Android将 ViewBinding封装到BaseActivity基类中(Java版)
  • JSP实现数据传递与保存(一)
  • 【论文笔记之 YIN】YIN, a fundamental frequency estimator for speech and music
  • 水印相机小程序源码
  • NXP实战笔记(八):S32K3xx基于RTD-SDK在S32DS上配置LCU实现ABZ解码
  • 【深度好文】simhash文本去重流程
  • 主流的开发语言和开发环境介绍
  • List去重有几种方式
  • 使用C#+NPOI进行Excel处理,实现多个Excel文件的求和统计
  • 华清远见嵌入式学习——驱动开发——day9
  • formality:set_constant应用
  • sqllabs的order by注入
  • 《The Art of InnoDB》第二部分|第4章:深入结构-磁盘结构-redo log
  • 大模型安全相关论文
  • 回归预测 | Matlab实现PSO-BiLSTM-Attention粒子群算法优化双向长短期记忆神经网络融合注意力机制多变量回归预测
  • [算法沉淀记录] 排序算法 —— 堆排序
  • C++ //练习 9.33 在本节最后一个例子中,如果不将insert的结果赋予begin,将会发生什么?编写程序,去掉此赋值语句,验证你的答案。
  • [corCTF 2022] CoRJail: From Null Byte Overflow To Docker Escape
  • thinkphp6定时任务
  • 支持国密ssl的curl编译和测试验证(上)
  • 包装类详解
  • vue3与vue2的区别
  • SSL OV证书和DV、EV证书的区别
  • 一款.NET下 WPF UI框架介绍
  • 东莞IBM服务器维修之IBM x3630 M4阵列恢复
  • Flask基础学习4
  • mac安装zookeeper