CrossEntropyLoss()的几个例子

1. 实例

当使用torch.nn.CrossEntropyLoss()与2D输出矩阵和1D标签张量时,输出矩阵的每一行对应于单个样本的原始预测(logits),并且标签张量的每个元素包含相应样本的整数类标签

"""
Model outputs(outputs):
For the first sample: [2.0, -1.0, 0.5]
For the second sample : [-0.5, 1.0, 3.0 ]
target labels [1,2]

Softmax([2.0, -1.0, 0.5]) = [0.832, 0.017, 0.151]
Softmax([-0.5, 1.0, 3.0]) = [0.046, 0.118, 0.836]
Average Loss = (4.08 + 0.18) / 2 ≈ 2.13
"""

2.torch模拟计算交叉熵

import torch
import torch.nn.functional as F


def test2():
    # Example outputs (logits) and labels
    outputs = torch.tensor([
        [ 1.2, -0.5,  0.3,  2.1],  # Raw predictions for sample 1
        [-0.8,  1.5,  2.3, -1.0],  # Raw predictions for sample 2
        [ 0.5, -1.0,  1.8,  0.2]   # Raw predictions for sample 3
    ])

    target = torch.tensor([2, 1, 3])  # Ground truth labels

    # Step 1: Compute softmax probabilities
    softmax_layer=torch.nn.Softmax(dim=1)
    softmax_outputs=softmax_layer(outputs)
    # or softmax_outputs = F.softmax(outputs, dim=1,)
    # softmax_outputs_size([3,4]) 

    # Step 2: Extract the predicted probabilities for the target labels
    predicted_probs = softmax_outputs[range(len(target)), target]
    # predicted_probs_size([3])

    # Step 3: Compute the negative log probabilities for the predicted classes
    neg_log_probs = -torch.log(predicted_probs)
    # neg_log_probs_size([3])

    # Step 4: Compute the mean of the negative log probabilities
    mean_loss = torch.mean(neg_log_probs)
    
    print(mean_loss.item())  # 1.851070761680603

test2()

 3.直接使用torch的函数

import torch
import torch.nn.functional as F
def test1():
    # Example outputs (logits) and labels
    outputs = torch.tensor([
        [ 1.2, -0.5,  0.3,  2.1],  # Raw predictions for sample 1
        [-0.8,  1.5,  2.3, -1.0],  # Raw predictions for sample 2
        [ 0.5, -1.0,  1.8,  0.2]   # Raw predictions for sample 3
    ])

    target = torch.tensor([2, 1, 3])  # Ground truth labels

    criterion=torch.nn.CrossEntropyLoss(reduction="mean")
    print(criterion(outputs,target)) # tensor(1.8511)

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值