我正在尝试访问 PyTorch 的交叉熵损失函数 (torch.nn.function.cross_entropy) 的特定值,我相信当输入是长度为 N 的向量时正在计算该值。我想访问 N 个个体的向量损失值;不是我认为返回的平均值或总和或什么。
通过查看文档,我尝试设置“reduction = None”;但是,它仍然返回一个标量值。该函数的默认设置是返回平均值。
这是错误消息:
Exception has occurred: IndexError
slice() cannot be applied to a 0-dim tensor.
IndexError: slice() cannot be applied to a 0-dim tensor.
这是导致错误的代码片段:
tMinusOne_loss = burnIn_model.loss(combined_tMinusOne_X, combined_tMinusOne_Y)
print("tMinusOne_loss:", tMinusOne_loss)
tMinusOne_first_loss = tMinusOne_loss[ :len(combined_tMinusOne_X_first)]
以下是错误发生之前从打印行打印出来的内容:
tMinusOne_loss: tensor(0.3171, grad_fn=<NllLossBackward0>)
谢谢!
如果我正确理解了这个问题,那么您正在计算大小为
(N)
或 (1, N)
(即单个项目,而不是批次)的向量的交叉熵损失。在这种情况下,只有一个预期损失值。 CrossEntropyLoss
为批次中的每一项生成一个值。
loss = nn.CrossEntropyLoss(reduction='none')
n_classes = 5
batch_size = 3
input = torch.randn(batch_size, n_classes)
target = torch.empty(batch_size, dtype=torch.long).random_(n_classes)
output = loss(input, target)
output.shape
> torch.Size([3]) # output is one value for each item in the batch
batch_size = 1
input = torch.randn(batch_size, n_classes)
target = torch.empty(batch_size, dtype=torch.long).random_(n_classes)
output = loss(input, target)
output.shape
> torch.Size([1]) # output is one value for each item in the batch
交叉熵计算为预期类别的负对数概率。由于每个项目只有一个目标类别,因此每个项目只有一个损失值。
loss = nn.CrossEntropyLoss(reduction='none')
n_classes = 5
batch_size = 1
input = torch.randn(batch_size, n_classes)
target = torch.empty(batch_size, dtype=torch.long).random_(n_classes)
loss_value = loss(input, target)
log_probs = nn.functional.log_softmax(input, dim=-1)
torch.allclose(-log_probs[:, target], loss_value) # CE loss is the same as negative log prob of expected class
> True