Views
No views yet
batch_size = 32,
n_epochs = 2,
max_seq_len = 256,
learning_rate = 5e-5| Metric | Our XLM-Roberta Large | Our ParsBert |
|---|---|---|
| Exact Match | 66.56* | 47.44 |
| F1 | 87.31* | 81.96 |
1from transformers import AutoTokenizer, AutoModelForQuestionAnswering
2path = 'pedramyazdipoor/parsbert_question_answering_PQuAD'
3tokenizer = AutoTokenizer.from_pretrained(path)
4model = AutoModelForQuestionAnswering.from_pretrained(path)1from transformers import AutoTokenizer, TFAutoModelForQuestionAnswering
2path = 'pedramyazdipoor/parsbert_question_answering_PQuAD'
3tokenizer = AutoTokenizer.from_pretrained(path)
4model = TFAutoModelForQuestionAnswering.from_pretrained(path)1def generate_indexes(start_logits, end_logits, N, max_index):
2
3 output_start = start_logits
4 output_end = end_logits
5
6 start_indexes = np.arange(len(start_logits))
7 start_probs = output_start
8 list_start = dict(zip(start_indexes, start_probs.tolist()))
9 end_indexes = np.arange(len(end_logits))
10 end_probs = output_end
11 list_end = dict(zip(end_indexes, end_probs.tolist()))
12
13 sorted_start_list = sorted(list_start.items(), key=lambda x: x[1], reverse=True) #Descending sort by probability
14 sorted_end_list = sorted(list_end.items(), key=lambda x: x[1], reverse=True)
15
16 final_start_idx, final_end_idx = [[] for l in range(2)]
17
18 start_idx, end_idx, prob = 0, 0, (start_probs.tolist()[0] + end_probs.tolist()[0])
19 for a in range(0,N):
20 for b in range(0,N):
21 if (sorted_start_list[a][1] + sorted_end_list[b][1]) > prob :
22 if (sorted_start_list[a][0] <= sorted_end_list[b][0]) and (sorted_end_list[a][0] < max_index) :
23 prob = sorted_start_list[a][1] + sorted_end_list[b][1]
24 start_idx = sorted_start_list[a][0]
25 end_idx = sorted_end_list[b][0]
26 final_start_idx.append(start_idx)
27 final_end_idx.append(end_idx)
28
29 return final_start_idx[0], final_end_idx[0]1device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
2model.eval().to(device)
3text = 'سلام من پدرامم 26 سالمه'
4question = 'چند سالمه؟'
5encoding = tokenizer(text,question,add_special_tokens = True,
6 return_token_type_ids = True,
7 return_tensors = 'pt',
8 padding = True,
9 return_offsets_mapping = True,
10 truncation = 'only_first',
11 max_length = 32)
12out = model(encoding['input_ids'].to(device),encoding['attention_mask'].to(device), encoding['token_type_ids'].to(device))
13#we had to change some pieces of code to make it compatible with one answer generation at a time
14#If you have unanswerable questions, use out['start_logits'][0][0:] and out['end_logits'][0][0:] because <s> (the 1st token) is for this situation and must be compared with other tokens.
15#you can initialize max_index in generate_indexes() to put force on tokens being chosen to be within the context(end index must be less than seperator token).
16answer_start_index, answer_end_index = generate_indexes(out['start_logits'][0][1:], out['end_logits'][0][1:], 5, 0)
17print(tokenizer.tokenize(text + question))
18print(tokenizer.tokenize(text + question)[answer_start_index : (answer_end_index + 1)])
19>>> ['▁سلام', '▁من', '▁پدر', 'ام', 'م', '▁26', '▁سالم', 'ه', 'نام', 'م', '▁چیست', '؟']
20>>> ['▁26']