Training the Chatbot model
The first thing we’re going to do is to train the Chatbot model. In order to do that, create a file named ‘intense.json’ in which we’ll write all the intents, tags and words or phrases our Chatbot would be responding to.
Step 1: Create a “intense.json”:
Here we’ve added just three tags just to show how it works. You can add a lot of them!
Step 2: Create a “training.py”:
The next step is training our model. We’ll use a class called WordNetLemmatizer() which will give the root words of the words that the Chatbot can recognize. For example, for hunting, hunter, hunts and hunted, the lemmatize function of the WordNetLemmatizer() class will give “hunt” because it is the root word.
- Create a WordNetLemmatizer() class object.
- Read the contents from the “intense.json” file and store it to a variable “intents”. Next, initialize empty lists to store the contents.
- Next up, we have a function called word_tokenize(para). It takes a sentence as a parameter and then returns a list containing all the words of the sentence as strings. Here we’re tokenizing the patterns and then appending them to a list ‘words’. So, at last, this list ‘words’ would have all the words that are in the ‘patterns’ list.
- In documents, we have all the patterns with their tags in the form of a tuple.
- Now, using a list comprehension, we’ll modify the list ‘words’ we created above and store the words’ ‘lemma’ or simply put, the root words.
- Dump the data of the ‘words’ and ‘classes’ to binary files of the same name, using the pickle module’s dump() function.
Python3
# importing the required modules. import random import json import pickle import numpy as np import nltk from keras.models import Sequential from nltk.stem import WordNetLemmatizer from keras.layers import Dense, Activation, Dropout from keras.optimizers import SGD lemmatizer = WordNetLemmatizer() # reading the json.intense file intents = json.loads( open ( "intense.json" ).read()) # creating empty lists to store data words = [] classes = [] documents = [] ignore_letters = [ "?" , "!" , "." , "," ] for intent in intents[ 'intents' ]: for pattern in intent[ 'patterns' ]: # separating words from patterns word_list = nltk.word_tokenize(pattern) words.extend(word_list) # and adding them to words list # associating patterns with respective tags documents.append(((word_list), intent[ 'tag' ])) # appending the tags to the class list if intent[ 'tag' ] not in classes: classes.append(intent[ 'tag' ]) # storing the root words or lemma words = [lemmatizer.lemmatize(word) for word in words if word not in ignore_letters] words = sorted ( set (words)) # saving the words and classes list to binary files pickle.dump(words, open ( 'words.pkl' , 'wb' )) pickle.dump(classes, open ( 'classes.pkl' , 'wb' )) |
Step 3: Now we need to classify our data into 0’s and 1’s because neural networks works with numerical values, not strings or anything else.
- Create an empty list called training, in which we’ll store the data used for training. Also create an output_empty list that will store as many 0’s as there are classes in the intense.json.
- Next up we’ll create a bag that will store the 0’s and 1’s. (0, if the word isn’t in the pattern and 1 if the word is in the pattern). To do that, we’ll iterate through the documents list and append 1 to the ‘bag’ if it is not in the patterns, 0 otherwise.
- Now shuffle this training set and make it a numpy array.
- Split the training set consisting of 1’s and 0’s into two parts, that is train_x and train_y.
Python3
# we need numerical values of the # words because a neural network # needs numerical values to work with training = [] output_empty = [ 0 ] * len (classes) for document in documents: bag = [] word_patterns = document[ 0 ] word_patterns = [lemmatizer.lemmatize( word.lower()) for word in word_patterns] for word in words: bag.append( 1 ) if word in word_patterns else bag.append( 0 ) # making a copy of the output_empty output_row = list (output_empty) output_row[classes.index(document[ 1 ])] = 1 training.append([bag, output_row]) random.shuffle(training) training = np.array(training) # splitting the data train_x = list (training[:, 0 ]) train_y = list (training[:, 1 ]) |
Step 4: We’ve come to the model-building part of our Chatbot model. Here, we’re going to deploy a Sequential model, that we’ll train on the dataset we prepared above.
- Add(): This function is used to add layers in a neural network.
- Dropout(): This function is used to avoid overfitting
Python3
# creating a Sequential machine learning model model = Sequential() model.add(Dense( 128 , input_shape = ( len (train_x[ 0 ]), ), activation = 'relu' )) model.add(Dropout( 0.5 )) model.add(Dense( 64 , activation = 'relu' )) model.add(Dropout( 0.5 )) model.add(Dense( len (train_y[ 0 ]), activation = 'softmax' )) # compiling the model sgd = SGD(lr = 0.01 , decay = 1e - 6 , momentum = 0.9 , nesterov = True ) model. compile (loss = 'categorical_crossentropy' , optimizer = sgd, metrics = [ 'accuracy' ]) hist = model.fit(np.array(train_x), np.array(train_y), epochs = 200 , batch_size = 5 , verbose = 1 ) # saving the model model.save( "chatbotmodel.h5" , hist) # print statement to show the # successful training of the Chatbot model print ( "Yay!" ) |
Output:
Deploy a Chatbot using TensorFlow in Python
In this article, you’ll learn how to deploy a Chatbot using Tensorflow. A Chatbot is basically a bot (a program) that talks and responds to various questions just like a human would. We’ll be using a number of Python modules to do this.
This article is divided into two sections:
First, we’ll train the Chatbot model, and then in section two, we’ll learn how to make it work and respond to various inputs by the user.