PyTorch模型验证:张量a(32)的大小必须与张量b(13)的大小匹配 [英] PyTorch model validation: The size of tensor a (32) must match the size of tensor b (13)

查看:118
本文介绍了PyTorch模型验证:张量a(32)的大小必须与张量b(13)的大小匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在机器学习方面,我是一个非常初学者.因此,出于学习目的,我试图开发一个简单的CNN来对棋子进行分类.网络已经可以使用,我可以对其进行训练,但是我的验证功能存在问题.

I am a very beginner in case of machine learning. So for learning purpose I am trying to develop a simple CNN to classify chess pieces. The net already works and I can train it but I have a problem with my validation function.

我无法将我的预测与我的 target_data 进行比较,因为我的预测只是大小为13的张量,而 target.data [batch_size] x13 .我不知道我的错误在哪里.PyTorch示例几乎都使用此功能将预测与目标数据进行比较.

I can't compare my prediction with my target_data because my prediction is only a tensor of size 13 while target.data is [batch_size]x13. I can't figure out where my mistake is. The PyTorch examples are almost all using this function to compare the prediction with the target data.

如果有人可以在这里帮助我,那将真是太好了.

It would be really great if anybody could help me out here.

您可以在此处查找其余代码: https://github.com/michaelwolz/ChessML/blob/master/train.ipynb

You can lookup the rest of the code here: https://github.com/michaelwolz/ChessML/blob/master/train.ipynb

def validate(model, validation_data, criterion):
    model.eval()
    loss = 0
    correct = 0

    for i in range(len(validation_data)):
        data, target = validation_data[i][0], validation_data[i][1]
        target = torch.Tensor(target)

        if torch.cuda.is_available():
            data = data.cuda()
            target = target.cuda()

        out = model(data)

        loss += criterion(out, target).item()

        _, prediction = torch.max(out.data, 1)
        correct += (prediction == target.data).sum().item()

    loss = loss / len(validation_data)
    print("###################################")
    print("Average loss:", loss)
    print("Accuracy:", 100. * correct / len(validation_data))
    print("###################################")

错误:

<ipython-input-6-6b21e2bfb8a6> in validate(model, validation_data, 

criterion)
     17 
     18         _, prediction = torch.max(out.data, 1)
---> 19         correct += (prediction == target.data).sum().item()
     20 
     21     loss = loss / len(validation_data)

RuntimeError: The size of tensor a (32) must match the size of tensor b (13) at non-singleton dimension 1

我的标签如下:

[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

每个索引代表一个类别.
torch.max()函数的输出似乎是该类的索引.我不明白如何将索引与 target_label 进行比较.我的意思是说我可以编写一个函数来检查预测的索引处是否有1,但是我认为我的错误在其他地方.

Each index represents one class.
The output of the torch.max() function seems to be the index of the class. I don't understand how I could compare the index to the target_label. I mean I could just write a function which checks if there is a 1 at the predicted index but I think that my mistake is somewhere else.

推荐答案

也只需在目标上运行"argmax":

Simply run "argmax" on the target as well:

_, target = torch.max(target.data, 1)

或更妙的是,只需将目标保持为 [example_1_class,example_2_class,...] ,而不是1-hot编码.

Or better yet, just keep the target around as [example_1_class, example_2_class, ...], instead of 1-hot encoding.

这篇关于PyTorch模型验证:张量a(32)的大小必须与张量b(13)的大小匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆