pytorch损失函数

loss function with pytorch

我是 pytorch 的新手,当我看到 MNIST 数据集的教程时,目标是一个标量(从 0 到 9 的数字),模型的输出是一个层,是一个向量(最后的代码层是 nn.Linear(32,10)) 并且他们用 (loss=nn.CrossEntropyLoss() loss = loss(output,target) ) 计算损失 他们是在比较数字和向量吗?

我认为根据 PyTorch 文档 torch.nn.functional.cross_entropy() that the output is like you mentioned a tensor with shape (N,C) (N is batch size) and C is the number of classes, where the target is either shape (N) when containing only class indices and shape (N,C) when containing also class probabilities. Details about how they compute the actual cross entropy is mentioned here (pytorch docs)。所以是的,他们正在将数字与张量进行比较,因为数字试图显示哪个索引应为 1,因此其他索引应为 0。