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

IndexError: too many indices for tensor of dimension 1 #8

Open
mfelice opened this issue Mar 24, 2021 · 2 comments
Open

IndexError: too many indices for tensor of dimension 1 #8

mfelice opened this issue Mar 24, 2021 · 2 comments

Comments

@mfelice
Copy link

mfelice commented Mar 24, 2021

Hi there,

I'm using the PyTorch implementation with bert-base-uncased and I get the following error when the sentence contains only one token:

Traceback (most recent call last):
  File "bert.py", line 28, in <module>
    print(scorer.score_sentences(["Hello"]))
  File ".../mlm-scoring/src/mlm/scorers.py", line 167, in score_sentences
    return self.score(corpus, **kwargs)[0]
  File ".../mlm-scoring/src/mlm/scorers.py", line 757, in score
    out = out[list(range(split_size)), token_masked_ids]
IndexError: too many indices for tensor of dimension 1

It works fine with MXNet MLMs, but I need to use a community model from HuggingFace.

Thanks!

@mfelice
Copy link
Author

mfelice commented Mar 25, 2021

OK, I think I found the problem.

out = out[0].squeeze()

should be changed to:

out = torch.reshape(out[0], (out[0].shape[0], -1))

squeeze() was removing a dimension that should be preserved.

@DarrenAbramson
Copy link

Hurray for publicly licensed software and donation of labour to the public good!

miidas added a commit to miidas/mlm-scoring that referenced this issue Jan 31, 2022
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