Intent Based Chatbot using Tensorflow

Bhatnagarsarthak
4 min readJun 14, 2021

--

image credit:- Google

In todays time where manual work has remained in very few sector of the industry. The chatbot have came to be a solution to handle basic QnA of a user. training these intent based chat bot is very simple as we will see below but before we start few things you might know are Neural Networks (we are not using complex models just simple dense net), NLTK(Library for NLP) and thats it and also python3 syntax understand.

So, before starting to talk about a how to implement lets talk about what is intent. Intent is nothing but the task that bot will be trying to accomplish for example if used give input “How many degrees outside” than our bot should reply with the exact temperature at that particular place and also to give appropriate reply to the user. So question arise how the bot will understand that it have to reply temperature. Its simple our bot will be trained on some random questions and the tag or intent they belong and after some epochs our bot will be trained enough to give output or intent. Now lets start by seeing our data

So you see our intent file its in JSON form this format is best to systematically process data since each object is in parent and child form making easy to loop through each form

As you can see intents is nothing but list of dictionaries that have form of tag, pattern, response and context set. so lets understand what does each means. TAG it is the value that we are actually trying to predict using the random data that out model can get this is accomplished by using PATTERNS this is the list of demo patterns that our model can get from user and we try to train model so that it can predict its tag from the key word that may be present in the user input. RESPONSE contains all the responses that our model will give when it is successful to predict the tag we have multiple response so we choose it randomly so it feels that model is giving random response each time

Now lets talk about Loading and preprocess code

So lets start talking about imports first we have nltk if you don’t have it then you can install it via pip

pip install nltk

Then we have tensorflow its bit tricky to download since it supports GPU for enhanced and parallel training the requirements are bit more you can go thoroughly for full installing guide on official tensorflow website for GPU support https://www.tensorflow.org/install/gpu

Now we have our setup ready lets talk about code

first we are importing the intent file and initializing 4 blank list 1)words for all the words in the pattern or the questions that will be asked 2)doc_x will be transformed into bag_of_words data 3)doc_y contains all the tags for the corresponding bag_of_word 4)Labels it contains the the target tag for words in the words list.

Then we are stemming our words in word list so that all words can be transformed and retransformed to its root word working with root word is more easier than working with the words with tense.

Now we will be looping through each word while tokenizing and also adding labels we are using one hot encoding method that means it will be in the format of 0 and 1. We are using it because it is easy to use and not vectorizing the words since this is simple data after doing this we are just appending them to training and output list

Now comes the most intresting part i.e Designing our NN model we will be using the Dense(Linear layer) since our data is in shape (None,len(training[0]),) Units of each layer is randomly taken and then optimize since it is hyperparameter and there is no fix pattern to choose it

I have not made deep network and did not use any filter layer just using two fully connected layer and one output layer. Fully connected layer activation is ‘ReLu’ and as usual Output layer have ‘Softmax’ activation.

Now comes to Optimizers I have chosen good old friend Stochastic Gradient Descent and have chosen custom hyperparameter (Note:- if you are using tensorflow 2.4.1 or above the learning rate abbreviation (lr) has been depreciated) loss is categorical crossentropy because our model is multi classification model since we are predicting many different tags. Now its only time left is to call fit method on our training data.

Since Now we have trained our model now lets make some prediction using this model. I am not going to explain line by line code for using this model

Above is a sample code for execution and a snip of terminal. Now lets talk about its application majorly it can be used as an QnA bot but it can also be used as Voice assistant got intrested don’t take tension I am working on it with all time favorites flutter SDK for mobile compatibility codes used here are on my GitHub account https://github.com/sarthak7509/Intent-chat-bot

If you like this overview on chatbot with Tensorflow then please like this post

--

--

Bhatnagarsarthak

A person with interest and expertise in machine learning and android development