Views
No views yet
longformer-base-4096 is a BERT-like model started from the RoBERTa checkpoint and pretrained for MLM on long documents. It supports sequences of length up to 4,096.squad_v2 from HuggingFace/Datasets| Dataset | Split | # samples |
|---|---|---|
| squad_v2 | train | 130319 |
| squad_v2 | valid | 11873 |
1!pip install datasets
2from datasets import load_dataset
3dataset = load_dataset('squad_v2')1import torch
2from transformers import AutoTokenizer, AutoModelForQuestionAnswering
3ckpt = "mrm8488/longformer-base-4096-finetuned-squadv2"
4tokenizer = AutoTokenizer.from_pretrained(ckpt)
5model = AutoModelForQuestionAnswering.from_pretrained(ckpt)
6
7text = "Huggingface has democratized NLP. Huge thanks to Huggingface for this."
8question = "What has Huggingface done ?"
9encoding = tokenizer(question, text, return_tensors="pt")
10input_ids = encoding["input_ids"]
11
12# default is local attention everywhere
13# the forward method will automatically set global attention on question tokens
14attention_mask = encoding["attention_mask"]
15
16start_scores, end_scores = model(input_ids, attention_mask=attention_mask)
17all_tokens = tokenizer.convert_ids_to_tokens(input_ids[0].tolist())
18
19answer_tokens = all_tokens[torch.argmax(start_scores) :torch.argmax(end_scores)+1]
20answer = tokenizer.decode(tokenizer.convert_tokens_to_ids(answer_tokens))
21
22# output => democratized NLPpipleine1from transformers import AutoTokenizer, AutoModelForQuestionAnswering, pipeline
2
3ckpt = "mrm8488/longformer-base-4096-finetuned-squadv2"
4tokenizer = AutoTokenizer.from_pretrained(ckpt)
5model = AutoModelForQuestionAnswering.from_pretrained(ckpt)
6
7qa = pipeline("question-answering", model=model, tokenizer=tokenizer)
8
9text = "Huggingface has democratized NLP. Huge thanks to Huggingface for this."
10question = "What has Huggingface done?"
11
12qa({"question": question, "context": text})<s>Created by Manuel Romero/@mrm8488 | LinkedIn
Made with ♥ in Spain