AI Logic

Glossary

Context Length

Updated:

A diagram comparing context lengths. On both sides, the model predicts the token that follows the same sentence, "I have a cat . Every morning the little". On the left, the context length is 3: only the last three tokens are inside the frame, and the earlier tokens are not visible to the model, so it cannot tell what the next token is. On the right, the context length is 9: the whole sentence is inside the frame, and because the model can see the earlier "cat", it can predict "cat" as the next token

Explanation

Context length is the number of words or tokens in the input data (especially text) that a model takes into account in natural language processing and deep learning. The longer the context length, the more contextual information the model can use, which is important for understanding the meaning of a sentence. In particular, context length is a key factor in contextual understanding in sequence models and Transformers. Transformers use the self-attention mechanism to process long contexts efficiently and have become popular across a wide range of NLP tasks. Traditional sequence models (e.g., RNNs and LSTM) handle fixed-length sequences, so they can struggle to capture long-range dependencies, and the context length needs to be set appropriately.

Where It Is Used

Context length plays an important role in many natural language processing tasks. In machine translation, an appropriate context length is needed to retain the context of the source sentence and translate it accurately into the target sentence. In text generation, when GPT-family models generate the next token based on past tokens, the context length strongly affects the accuracy of the generation. In sentiment analysis, an appropriate context length is needed to take into account the context of reviews, comments and similar text. In question answering, maintaining a sufficient context length to extract information relevant to the question from documents enables more accurate answers. Finally, in document classification, taking the context of the entire text into account affects accuracy, so setting the context length is important.

FAQ

Q. What happens if the context length is short?
A.

The context the model can refer to is limited, making it harder to capture long-range dependencies and the flow of the text. As a result, the accuracy of the output may drop.

Q. How do Transformers handle it?
A.

Because they use the self-attention mechanism to process the relationships among all tokens at once, they can capture semantic relationships efficiently even in long contexts.

Q. For which tasks is it especially important?
A.

It is an important parameter for retaining context in many natural language processing tasks, including machine translation, text generation, question answering, document classification and sentiment analysis.