Make a Chat Bot with TensorFlow NLP and Anaconda Navigator..

Mathan Jeya
Voice Tech Podcast
Published in
6 min readApr 3, 2019

Keep On Practicing guys Python looks easy….

Nowadays, there is great demand of Machine learning and Artificial Intelligence Engineers. If you are looking for a good job then you must have great learning and great understanding of Machine Learning and Artificial Intelligence algorithms. You should also work with few good projects which will give you an end-to-end experience of logic that we use in Machine learning. Today, I am going to share with you the implementation of ChatBot which you can use for your personal question and answer play.

So let’s start building ChatBot using movies dialogues dataset. Follow each and every steps to get the desired output.

Installation:

For windows user please install Anaconda using following link.

For Mac user please follow this link.

For Linux user please follow this link.

You can easily install anaconda and after that we will create a virtual environment for our ChatBot. After the installation of anaconda open Anaconda prompt (Windows user) and Terminal(Mac & Linux user) and then type the following command-

conda create -n chatbot python=3.5 anaconda

Click on enter button and then let the installation start. After the installation of this virtual environment write a command-

conda activate chatbot

Press enter after writing the above command and then you will enter in your virtual environment. After you enter in your virtual environment now you have to install TensorFlow in your environment. So write the following command to install TensorFlow-

pip install tensorflow==0.12.1

After the installation of tensorflow in your virtual environment you are ready to go and let’s start creating our chatbot. You can download the dataset from the following link.

Import these libraries-

Data Preprocessing-

This involves a series of steps to preprocess the imported dataset to the required format needed for it to work with the Seq2seq model in Tensorflow.

Creating dictionary so that mapping of each line and id can be done.

If the length of list in the dictionary is even, the first conversation is taken as the question and the second one as the answer. This is repeated for its entire length. For odd number of conversations, the last one is neglected for convenience.

This function converts all characters to lower-cases and converts commonly used short texts to their full version. Now lets pass our questions and answers through this function.

This code is self explanatory. It removes very short and very long questions. Makes it easier for our model to train.

This is to get the frequency of each word in our dataset. Will help in filtering out words that are rare (usually names,nouns that don’t help the model learn). The less frequent words are removed with the below code.

These tokens help the model understand the start and the end of a sentence, rare words etc.

The answersints2word helps form a sentence when the model predicts an output with integers.

Helps the model understand the end of each answer in the model.

Converts every single word in questions and answers to integers and assigns the ‘OUT’ tag for the words that were filtered out in one of the above steps.

Sorts questions and answers by their length. From 1 to 25.

Build better voice apps. Get more articles & interviews from voice technology experts at voicetechpodcast.com

Building the seq2seq model -

A seq2seq model is a type of many-to-many RNN model. They are most commonly used for chatbots and translation models. It has 2 components, the encoder and the decoder.

A placeholder is simply a variable that we will assign data to at a later date. It allows us to create our operations and build our computation graph, without needing the data.

Assigns SOS tag to the start of every target(answers).

The Decoder RNN is built after decoding the training and test sets using the encoder state obtained from the above function.

Now that we have both the encoder RNN and the decoder RNN we’ll use them to build our seq2seq model.

Training the seq2seq model-

We have all our functions defined and are getting closer to training the model. Let’s setup the hyperparameters needed.

This is totally modifiable depending on your system spec. 25 epochs is usually too small for a building a good chatbot. You can choose to increase it if you want better results.

Starting a Tensorflow session and defining few other parameter needed for training.

Defining the predictions, which we know is the output of the seq2seq function defined above.

Like any Deep Learning model, we have to define the loss error and optimizer. And gradient clipping is done to prevent exploding or vanishing of errors in the earlier layers of the neural network.

Padding are dummy values added to the sequence making the questions and answers to have the same length.

The RNN model accepts inputs only in batches.

Splitting the data to training set and test set. The model trains of 85% the data and tests its prediction on the 15% data to compute the loss error and improve in further epochs.

Testing the seq2seq model-

During training, the weights are saved at every checkpoint. This can be loaded to chat with the chatbot.

This function converts our input question to integers. Our model was trained with integers and not words.

Setting up our chatbot-

Interacting with our chatbot-

--

--

Mathan Jeya
Voice Tech Podcast

Software Engineer in Web and Mobile Applications. Likes to write about tech, coding, investing, finance & economics. Believes in adding value to people’s lives.