RuntimeError: non-empty 3D or 4D input tensor expected but got ndim: 4

When I started feeding data through my Pytorch net, I got this error: RuntimeError: non-empty 3D or 4D input tensor expected but got ndim: 4 .

This is my net:

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, 5, 1)
        self.conv2 = nn.Conv2d(32, 64, 5, 1)
        self.conv3 = nn.Conv2d(64, 128, 5, 1)
        self.fc1 = nn.Linear(3*3*3*128, 512)
        self.fc2 = nn.Linear(512, 256)
        self.fc3 = nn.Linear(256, 16)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.max_pool2d(x, 2, 2)
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2, 2)
        x = F.relu(self.conv3(x))
        x = F.max_pool2d(x, 2, 2)
        x = x.view(-1, 3*3*3*128)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return F.log_softmax(x, dim=1)

Complete message:

Traceback (most recent call last):
  File "model.py", line 145, in <module>
    outputs = net(batch_X)
  File "C:\Users\-\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "model.py", line 97, in forward
    x = F.max_pool2d(x, 2, 2)
  File "C:\Users\-\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\_jit_internal.py", line 181, in fn
    return if_false(*args, **kwargs)
  File "C:\Users\-\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\nn\functional.py", line 487, in _max_pool2d
    return torch.max_pool2d(
RuntimeError: non-empty 3D or 4D input tensor expected but got ndim: 4

Would anybody know what I can do to fix it?

This error would be raised, if you pass an empty tensor to the pooling layer as shown here:

pool = nn.MaxPool2d(2)
x = torch.randn(1, 3, 24, 24)
out = pool(x) # works

y = torch.tensor([[[[]]]])
out = pool(y)
> RuntimeError: non-empty 3D or 4D input tensor expected but got ndim: 4

Could you check the shape of each tensor you are passing to the pooling layers?

1 Like