Pytorch中十九种损失函数具体应用方法详细介绍?

2026-05-26 20:250阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1771个文字,预计阅读时间需要8分钟。

Pytorch中十九种损失函数具体应用方法详细介绍?

损失函数通过torch.nn包实现,基本用法如下:pythoncriterion=LossCriterion()

损失函数通过torch.nn包实现,

1 基本用法

criterion = LossCriterion() #构造函数有自己的参数 loss = criterion(x, y) #调用标准时也有参数

2 损失函数

2-1 L1范数损失 L1Loss

计算 output 和 target 之差的绝对值。

torch.nn.L1Loss(reduction='mean')

参数:

reduction-三个值,none: 不使用约简;mean:返回loss和的平均值; sum:返回loss的和。默认:mean。

2-2 均方误差损失 MSELoss

计算 output 和 target 之差的均方差。

torch.nn.MSELoss(reduction='mean')

参数:

reduction-三个值,none: 不使用约简;mean:返回loss和的平均值; sum:返回loss的和。默认:mean。

2-3 交叉熵损失 CrossEntropyLoss

当训练有 C 个类别的分类问题时很有效. 可选参数 weight 必须是一个1维 Tensor, 权重将被分配给各个类别. 对于不平衡的训练集非常有效。

阅读全文

本文共计1771个文字,预计阅读时间需要8分钟。

Pytorch中十九种损失函数具体应用方法详细介绍?

损失函数通过torch.nn包实现,基本用法如下:pythoncriterion=LossCriterion()

损失函数通过torch.nn包实现,

1 基本用法

criterion = LossCriterion() #构造函数有自己的参数 loss = criterion(x, y) #调用标准时也有参数

2 损失函数

2-1 L1范数损失 L1Loss

计算 output 和 target 之差的绝对值。

torch.nn.L1Loss(reduction='mean')

参数:

reduction-三个值,none: 不使用约简;mean:返回loss和的平均值; sum:返回loss的和。默认:mean。

2-2 均方误差损失 MSELoss

计算 output 和 target 之差的均方差。

torch.nn.MSELoss(reduction='mean')

参数:

reduction-三个值,none: 不使用约简;mean:返回loss和的平均值; sum:返回loss的和。默认:mean。

2-3 交叉熵损失 CrossEntropyLoss

当训练有 C 个类别的分类问题时很有效. 可选参数 weight 必须是一个1维 Tensor, 权重将被分配给各个类别. 对于不平衡的训练集非常有效。

阅读全文