Skip to content

Binary classification  #2

@GabrieleAraujo

Description

@GabrieleAraujo

Hi,

I'm encountering an issue during the training phase when using binary_cross_entropy_with_logits for binary classification. The error traceback is as follows:
ValueError: Target size (torch.Size([2])) must be the same as input size (torch.Size([2, 2]))
This error arises when I call the trainer.fit() method. From what I understand, the target size should match the input size produced by the model. However, in my case, it seems that the model is outputting a tensor of size [2, 2] instead of the expected [2].

Here is the relevant part of my code:
self.classifier = torch.nn.Linear(512, num_labels)
In my use case, num_labels is set to 2 for binary classification. I believe the issue might be related to how the model outputs are structured, given that binary classification typically expects a single output per instance rather than multiple logits.

Could you please advise on how to adjust the model or the loss function so that the target and input sizes match correctly for binary classification? Should I modify the classifier layer to output a single value (using num_labels=1) and then apply a sigmoid activation?

Any guidance on this would be greatly appreciated!

Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions