RoBERTa for QuestionAnswering 
Author: mchari
Pull request: no

I am trying to replicate the example in this link 
https://github.com/huggingface/transformers/pull/1502/files, but I get the following error : 
``
ValueError                                Traceback (most recent call last)
<ipython-input-23-823cc70a5d4f> in <module>
----> 1 token_type_ids = [0 if i <= input_ids.index(102) else 1 for i in range(len(input_ids))]
      2 start_scores, end_scores = model(torch.tensor([input_ids]), token_type_ids=torch.tensor([token_type_ids]))
      3 all_tokens = tokenizer.convert_ids_to_tokens(input_ids)
      4 print(' '.join(all_tokens[torch.argmax(start_scores) : torch.argmax(end_scores)+1]))

<ipython-input-23-823cc70a5d4f> in <listcomp>(.0)
----> 1 token_type_ids = [0 if i <= input_ids.index(102) else 1 for i in range(len(input_ids))]
      2 start_scores, end_scores = model(torch.tensor([input_ids]), token_type_ids=torch.tensor([token_type_ids]))
      3 all_tokens = tokenizer.convert_ids_to_tokens(input_ids)
      4 print(' '.join(all_tokens[torch.argmax(start_scores) : torch.argmax(end_scores)+1]))

ValueError: 102 is not in list
``
I see the same error discussed in https://github.com/huggingface/transformers/issues/2261

Any ideas how I could resolve this issue ?
Thanks in advance.


Comments:
Hey @mchari, could you please post your environment information and a code sample that we can run to reproduce your error?

@patrickvonplaten , thanks for your reply.

i am using transformers 3.0.2 that i got from a pip install. 
Here is the code 
`
from transformers import RobertaTokenizer, RobertaForQuestionAnswering
import torch
import tensorflow

tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
print("Loaded tokenizer !!!")
model = RobertaForQuestionAnswering.from_pretrained('roberta-base')

print("Loaded QA model !!!")

question = "Who was Jim Henson?"
context = "Jim Henson was a nice puppet"

input_text = "[CLS] " + question + " [SEP] " + context + " [SEP]"
#input_text = question + " [SEP] " + context
#print(tokenizer(input_text))

input_ids = tokenizer.encode(input_text)
start_scores, end_scores = model(torch.tensor([input_ids]))
print(input_ids)

token_type_ids = [0 if i <= input_ids.index(102) else 1 for i in range(len(input_ids))] 
start_scores, end_scores = model(torch.tensor([input_ids]), token_type_ids=torch.tensor([token_type_ids]))
all_tokens = tokenizer.convert_ids_to_tokens(input_ids)  
print(' '.join(all_tokens[torch.argmax(start_scores) : torch.argmax(end_scores)+1]))
`

i was able to workaround the issue by using the following code that doesn't look for id 102...
not sure if it is equivalent....


encoding = tokenizer.encode_plus(question,context)
input_ids, attention_mask = encoding["input_ids"], encoding["attention_mask"]
start_scores, end_scores = model(torch.tensor([input_ids]), attention_mask=torch.tensor([attention_mask]))

your workaround is correct. The link you posted above points to a very old example. Please take a look at the example of the updated model of `BertForQuestionAnswering` : https://huggingface.co/transformers/model_doc/bert.html#transformers.BertForQuestionAnswering