ValueError: Target and input must have the same number of elements. target nelement (50) != input nelement (100)
I'm new to Pytorch so I tried to learn it by creating simple dogs vs cats classification.
The code:

class DogCatClassifier(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 32, 5)
        self.conv2 = nn.Conv2d(32, 64, 5)
        self.conv3 = nn.Conv2d(64, 128, 5)

        self.fc1 = nn.Linear(512, 256)
        self.fc2 = nn.Linear(256, 2)

    def forward(self, x):
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        print("1-st: ", x.shape)

        x = F.max_pool2d(F.relu(self.conv2(x)), (2, 2))
        print("2-nd: ", x.shape)

        x = F.max_pool2d(F.relu(self.conv3(x)), (2, 2))
        print("3-rd: ", x.shape)

        x = torch.flatten(x, start_dim=1)

        x = F.relu(self.fc1(x))
        print("6-th: ", x.shape)

        x = self.fc2(x)  # bc this is our output layer. No activation here.
        print("7-th: ", x.shape)

        x = F.sigmoid(x)
        print("8-th: ", x.shape)

        return x


I pass a single batch of data (data shape is (50, 1, 50, 50)

model = DogCatClassifier()

images, labels = next(iter(train_loader))

preds = model(images)
print(pred)

loss = F.binary_cross_entropy(preds, labels)


My prediction shape is (50, 2), so as I understand F.binary_cross_entropy(preds, labels) checks both predictions from a single image and that's why I get 100 predictions against 50 labels. Coming from tensorflow I thought that I could just implement the same logic like using sigmoid as last activation and binary_cross_entropy as loss function. What I don't understand is how to make this piece of code work.

Your problem arises because you are using binary cross entropy instead of regular cross entropy. As the name implies, it checks weather the label is correct or not thus the shape of both tensors (preds and labels in your code) should be the same. As you are giving the confidence of both classes, the BCE loss function gets confused and the code crashes. You can either do two things:

1- Change to F.cross_entropy(preds, label) as your loss function.

2- Change your code to pick the maximum value as the target.

  pred = pred.argmax(dim=1, keepdim=True) # gets the max value


Let me know if this works, if it doesn't please update with the new error.
