# Handling long text in BERT for Question Answering

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhandling-long-text-in-bert-for-question-answering%2F382%3E%5C
**Category:** Beginners\
**Created:** [July 19, 2020, 10:52am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhandling-long-text-in-bert-for-question-answering%2F382 "2020-07-19T10:52:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![benj](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@benj](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fbenj)%5C
**Post date:** [July 19, 2020, 10:52am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhandling-long-text-in-bert-for-question-answering%2F382%2F1 "2020-07-19T10:52:28Z")

</div>

I’ve read post which explains how the sliding window works but I cannot find any information on how it is actually implemented.

From what I understand if the input are too long, sliding window can be used to process the text.

Please correct me if I am wrong.  
Say I have a text _ **"In June 2017 Kaggle announced that it passed 1 million registered users"** _.

Given some `stride` and `max_len`, the input can be split into chunks with over lapping words (not considering padding).

```auto
In June 2017 Kaggle announced that # chunk 1
announced that it passed 1 million # chunk 2
1 million registered users # chunk 3

```

If my questions were _ **"when did Kaggle make the announcement"** _ and _ **"how many registered users"** _ I can use `chunk 1` and `chunk 3` and **not use** `chunk 2` **at all** in the model. Not quiet sure if I should still use `chunk 2` to train the model

So the input will be:  
`[CLS]when did Kaggle make the announcement[SEP]In June 2017 Kaggle announced that[SEP]`  
and  
`[CLS]how many registered users[SEP]1 million registered users[SEP]`

* * *

Then if I have a question with no answers do I feed it into the model with all chunks like and indicate the starting and ending index as **-1**? For example _ **"can pigs fly?"** _

`[CLS]can pigs fly[SEP]In June 2017 Kaggle announced that[SEP]`

`[CLS]can pigs fly[SEP]announced that it passed 1 million[SEP]`

`[CLS]can pigs fly[SEP]1 million registered users[SEP]`

---

<div class="post-metadata">

**Author:** ![lewtun](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lewtun/32/30995_2.png) [@lewtun](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flewtun)%5C
**Post date:** [January 22, 2021, 7:50pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhandling-long-text-in-bert-for-question-answering%2F382%2F2 "2021-01-22T19:50:44Z")

</div>

Hi @benj, Sylvain has a nice tutorial ([link](https://github.com/huggingface/notebooks/blob/master/examples/question_answering.ipynb)) on question answering that provides a lot of detail on how the sliding window approach is implemented.

The short answer is that all chunks are used to train the model, which is why there is a fairly complex amount of post-processing required to combine everything back together.

---

<div class="post-metadata">

**Author:** ![rohit11](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@rohit11](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Frohit11)%5C
**Post date:** [April 29, 2021, 7:57am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhandling-long-text-in-bert-for-question-answering%2F382%2F3 "2021-04-29T07:57:19Z")

</div>

sir , I saw the article ,can you please tell after fine tuning the model , how to use it for question answering ?

---

<div class="post-metadata">

**Author:** ![lewtun](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lewtun/32/30995_2.png) [@lewtun](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flewtun)%5C
**Post date:** [April 30, 2021, 7:12pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhandling-long-text-in-bert-for-question-answering%2F382%2F4 "2021-04-30T19:12:05Z")

</div>

hey @rohit11, i think the simplest approach would be to load the fine-tuned model and tokenizer in a question-answering pipeline as follows:

```auto
from transformers import pipeline, AutoTokenizer, AutoModelForQuestionAnswering

model_checkpoint = "/path/to/your/model"
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
model = AutoModelForQuestionAnswering.from_pretrained(model_checkpoint)
pipeline('question-answering', model=model, tokenizer=tokenizer)

```

you can consult the [docs](https://huggingface.co/transformers/main_classes/pipelines.html?highlight=pipeline#transformers.pipeline) for more information on the `pipeline` 🙂

---

<div class="post-metadata">

**Author:** ![Sam2021](https://avatars.discourse-cdn.com/v4/letter/s/a8b319/32.png) [@Sam2021](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FSam2021)%5C
**Post date:** [August 13, 2021, 4:33pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhandling-long-text-in-bert-for-question-answering%2F382%2F5 "2021-08-13T16:33:52Z")

</div>

Hi

When handling long context by breaking it into chunk where each chunk can be seprate example , is it likely that multiple chunks can have the answer independently or the answer can lie in an overlapping fashion spanning two chunks ?

---

<div class="post-metadata">

**Author:** ![benj](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@benj](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fbenj)%5C
**Post date:** [August 24, 2021, 7:26am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhandling-long-text-in-bert-for-question-answering%2F382%2F6 "2021-08-24T07:26:31Z")

</div>

You would want to find the _“best span”_. Check Google’s implementation of Squad [here](https://github.com/google-research/bert/blob/eedf5716ce1268e56f0a50264a88cafad334ac61/run_squad.py#L513).

---

<div class="post-metadata">

**Author:** ![dheeraja486](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/dheeraja486/32/2217_2.png) [@dheeraja486](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fdheeraja486)%5C
**Post date:** [January 19, 2022, 2:37pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhandling-long-text-in-bert-for-question-answering%2F382%2F7 "2022-01-19T14:37:54Z")

</div>

@Sam2021 As you can see in the notebook, they are finding answers in each chucks with modified start and end token. Also that’s why they are using offset tokens which maps tokens to original text.  
Answers do not get divided into two chucks that’s why they are using doc\_stride which basically mitigate this problem

---

<div class="post-metadata">

**Author:** ![yasminebe](https://avatars.discourse-cdn.com/v4/letter/y/51bf81/32.png) [@yasminebe](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fyasminebe)%5C
**Post date:** [March 10, 2022, 9:24am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhandling-long-text-in-bert-for-question-answering%2F382%2F8 "2022-03-10T09:24:19Z")

</div>

Hello, I am having an issue with the same situation. I have long paragraphs from which i want to extract the answer. I split the paragraphs into chunks and used the (start\_end) with the highest probabilities in each chunk (for each paragraph) . I saw the notebook and the solution of using the n\_best (start\_end) and check the score of all the combinations is interesting. But I wonder if there is a more elegant way to decide about which (start\_end) token to choose among the chunk.  
Is there an official implementation of this problem?  
the best span function implemented in the official code doesn’t really solve the problem of having start index in chunk 2 and the end index in chunk 9 for example. It just solves the problem of picking the righ sub-paragraph.  
Thanks in advance.
