Views
No views yet
context, question, optionscontext + question + option. We follow the architecture/setup described in https://openreview.net/forum?id=HJgJtT4tvB).
The output is the logit over the options. This is the question answering (QA) component in our MQAG paper,
or please refer to the GitHub repo of this project: https://github.com/potsawee/mqag0.1>>> import torch
2>>> import numpy as np
3>>> from transformers import LongformerTokenizer, LongformerForMultipleChoice
4
5>>> tokenizer = LongformerTokenizer.from_pretrained("potsawee/longformer-large-4096-answering-race")
6>>> model = LongformerForMultipleChoice.from_pretrained("potsawee/longformer-large-4096-answering-race")
7
8>>> context = r"""Chelsea's mini-revival continued with a third victory in a row as they consigned struggling Leicester City to a fifth consecutive defeat.
9Buoyed by their Champions League win over Borussia Dortmund, Chelsea started brightly and Ben Chilwell volleyed in from a tight angle against his old club.
10Chelsea's Joao Felix and Leicester's Kiernan Dewsbury-Hall hit the woodwork in the space of two minutes, then Felix had a goal ruled out by the video assistant referee for offside.
11Patson Daka rifled home an excellent equaliser after Ricardo Pereira won the ball off the dawdling Felix outside the box.
12But Kai Havertz pounced six minutes into first-half injury time with an excellent dinked finish from Enzo Fernandez's clever aerial ball.
13Mykhailo Mudryk thought he had his first goal for the Blues after the break but his effort was disallowed for offside.
14Mateo Kovacic sealed the win as he volleyed in from Mudryk's header.
15The sliding Foxes, who ended with 10 men following Wout Faes' late dismissal for a second booking, now just sit one point outside the relegation zone.
16""".replace('\n', ' ')
17>>> question = "Who had a goal ruled out for offside?"
18>>> options = ['Ricardo Pereira', 'Ben Chilwell', 'Joao Felix', 'The Foxes']
19
20>>> inputs = prepare_answering_input(
21 tokenizer=tokenizer, question=question,
22 options=options, context=context,
23 )
24>>> outputs = model(**inputs)
25>>> prob = torch.softmax(outputs.logits, dim=-1)[0].tolist()
26>>> selected_answer = options[np.argmax(prob)]
27
28>>> print(prob)
29[0.00145158, 0.00460851, 0.99049687, 0.00344302]
30>>> print(selected_answer)
31Joao Felix1def prepare_answering_input(
2 tokenizer, # longformer_tokenizer
3 question, # str
4 options, # List[str]
5 context, # str
6 max_seq_length=4096,
7 ):
8 c_plus_q = context + ' ' + tokenizer.bos_token + ' ' + question
9 c_plus_q_4 = [c_plus_q] * len(options)
10 tokenized_examples = tokenizer(
11 c_plus_q_4, options,
12 max_length=max_seq_length,
13 padding="longest",
14 truncation=True,
15 return_tensors="pt",
16 )
17 input_ids = tokenized_examples['input_ids'].unsqueeze(0)
18 attention_mask = tokenized_examples['attention_mask'].unsqueeze(0)
19 example_encoded = {
20 "input_ids": input_ids,
21 "attention_mask": attention_mask,
22 }
23 return example_encodedContext ---> Question + Answer:1@article{manakul2023mqag,
2 title={MQAG: Multiple-choice Question Answering and Generation for Assessing Information Consistency in Summarization},
3 author={Manakul, Potsawee and Liusie, Adian and Gales, Mark JF},
4 journal={arXiv preprint arXiv:2301.12307},
5 year={2023}
6}