DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Large Language Models: A Comprehensive Analysis of Real-World CX Applications
  • Building an Intelligent QA System With NLP and Milvus
  • A Complete Guide to Modern AI Developer Tools
  • Getting Started With GenAI on BigQuery: A Step-by-Step Guide

Trending

  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces
  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. How To Create a Question-Answering Model From Scratch

How To Create a Question-Answering Model From Scratch

Building your question-answering model might seem like it could be more manageable. This tutorial will walk you through step by step to get it done.

By 
Joseph owino user avatar
Joseph owino
·
May. 10, 23 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
5.9K Views

Join the DZone community and get the full member experience.

Join For Free

Building your question-answering model might seem like it could be more manageable. This tutorial will walk you through step by step to get it done.

Question and Answering is a natural language processing (NLP) technique used to answer questions based on data given or text. Question Answering models aim to develop a system that automatically understands questions passed in natural language and provides accurate or relevant answers. Question answering is widely used in various disciplines, such as Chatbots and virtual assistants, Education and E-Learning, information retrieval, and search engines.

Setting Development Environment

Before continuing to this tutorial, you need to have some knowledge of Python to follow quickly through this setup and development process. Navigate to Google Colab or activate your Jupyter Notebook. Create a new Notebook. Then execute the following commands to install the required libraries in your development environment.

Python
 
!pip install transformers


You will use transformers to provide pre-trained models and tools for working with state-of-the-art NLP models.

Importing Necessary Libraries

Import the required libraries that you will use to preprocess the data and create the model.

Python
 
import tensorflow as tf
from transformers import pipeline
from transformers import BertForQuestionAnswering


Imported libraries will be used later in the code to develop the model.

Define Context and List of Questions Based on Context

Define a context string that contains the information you want to ask questions about. Here we have chosen a storyline about mountain formation.

Python
 
question_answerer =pipeline("question-answering")
context = "A mountain is an elevated portion of the Earth's crust, generall


Next, create a list of questions you want to ask about the given context.

Python
 
questions = [
"What is a mountain?",
"What is a plateau?"
]


Encode the first question using the tokenizer’s encode method, specifying truncation and padding. Encoding helps improve the model’s consistency, memory efficiency, and training performance.

Python
 
tokenizer.encode(questions[0], truncation=True, padding=True)


Create a question-answering pipeline using the BERT-based model and tokenizer.

Python
 
question_answerer=pipeline('question-answering', model=model, tokenizer=tok
question_answerer({
'question':questions,
'context' : context;
})


Load the Pre-Trained BERT-Based Model and Tokenizer

BERT (Bidirectional Encoder Representations from Transformers) mostly used language representation models in NLP tasks and questions answering. Loading pre-trained BERT-based models increases the chances of the model understanding language and its capabilities for question answering. Tokenization will divide the input text into meaningful units for the model to process.

Combining BERT and tokenizer increases your model’s performance and accurate result.

Python
 
from transformers import AutoTokenizer
model = BertForQuestionAnswering.from_pretrained("deepset/bert-base-cased-s
tokenizer = AutoTokenizer.from_pretrained("deepset/bert-base-cased-squad2")


Obtaining Answers

Now that you have got context and the pre-trained model loaded, you need to pass access to the answers by assigning the answers to a dictionary and printing the score together with the answer. Like in the above, we have two questions and will assign each question by its index. The model will print the answers and the score in each.

Python
 
question_answerer({
'question': questions[0],
'context': context
})
question_answerer({
'question': questions[1],
'context': context
})


Wow! Just simply, you have your question-answering model using Hugging Face Transformers.

Conclusion

Following this tutorial, you have a basic QA model using the Hugging Face Transformers library. By following this tutorial, you can quickly adapt the code to work with different contexts and questions, allowing you to create your question-answering model for various contexts.

Remember that the example code provided is just a starting point, and you can explore and customize it further based on your specific needs and requirements. Have fun experimenting and building more advanced question-answering models!

Complete code available on GITHUB REPO

NLP Question answering

Opinions expressed by DZone contributors are their own.

Related

  • Large Language Models: A Comprehensive Analysis of Real-World CX Applications
  • Building an Intelligent QA System With NLP and Milvus
  • A Complete Guide to Modern AI Developer Tools
  • Getting Started With GenAI on BigQuery: A Step-by-Step Guide

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!