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

QA Documentation: I got error just copy and pasting documentation #10210

Closed
andreabac3 opened this issue Feb 16, 2021 · 6 comments
Closed

QA Documentation: I got error just copy and pasting documentation #10210

andreabac3 opened this issue Feb 16, 2021 · 6 comments

Comments

@andreabac3
Copy link
Contributor

andreabac3 commented Feb 16, 2021

Environment info

  • transformers version: 4.3.1
  • Platform:Manjaro Linux
  • Python version: 1.5.1
  • PyTorch version (GPU?): Yes
  • Using GPU in script?: Yes
  • Using distributed or parallel set-up in script?: No

Who can help

@sgugger

Information

I am trying the to train a QA model following the huggingface documentation, I just copied and pasted the code in my machine (and in Colab) but I was not able to proceed in the training phase because I got None value.

To reproduce

Steps to reproduce the behavior:

  1. Go to the documentation: https://huggingface.co/transformers/custom_datasets.html at Squad training section
  2. Copy and paste the code as you can see from my pastebin: https://pastebin.com/hZvq7Zs7
  3. And you got the following error
    File "/home/andrea/PycharmProjects/qa-srl/test.py", line 78, in __getitem__ return {key: torch.tensor(val[idx]) for key, val in self.encodings.items()} RuntimeError: Could not infer dtype of NoneType
  4. My naive solution was modifying the getitem method from the SquadDataset class in order to avoid to serve the val[idx] == None
@sgugger
Copy link
Collaborator

sgugger commented Feb 16, 2021

Pinging @joeddav on this one, since he wrote this tutorial :-)

@andreabac3
Copy link
Contributor Author

Thank you @sgugger for the reply.
Ok I can wait for the answer from @joeddav.

Have a nice day.

@joeddav
Copy link
Contributor

joeddav commented Feb 16, 2021

Figured it out. answer_end is the character position immediately after the answer, so end_position should be derived from answer_end - 1. I'm not sure why I was able to run it without this error previously (perhaps a resolved tokenizer bug?), but this should be correct.

def add_token_positions(encodings, answers):
    start_positions = []
    end_positions = []
    for i in range(len(answers)):
        start_positions.append(encodings.char_to_token(i, answers[i]['answer_start']))
        end_positions.append(encodings.char_to_token(i, answers[i]['answer_end'] - 1))
 
        # if start position is None, the answer passage has been truncated
        if start_positions[-1] is None:
            start_positions[-1] = tokenizer.model_max_length
            end_positions[-1] = tokenizer.model_max_length

    encodings.update({'start_positions': start_positions, 'end_positions': end_positions})

@LysandreJik
Copy link
Member

Closed by #10217

@andreabac3
Copy link
Contributor Author

Thank you @joeddav the posted code works perfectly.

@andreabac3
Copy link
Contributor Author

Sorry for bothering you @joeddav again, I have a question related to the code posted by you here.
I am still getting None with the dataset built by myself using this code. My dataset works perfectly with the run_squad original script.
In this snipped posted by you I encounter None in the vector of end_positions and I don't know how fix it. I saw the condition in which there's a None the start_positions but what I have to do in the case the None is only in the end_positions vector?

Kind regards,
Andrea

joeddav pushed a commit that referenced this issue Feb 25, 2021
* Fix None in add_token_positions - issue #10210

Fix None in add_token_positions related to the issue #10210

* add_token_positions fix None values in end_positions vector

add_token_positions fix None in end_positions vector as proposed by @joeddav
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

4 participants