Python Chat Bot Tutorial - Chatbot with Deep Learning (Part 3)

Tech With Tim · Beginner ·🧠 Large Language Models ·7y ago

Key Takeaways

This video tutorial demonstrates how to create a deep learning chatbot using Python, TensorFlow, and TFlearn, covering topics such as neural network architecture, model training, and prediction.

Full Transcript

hey guys and welcome back to part three of AI chat bots in Python now up until this point all we've been doing is pre-processing our data and we have a few small things we need to do before we can move on from that but after that what we're gonna do is actually talk about the model that we're gonna use to make predictions based on a string of text and how that kind of works and just talk about that and draw some things I don't understand that as well as coded here now the first thing we need to do just a few minor errors here always run into typos and stuff when I'm doing this and I realize later but up here where it says Doc's X dot append pattern we need to change that to words and that is because we want to append the tokenized words I thought when we do this creating bag of words it actually works we also need to change this here to be labels because before that was classes and I don't have I don't have a list called classes so this needs to be labels I don't know why I called it classes and then the last thing is for these unique words here for W in words what we're actually gonna do is we're gonna say just add an if statement at the end of this just to remove any question marks because question marks are is a pretty common thing that people could type and we don't actually want that to have any meaning to the model so we're just gonna remove it so to do that we're gonna say if W not in and in this case question mark now we can do not in question mark where we just say W does not equal question mark doesn't really matter it's the same thing so we'll do does not equal question mark so those are the few fixes we need to do and now what we're gonna do actually is just change our output and our training into numpy arrays and that's because that is the form that needs to be taken by our model so we're just gonna say training equals an umpire dot array and in this case we're gonna say training and then we can just copy the same thing here and we'll do output equals numpy dot array and in this case we will say output now what this is gonna do is just take these arrays and change them or take these lists and change them into arrays so that we can feed them to our model so now that we've done that what we're gonna do is actually start building our model using T learn now this is very similar to tensorflow so if you've done tensorflow you understand how this works but I'm gonna code the model out and then we're going to kind of draw it and visualize what it actually looks like and understand how it's gonna work on classifying our data so we're gonna say TF which actually we have to type tensorflow because I didn't do import as - yep we'll do it tensor flowed reset underscore default underscore graph now we're just doing this to make sure that we get rid of all like previous settings and stuff it's resetting the underlying data graph or graph data you don't really have to understand what that means we're gonna say net equals in this case TF learned dot input underscore data and this K Swedish it's a shape equals and then none and the length of our training zero now what this is gonna do is define the input shape that we're expecting for our model so in this case we're getting the length of training zero because each training input is gonna be the same length so by doing this we're saying okay well the model should expect us to have an array of length forty five or however many words that we have right now next what we're gonna do is say next equals TF learn dot fully underscore connect it it's a lot of typing but it's we won't have to do that much I'm gonna say net eight and what this means is we're gonna say where you add this fully connected layer to our neural network which starts at this input data and we're gonna have eight neurons for that hidden layer or this first yeah I guess hidden layer now after this we're just gonna copy this again because we're gonna have another hidden layer that has eight neurons as well and then finally we need two more layers so we're gonna do TF learned up fully connected in this case we're gonna do net but it's gonna be our output layer so it's gonna be the length of output 0 and then what we're gonna say here is activation equals softmax now what this is gonna do essentially is allow us to get probabilities for each output and we'll talk about this morning when I draw the model but essentially softmax is gonna go through and give us a probability for each neuron in this lair and that will be our output for the network now after that we're just gonna add next equals TF learned regression regression and we're going to apply that to network now to train our model what we're gonna do is you say model equals in this case TF learned DN n prints a net and that's all we need to do now this is actually our complete model that's the whole a I kind of aspect of this I know it seems really short but let's go through exactly what I just typed and what this is now essentially we should start with an input data which is the length of our training data that we have two hidden layers with eight neurons fully connected I also connected to an output layer that has neurons representing each of our classes so let me actually bring out my drawing time but quickly and show you a little picture of this maybe make it a bit more clear on what's going on here so give me one second just to get this set up here and let's load up our little drawing thing if I can get this going here okay so what we have right now is we have a neural network and I'm just gonna draw it out for us we can have a look at exactly what it is like now we start with a a bunch of input neurons which are the length of our input data which means however many words we have because our bag of words is gonna be how many words we had in the thing so in this case I think we have something like 45 or something so we're gonna say we have a bunch of neurons and in this case let's just say we have like 45 of them okay so say this is like 45 now this is our input okay this is our first layer this is our input layer now the next layer that we have is 8 neuron so one two three four five six seven eight and they are connected to each our inputs so each input connects to each neuron just like this now I don't want to draw all of them out but you guys get the point it's fully connected just like that okay that's all I'm gonna draw for that now we have another layer that has eight neurons so we draw another eight neurons and all of these neurons are connected together fully connected once again so each one of these neurons connects to each other one in the lair again I'm not going to draw all the lines and then finally we have our output layer which has a soft max activation and this I'll just make it green so it's a bit different has six nerves so one two three four five six now again these are fully connected so it goes like this and all of them connect not gonna draw them all out so our output layer is special because it has this soft max activation so essentially what this means is all of these neurons are gonna be run through this softmax activation function and what that's gonna do is give a probability to each of these neurons so let's say that this first neuron represents hello okay and maybe that's the tag we have maybe this one represents goodbye and so on you know these all represent specific classes well if the model thinks that our response should be the hello tag so one of the responses under the hello tag the miss neuron will have a higher probability than all of these ones and that's essentially the way that this works it's gonna say well I think that you know it's 70% the hello tag so since that's the highest probability we will take that and well then we will grab some kind of response from Hello and spit that out to the user so all our model is really doing is predicting which tag that we should take a response from to give to the user so again we have six tags are because that's how many labels we have and we just our model picks one of these it actually gives us prediction values for all of them we say whatever one is the most highly predicted so maybe this one is like 90% this one's like 0.1% this one's like 5% we take the greatest highest predicted one we grab some responses from that we randomly pick one and then we give that to our user and that's kind of the way that this model works so we take in as input a bag of words and as output we get some kind of class or sorry label telling us what we think we should respond with what tag it comes from and that is how this very basic model works so what we are hoping is gonna happen when we start training and feeding information into our model so these hidden layers are gonna kind of figure out you know what words represent what one of these outputs may be if it sees the word hi it's gonna start changing some weights and changing some biases in here so that we get hello more commonly from that and these hidden layers are really what is doing kind of all of the work and they're what's gonna figure out what's going on and how this works and all of that kind of stuff with more and more intense or tags you would probably want to add more neurons to your hidden layers but two hidden layers is typically enough for a problem like this and you guys will see when we train the model this is actually a very accurate model for this kind of classification task that we're doing because essentially all we're doing is classifying sentences of words to some kind of output in some kind of tag so that is essentially how that works let's close that up now I hope you guys have a little bit of an understanding again I'm not gonna really teach neural networks but I want you guys to understand a little bit on why I chose this model and how it kind of works now the DNN is just a type of neural network and it's just gonna take these networks in or this network that we've defined here and just use that so now it's time to actually fit or our model so to do this we're gonna say model dot fit and what this means is we're actually gonna start passing it all of our training data so let's do this so we're gonna pass it training we're gonna pass it output we're gonna say number of underscore epochs and number of epochs is the amount of times that it's gonna see the same data so in this case we're gonna show it the same data a thousand times and hopefully the more it sees the data the better it gets at classifying now mess with this number make it 2,000 make it 5,000 make it a hundred and see what you get by doing that a lot of machine learning is trial and error so you got to understand that okay so batch underscore size we're gonna set as eight and we're gonna say show underscore metric equals true and this is just so that we get a nice kind of output when we're fitting this model now the last thing to do after we do this is simply save the model so we're just gonna say model dot Save and in this case we're just gonna save it as I guess we'll do like model dot tf2 learn and that's fine we'll save the model and that should just work for us when we start running this if we want to use the model to make some predictions we're gonna do that in the next video but let's just see how this works so let me go ahead I've already activated my environment I'm just gonna run this script and make sure that nothing went wrong here okay so there we go we're running our model you can see it's going through all these different epochs and you'll notice that when it stomps we had an accuracy of 99.97% which means it worked very well for our intents now again this is very simple because we only have six kind of tags here in our intents if you were to add more you would expect your accuracy would drop slightly but this model seems to be working very well at least on the data that it seems so far so the next step and in the next video we're going to start predicting data using this model and then in the final video in this series what we're going to do is actually set up a framework that will allow users to type to the model and get responses there's another kind of bonus part that I might do in this a sixth episode of this series but that's gonna have to wait till later so anyways that has been it for this chatbot AI video if you guys enjoyed please make sure leave a like and subscribe to the channel and I will see you in the next tutorial

Original Description

This python chat bot tutorial will show you how to create a model that can classify our bag of words into a specific class. In the next part we will use the model to make predictions and generate responses. Text-Based Tutorial: https://techwithtim.net/tutorials/ai-chatbot/part-3/ Playlist: https://www.youtube.com/watch?v=wypVcNIH6D4&list=PLzMcBGfZo4-ndH9FoC4YWHGXG5RZekt-Q ◾◾◾◾◾ 💻 Enroll in The Fundamentals of Programming w/ Python https://tech-with-tim.teachable.com/p... 📸 Instagram: https://www.instagram.com/tech_with_tim 🌎 Website https://techwithtim.net 📱 Twitter: https://twitter.com/TechWithTimm ⭐ Discord: https://discord.gg/pr2k55t 📝 LinkedIn: https://www.linkedin.com/in/tim-rusci... 📂 GitHub: https://github.com/techwithtim 🔊 Podcast: https://anchor.fm/tech-with-tim 💵 One-Time Donations: https://www.paypal.com/donate/?token=... 💰 Patreon: https://www.patreon.com/techwithtim ◾◾◾◾◾◾ ⚡ Please leave a LIKE and SUBSCRIBE for more content! ⚡ Tags: - Tech With Tim - Python Chatbot tutorial - Python Chat bot - Chatbt in python - Python deep learning chatbot - Python Tutorials
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video tutorial teaches how to create a deep learning chatbot using Python and TensorFlow, covering neural network architecture, model training, and prediction. By following this tutorial, viewers can learn how to build a chatbot that can classify user input and generate responses.

Key Takeaways
  1. Change Doc's X dot append pattern to words
  2. Change the list from classes to labels
  3. Add an if statement to remove question marks from the unique words
  4. Change output and training into numpy arrays
  5. Define the input shape of the model using TF learned dot input data
  6. Add a fully connected layer with 8 neurons to the neural network
  7. Copy the layer for another hidden layer with 8 neurons
  8. Add two more layers: one with softmax activation for the output layer and another with TF learned regression for training the model
  9. Use TF learned to build the neural network model
  10. Pass the training data to the model
💡 The model predicts which tag to take a response from to give to the user based on the input bag of words, and the hidden layers figure out what words represent what output may be by changing weights and biases.

Related AI Lessons

Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →