Forward method returns output of one example instead of whole batch


My simple CNN is as follows:

lass CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.conv2 = nn.Conv2d(6, 10, 5)
        #self.conv2_drop = nn.Dropout2d()
        self.fc1 = nn.Linear(446520, 50)
        self.fc2 = nn.Linear(50, 3)

    def forward(self, x):
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2(x), 2))
        x = x.view(-1, 446520)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x

And I compute output as follows:

		output = model.forward(data)

Here, data's dimension is 3 * 3 * height * weight. My label is a list of three elements. So output should be 3 * 3 (batch size = 3). But in this case, output is 1 * 3. I am not getting what’s wrong. I tried changing the batch size (i.e 4), still it returns 1 *3 matrix,

I think the view might lead to a wrong shape.
Try to change it to

x = x.view(x.size(0), 446520)
# or
x = x.view(x.size(0), -1)

I think you will probably get an size mismatch error.

Thanks. yes, I got a size mismatch error if I try second approach. In case of first one, it gives an error size '[3 x 446520]' is invalid for input with 446520 elements at /pytorch/torch/lib/TH/THStorage.c:41. What should I do next?

Print the shape of x in __forward__, run it once, and change your layer to the appropriate size.

def forward(self, x):
    x = x.view(x.size(0), -1)

One could of course calculate the shape manually, but I don’t know your input shape.

1 Like

Thanks, I figured it out.