The cause of the problem is here: to compare between predicts and labels, you should select the index of the maximum probability from each row in predicts tensor like that:
But predicts is bool tensor, and you cannot apply argmax to bool tensor directly. Therefore, you got the error message as you indicated in the comment. To solve this problem, you need only to do the following:
predicts = (output >= 0.5)*1
Now you can compare between the two tensors predicts, labels, because both have the same size.
Your problem is solved, but the accuracy is logically not correct. Therefore, be careful if you want to use sigmoid with multi-classification problem because you use output >= 0.5 however, you have 3 classes in the output. This is not correct because imagine you have in the output [0.15,0.45,0.4]. Your predict will be [0, 0, 0] and then argmax(1) will select the first index if there are equal numbers, however, the second index should be selected in this case because it has the largest probability. The best way to do that if you have a multi-classification problem is to use softmax instead of sigmoid (>= 0.5).
By the way, if you come back to your model structure (last line), you will find that you already used nn.LogSoftmax. You need just to remove this line predicts = (outputs >= 0.5) and use directly:
#before the for loop
num_corrects = 0
# inside the for loop
num_corrects = num_corrects + torch.sum(outputs.argmax(1) == Labels)
#outside the loop
train_accuracy = (100. * num_corrects / len(train_loader.dataset))
发布评论
评论(1)
大小为 [32,3] 的
output
张量 32 是小批量的数量,3 是神经网络的输出,例如当您比较
output
> > 时。 = 0.5 结果是predict
张量,但它是bool
张量,具有与输出 [32,3] 相同的大小:并且
Labels
是0D 张量32个值,例如问题的原因在这里:要比较
预测
和标签
,您应该从中的每一行中选择最大概率的索引>predicts
张量,如下所示:但是
predicts
是bool
张量,并且您不能将argmax
应用于直接 bool 张量。因此,您收到了评论中指出的错误消息。要解决这个问题,您只需执行以下操作:
现在您可以比较两个张量的预测、标签,因为它们的大小相同。
简而言之,您应该使用:
您的问题已解决,但准确性在逻辑上不正确。
因此,如果您想在多分类问题中使用 sigmoid,请务必小心,因为您使用
output >= 0.5
但是,输出中有 3 个类。这是不正确的,因为假设输出中有 [0.15,0.45,0.4]。您的预测
将为[0, 0, 0],然后如果数量相等,argmax(1)
将选择第一个索引,但是,应选择第二个索引在这种情况下,因为它的概率最大。如果您遇到多分类问题,最好的方法是使用softmax
而不是sigmoid (>= 0.5)
。顺便说一下,如果你回到你的模型结构(最后一行),你会发现你已经使用了 nn.LogSoftmax 。您只需删除此行
predicts = (outputs >= 0.5)
并直接使用:Your
output
tensor of the size [32,3] 32 is the number of mini-batches and 3 is the output of your neural network e.g.When you compare whether
output
>= 0.5 the result ispredict
tensor but it isbool
tensor with the same size of output [32,3] like that:and
Labels
is 0D tensor with 32 values e.g.The cause of the problem is here: to compare between
predicts
andlabels
, you should select the index of the maximum probability from each row inpredicts
tensor like that:But
predicts
isbool
tensor, and you cannot applyargmax
tobool
tensor directly. Therefore, you got the error message as you indicated in the comment. To solve this problem, you need only to do the following:Now you can compare between the two tensors
predicts, labels
, because both have the same size.In brief, you should use:
Your problem is solved, but the accuracy is logically not correct.
Therefore, be careful if you want to use sigmoid with multi-classification problem because you use
output >= 0.5
however, you have 3 classes in the output. This is not correct because imagine you have in the output [0.15,0.45,0.4]. Yourpredict
will be [0, 0, 0] and thenargmax(1)
will select the first index if there are equal numbers, however, the second index should be selected in this case because it has the largest probability. The best way to do that if you have a multi-classification problem is to usesoftmax
instead ofsigmoid (>= 0.5)
.By the way, if you come back to your model structure (last line), you will find that you already used
nn.LogSoftmax
. You need just to remove this linepredicts = (outputs >= 0.5)
and use directly: