通过Pytork转换EMNIST数据时出错

2024-06-25 06:37:28 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我试图寻找解决方案,发现我应该在之前转换数据。所以我试着用最常见的建议来改变它:

transform_valid = transforms.Compose(
    [
     transforms.ToTensor(),  
    ])

但是我再次得到下面提到的错误。也许问题就在于转换部分

/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)


Tags: thetoselfviewreturndefnnmax
1条回答
网友
1楼 · 发布于 2024-06-25 06:37:28

欢迎来到Stackoverflow

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

您需要检查您在模型中输入的张量的尺寸。它们肯定太小了。也许你在某个地方用了一个view错了(没有minimal reproducible example很难说)

如果我可以试着猜测,你在开始时有一个张量大小28x28x1(典型的MNIST),你把它放在一个convolution中,这个BxCxWxH(批量大小,通道,宽度,高度),比如(B,1,28,28),但是你混淆了输入通道的宽度(28)(nn.Conv2d(->28<-, 64, (5, 5), padding=2)

我相信你希望你的第一层是nn.Conv2d(1, 64, (5, 5), padding=2),你需要调整你的张量的大小,给它们形状(B, 1, 28, 28)(B的值由你决定),然后再把它们交给网络

旁注:关于可写numpy数组的警告是完全不相关的,它只是意味着pytorch可能会覆盖numpy数组的“不可写”数据。如果您不关心修改此numpy数组,可以忽略该警告

相关问题 更多 >