PyTorch:预期输入 batch_size (12) 匹配目标 batch_size (64) [英] PyTorch: Expected input batch_size (12) to match target batch_size (64)

查看:484
本文介绍了PyTorch:预期输入 batch_size (12) 匹配目标 batch_size (64)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了 PyTorch 并想为 MNIST 编写一个程序.但是,我收到了错误消息:

I tried out PyTorch and wanted to write a program for MNIST. But, I got the error message:

预期输入batch_size (12) 匹配目标batch_size (64)

Expected input batch_size (12) to match target batch_size (64)

我搜索了一个解决方案,但我不明白我的代码有什么问题.

I searched for a solution but I don't understand what's wrong with my code.

#kwargs is empty because I don't use cuda
kwargs = {}
train_data = torch.utils.data.DataLoader(
    datasets.MNIST('data', train=True, download=True,
                    transform=transforms.Compose([transforms.ToTensor(),
                    transforms.Normalize((0.1307,),(0.3081,))])),
    batch_size=64, shuffle=True, **kwargs)

test_data = torch.utils.data.DataLoader(
    datasets.MNIST('data', train=False,
                    transform=transforms.Compose([transforms.ToTensor(),
                    transforms.Normalize((0.1307,),(0.3081,))])),
    batch_size=64, shuffle=True, **kwargs)

class Netz(nn.Module):
    def __init__(self):
        super(Netz, self).__init__()
        self.conv1 = nn.Conv2d(1,10, kernel_size=5)
        self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
        self.conv_dropout = nn.Dropout2d()
        self.fc1 = nn.Linear(320, 60)
        self.fc2 = nn.Linear(60, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = F.max_pool2d(x, 2)
        x = F.relu(x)
        x = self.conv2(x)
        x = self.conv_dropout(x)
        x = F.max_pool2d(x, 2)
        x = F.relu(x)
        print(x.shape)
        x = x.view(-1, 320)
        x = self.fc1(x)
        x = x.view(-1, 320)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return F.log_softmax(x, dim=0)

model = Netz()

optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=0.8)
def train(epoch):
    model.train()

    for batch_id, (data, target) in enumerate(train_data):
        data = Variable(data)
        target = Variable(target)
        optimizer.zero_grad()
        out = model(data)
        print(out.shape)
        criterion = nn.CrossEntropyLoss()
        loss = criterion(out, target)
        loss.backward()
        optimizer.step()
        print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'. format(
            epoch, batch_id * len(data), len(train_data.dataset),
            100. * batch_id / len(train_data), loss.data[0]))

输出应该显示时代和其他一些信息.实际上,我打印了张量的形状,但我不知道出了什么问题.这是错误信息:

The output should show the epoch and some other information. Actually, I print out the shape of my tensor but I don't know what's wrong. Here is the error message:

/home/michael/Programmierung/Python/PyTorch/venv/bin/python /home/michael/Programmierung/Python/PyTorch/mnist.py
torch.Size([64, 20, 4, 4])
torch.Size([12, 10])
Traceback (most recent call last):
  File "/home/michael/Programmierung/Python/PyTorch/mnist.py", line 69, in <module>
    train(epoch)
  File "/home/michael/Programmierung/Python/PyTorch/mnist.py", line 60, in train
    loss = criterion(out, target)
  File "/home/michael/Programmierung/Python/PyTorch/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/michael/Programmierung/Python/PyTorch/venv/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 942, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)
  File "/home/michael/Programmierung/Python/PyTorch/venv/lib/python3.6/site-packages/torch/nn/functional.py", line 2056, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "/home/michael/Programmierung/Python/PyTorch/venv/lib/python3.6/site-packages/torch/nn/functional.py", line 1869, in nll_loss
    .format(input.size(0), target.size(0)))
ValueError: Expected input batch_size (12) to match target batch_size (64).

Process finished with exit code 1

推荐答案

发生错误是因为您的模型输出 out 的形状为 (12, 10),而您的 target 的长度为 64.

The error occurs because your model output, out, has shape (12, 10), while your target has a length of 64.

由于您使用的是 64 的批量大小并预测 10 个类别的概率,因此您希望模型输出的形状为 (64, 10),因此很明显,其中存在一些问题forward() 方法.

Since you are using a batch size of 64 and predicting the probabilities of 10 classes, you would expect your model output to be of shape (64, 10), so clearly there is something amiss in the forward() method.

逐行检查并注意每一步 x 的大小,我们可以尝试找出问题所在:

Going through it line by line and noting the size of x at every step, we can try to find out what is going wrong:

    ...
    # x.shape = (64, 20, 4, 4) at this point as seen in your print statement
    x = x.view(-1, 320)             # x.shape = (64, 320)
    x = self.fc1(x)                 # x.shape = (64, 60)
    x = x.view(-1, 320)             # x.shape = (12, 320)
    x = F.relu(self.fc1(x))         # x.shape = (12, 60)
    x = self.fc2(x)                 # x.shape = (12, 10)
    return F.log_softmax(x, dim=0)  # x.shape = (12, 10)

您实际上最可能想要的是:

What you actually most likely want is:

    ...
    # x.shape = (64, 20, 4, 4) at this point as seen in your print statement
    x = x.view(-1, 320)             # x.shape = (64, 320)
    x = F.relu(self.fc1(x))         # x.shape = (64, 60)
    x = self.fc2(x)                 # x.shape = (64, 10)
    return F.log_softmax(x, dim=1)  # x.shape = (64, 10)

注意:虽然与错误无关,但还要注意您希望在 dim=1 上进行 softmax,因为这是包含类对数的维度.

Note: While not related to the error, note also that you want to softmax over dim=1 since that is the dimension that contains the logits for the classes.

这篇关于PyTorch:预期输入 batch_size (12) 匹配目标 batch_size (64)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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