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)