Tensorflow: “BERT, or Bidirectional Encoder Representations from Transformers, is a method of pre-training language representations which obtains state-of-the-art results on a wide array of Natural Language Processing tasks.
This app uses a compressed version of BERT, MobileBERT, that runs 4x faster and has a 4x smaller model size.
SQuAD, or Stanford Question Answering Dataset, is a reading comprehension dataset consisting of articles from Wikipedia and a set of question-answer pairs for each article.
The model takes a passage and a question as input then returns a segment of the passage that most likely answers the question. It requires semi-complex pre-processing including tokenization and post-processing steps that are described in the BERT paper and implemented in the sample app.”
Tensorflow.js grow its popularity in recent times, due to enabling you to do Deep Learning in the browser. Paired up with Node.js it paves Deep Learning’s way into modern Web Applications development, Desktop Application development, etc.
Building a Natural Language Processing Question Answering System
The first thing you do is create a Node.js project. If you don’t know how to do, check our previous article here, where we explain it thoroughly.
Next thing is to create a .html file, we’ve named it questionanswering.html, and a .js file, we’ve named it nlpqna.js.
After you’ve done this, add the following code in your .html file.
Next, we add the following code into our .js file. This code loads the model.
After we’ve loaded the model, the next move is to try to ask question and see the answer we get.
To do that we need to use the findAnswers(question: string, passage: string) function, that takes two arguments, the first one is the question which as the name suggest is the question and the second is the passage which is the content from where we extract our answers.
To do that, add the following lines, to your .js file.
The last line in the code below just adds an event listener that will fire up our function answer_questions(), every time we make a GET request to our page.
Now, to test our work, we need to open the .html file in the browser, and open our console (inspect element) and see what we get back.
It might get some time (like 1 minute or less. You can print some messages using console.log() to be sure that your code works properly), but here are the answers:
So, here is how you can build a Natural Language Processing (NLP) question answering system in less than 20 lines of code using Tensorflow.js, based on Stanford University’s dataset. If you think it’s easy and you want to give it a shot you can find the project on our Github repository.
The inspiration for this post can be found on TensorFlow’s Github page. You will notice that we have a different approach to the solution since working with Node.js it’s a bit tricky and the given solution was not quite working and can be confusing for beginners.
Check our older articles below, you might find them helpful.
- FREE Computer Science Curriculum From The Best Universities and Companies In The World
- How To Become a Certified Data Scientist at Harvard University for FREE
- How to Gain a Computer Science Education from MIT University for FREE
- Top 50 FREE Artificial Intelligence, Computer Science, Engineering and Programming Courses from the Ivy League Universities
- Top 10 Best FREE Artificial Intelligence Courses from Harvard, MIT, and Stanford
- Top 10 Best Artificial Intelligence YouTube Channels in 2020
Like with every post we do, we encourage you to continue learning, trying and creating.