简体   繁体   English

匹配 PyTorch 张量维度

[英]matching PyTorch tensor dimensions

I am having some issues with regards to the dimensionality of my tensors in my training function at present.目前,我在训练函数中的张量维数方面存在一些问题。 I am using the MNIST dataset, so 10 possible targets, and originally wrote the prototype code using a training batch size of 10, which was in retrospect not the wisest choice.我使用的是 MNIST 数据集,因此有 10 个可能的目标,并且最初使用 10 的训练批次大小编写原型代码,回想起来这不是最明智的选择。 It gave poor results during some earlier tests, and increasing the amount of training iterations saw no benefit.它在一些早期的测试中给出了糟糕的结果,增加训练迭代次数也没有任何好处。 Upon trying to then increase the batch size, I realised that what I had written was not that general, and I was likely never training it on the proper data.在尝试增加批量大小后,我意识到我写的内容并不那么通用,而且我可能从未在正确的数据上对其进行过训练。 Below is my training function:下面是我的训练函数:

def Train(tLoops, Lrate):
    for _ in range(tLoops):
        tempData = train_data.view(batch_size_train, 1, 1, -1)
        output = net(tempData)
        trainTarget = train_targets
        criterion = nn.MSELoss()
        print("target:", trainTarget.size())
        print("Output:", output.size())
        loss = criterion(output, trainTarget.float())
        # print("error is:", loss)
        net.zero_grad()  # zeroes the gradient buffers of all parameters
        loss.backward()
        for j in net.parameters():
            j.data.sub_(j.grad.data * Lrate)

of which the print functions return其中打印函数返回

target: torch.Size([100])
Output: torch.Size([100, 1, 1, 10])

before the error message on the line where loss is calculated;在计算损失的行上的错误消息之前;

RuntimeError: The size of tensor a (10) must match the size of tensor b (100) at non-singleton dimension 3

The first print, target, is a 1-dimensional list of the respective ground truth values for each image.第一个打印,目标,是每个图像的相应地面实况值的一维列表。 Output contains the output of the neural net for each of those 100 samples, so a 10 x 100 list, however from skimming and reshaping the data from 28 x 28 to 1 x 784 earlier, I seem to have extra dimensions unnecessarily.输出包含这 100 个样本中每一个的神经网络的输出,因此是一个 10 x 100 的列表,但是从之前的 28 x 28 到 1 x 784 的略读和整形数据中,我似乎有不必要的额外维度。 Does PyTorch provide a way to remove these? PyTorch 是否提供了删除这些的方法? I couldn't find anything in the documentation, or is there something else that could be my issue?我在文档中找不到任何内容,或者还有其他可能是我的问题吗?

There are several problems in your training script.您的训练脚本中有几个问题。 I will address each of them below.我将在下面逐一说明。

  1. First, you should NOT do data batching by hand.首先,您不应该手动进行数据批处理。 Pytorch/torchvision have functions for that, use a dataset and a data loader: https://pytorch.org/tutorials/recipes/recipes/loading_data_recipe.html . Pytorch/torchvision 有这方面的功能,使用数据集和数据加载器: https ://pytorch.org/tutorials/recipes/recipes/loading_data_recipe.html。

  2. You should also NEVER update the parameters of you network by hand.您也不应该手动更新网络参数。 Use an Optimizer: https://pytorch.org/docs/stable/optim.html .使用优化器: https : //pytorch.org/docs/stable/optim.html In your case, SGD without momentum will have the same effect.在您的情况下,没有动量的 SGD 将具有相同的效果。

  3. The dimensionality of your input seems to be wrong, for MNIST an input tensor should be (batch_size, 1, 28, 28) or (batch_size, 784) if you're training a MLP.您输入的维度似乎是错误的,对于 MNIST,如果您正在训练 MLP,则输入张量应该是 (batch_size, 1, 28, 28) 或 (batch_size, 784)。 Furthermore, the output of your network should be (batch_size, 10)此外,您的网络的输出应该是 (batch_size, 10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM