0
0
NLPml~5 mins

Answer span extraction in NLP

Choose your learning style9 modes available
Introduction

Answer span extraction helps find the exact part of a text that answers a question. It makes machines understand and pick the right piece of information quickly.

When building a chatbot that answers questions from a document.
When creating a search engine that shows exact answers, not just links.
When summarizing long articles by highlighting key answers.
When helping users find specific facts in manuals or guides.
Syntax
NLP
start_logits, end_logits = model(input_ids)
start_index = start_logits.argmax()
end_index = end_logits.argmax()
answer_span = input_ids[start_index : end_index + 1]

start_logits and end_logits are scores for each word position showing where the answer might start and end.

The argmax() function picks the position with the highest score.

Examples
This example shows how to find the start and end positions from scores.
NLP
start_logits = torch.tensor([0.1, 0.2, 3.0, 0.5])
end_logits = torch.tensor([0.1, 0.3, 0.4, 2.5])
start_index = start_logits.argmax()  # 2
end_index = end_logits.argmax()      # 3
Extract tokens from input and convert them back to readable text.
NLP
answer_tokens = input_ids[start_index : end_index + 1]
answer_text = tokenizer.decode(answer_tokens)
Sample Model

This code uses a pre-trained model to find the answer span in the context for the question. It prints the exact answer text.

NLP
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
import torch

# Load model and tokenizer
model_name = 'distilbert-base-uncased-distilled-squad'
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForQuestionAnswering.from_pretrained(model_name)

# Sample context and question
context = "The Eiffel Tower is located in Paris. It is a famous landmark."
question = "Where is the Eiffel Tower located?"

# Encode inputs
inputs = tokenizer(question, context, return_tensors='pt')

# Get model outputs
outputs = model(**inputs)
start_logits = outputs.start_logits
end_logits = outputs.end_logits

# Find start and end positions
start_index = torch.argmax(start_logits)
end_index = torch.argmax(end_logits)

# Extract answer tokens and decode
answer_tokens = inputs['input_ids'][0][start_index : end_index + 1]
answer = tokenizer.decode(answer_tokens, skip_special_tokens=True)

print(f"Answer: {answer}")
OutputSuccess
Important Notes

The model predicts scores for each word position to find the answer start and end.

Sometimes the predicted end position can be before the start; in practice, you may add checks to handle this.

Using a tokenizer helps convert text to tokens and back, making extraction easier.

Summary

Answer span extraction finds the exact part of text answering a question.

It uses model scores to pick start and end positions in the text.

This helps build smart question-answering systems that give precise answers.