Python 通过Pytork转换EMNIST数据时出错

Python 通过Pytork转换EMNIST数据时出错,python,numpy,deep-learning,pytorch,mnist,Python,Numpy,Deep Learning,Pytorch,Mnist,我试图通过使用Pytork来训练我的EMNIST预测模型。 编辑:-这是colab针对该问题的链接 class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(28, 64, (5, 5), padding=2) self.conv1_bn = nn.BatchNorm2d(64) self.conv

我试图通过使用Pytork来训练我的EMNIST预测模型。 编辑:-这是colab针对该问题的链接

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(28, 64, (5, 5), padding=2)
        self.conv1_bn = nn.BatchNorm2d(64)
        self.conv2 = nn.Conv2d(64, 128, 2, padding=2)
        self.fc1 = nn.Linear(2048, 1024)
        self.dropout = nn.Dropout(0.3)
        self.fc2 = nn.Linear(1024, 512)
        self.bn = nn.BatchNorm1d(1)
        self.fc3 = nn.Linear(512, 128)
        self.fc4 = nn.Linear(128, 47)
    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.max_pool2d(x, 2, 2)
        x = self.conv1_bn(x)
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2, 2)
        x = x.view(-1, 2048)
        x = F.relu(self.fc1(x))
        x = self.dropout(x)
        x = self.fc2(x)
        x = x.view(-1, 1, 512)
        x = self.bn(x)
        x = x.view(-1, 512)
        x = self.fc3(x)
        x = self.fc4(x)
        return F.log_softmax(x, dim=1)
        return x
每当我训练我的模型时,我都会遇到如下所示的这种错误

<ipython-input-11-07c68cf1cac2> in forward(self, x)
     24     def forward(self, x):
     25         x = F.relu(self.conv1(x))
---> 26         x = F.max_pool2d(x, 2, 2)
     27         x = self.conv1_bn(x)
RuntimeError: Given input size: (64x28x1). Calculated output size: (64x14x0). Output size is too small
但是我再次得到下面提到的错误。也许问题就在于转换部分

/opt/conda/lib/python3.7/site-packages/torchvision/datasets/mnist.py:469: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at  /opt/conda/conda-bld/pytorch_1595629403081/work/torch/csrc/utils/tensor_numpy.cpp:141.)
  return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)
我想通过使用“ndarray.setflags(write=None,align=None,uic=None)”使特定的numpy数组可写,但我无法确定应该在何处以及哪些类型的数组可写,因为我直接使用->
“datasets.EMNIST(root,split=“balanced”,train=False,download=True,transform=transform\u valid)”

欢迎来到Stackoverflow

您的问题与
toTensor
变换无关,产生此错误的原因是您在
maxpool
中输入的张量的维数:该错误清楚地表明您正在尝试maxppol一个张量,其中一个维数为1(64,28,1),因此它将输出一个维数为0(64,14,0)的张量,这毫无意义

您需要检查您在模型中输入的张量的尺寸。它们肯定太小了。也许你在某个地方的
视图中犯了一个错误(如果没有


如果我可以试着猜测,你在开始时有一个张量大小
28x28x1
(典型的MNIST),你把它放在一个期望一个dims的张量
BxCxWxH
(批量大小,通道,宽度,高度),例如(B,1,28,28),但是你混淆了输入通道的宽度(28)(
nn.Conv2d>)(->28Hi,谢谢大家的热烈欢迎!这算是一个最小的可重复示例吗?你可以检查第三个单元格,我在data.view中提到的输入是(1,28,28,1)。您好,不是真的“最小”,但它可以。所以我在回答中所做的假设是准确的,所以您可以按照我的建议解决问题(将Conv2d和视图更改为
data.view(1,1,28,28)
。同时删除
转置(data,1,2)
,除非您有很好的理由这样做,我需要知道。您应该使用(1,1,28,28)维度的原因是您有一批带有一个通道的1个图像(灰色图像,rgb为3个通道),每个通道的宽度和高度为28像素。因此(B、C、W、H)=(1,1,28,28)(使用pytorch时的默认格式)
/opt/conda/lib/python3.7/site-packages/torchvision/datasets/mnist.py:469: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at  /opt/conda/conda-bld/pytorch_1595629403081/work/torch/csrc/utils/tensor_numpy.cpp:141.)
  return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)