Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RobertaForClassification throws an error because of dimension mismatch #31780

Open
4 tasks
ErikVogelLUH opened this issue Jul 3, 2024 · 1 comment
Open
4 tasks

Comments

@ErikVogelLUH
Copy link

System Info

transformers version 4.41.2, Windows 10

Who can help?

@ArthurZucker

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

I wanted to train a Roberta model for classification. However, during the computation of the loss for multi-label classification, the dimensions are mismatched. I found the problem in transformers/models/roberta/modeling_roberta.py:1229:

loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))

The labels are flattened but the logits are formed to have dimensions (batch_size, num_labels). Slightly changing the line fixes the problem

loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1, self.num_labels))

Expected behavior

Compute the loss without the dimension mismatch

@LysandreJik
Copy link
Member

Hey @ErikVogelLUH! It's a bit hard to help you without knowing the code that failed. We know this model works well with our scripts (such as this one: https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-classification)

Did you start from one of our scripts or from something else?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants