Chat Bot With PyTorch - NLP And Deep Learning - Python Tutorial (Part 3)

Patrick Loeber · Beginner ·🧬 Deep Learning ·6y ago

Key Takeaways

This video tutorial demonstrates how to build a simple chatbot using PyTorch and Deep Learning, covering basic Natural Language Processing (NLP) techniques such as Stemming, Tokenization, and bag of words, and implementing a PyTorch model and training pipeline.

Full Transcript

hey guys and welcome back to the third part of our chat bot tutorial so last time we created our data set and now in this part we will continue and implement the actual PI torch models or deep learning neural nets and then the training pipeline so let's do this and before we start I noticed a little arrow here so here I have to use a comma instead of a dot of course and then in our chat data set class in the get item function here I call this index not short I DxO index so now we can continue and now we want to create the actual model so for this we create a new file so let's say train dot pi sorry not trained up i but model dot pi and here we import torch and import torch torch dot and n s and n and then we create a new class for our models so class neural net or whatever you want to call this and this must be derived from n n dot module and then we have to define the init which gets self and the input size and the hidden size and the number of classes so this will be a feed-forward neural net with two hidden layers and now if you have a look at the slides again here I have the feed-forward neural net which gets our bag of words as an input then we have one and neural one layer fully connected which has the number of different patterns as the input size and then the hidden layer and then another hidden layer and the output size must be the number of different classes and then we apply the softmax and get probabilities for each classes so let's do this so we want to create three linear layers by saying self l1 equals n n dot linear and this gets the input size and then the hiddenness sighs then let's copy this to x so our second layer has the hidden size as input and the hidden size also is output and then the third layer has the hidden size as input and the number of classes as output so input size and number of classes must be fixed but you can change the hidden size if you want to and then let's also create an activation function for in-between so we use a reloj activation function then we want to import the forward or implement the forward pass so this gets self and x and then we say out equal self dot l1 and this gets X first then we apply our activation function in between so we say out equals self dot re Lu which gets X now then we do the same thing two more times so we apply our second linear layer which gets out as an input and then puts out the next output then the next activation function and then the third linear layer so l3 and at the end we want to be careful so we don't want and activations no activation and no softmax because lai we apply the cross-entropy laws and this will already apply this for us so don't apply this here and simply return the output then so this is our model now we can go back to our training file and then create our model so here we say model equals torch sorry this will be our model so we have to import this so we say from model import neural net then we create this so we say neural net and then it gets the input size it gets the hidden size and it gets the output size output size so we have to define this here so we say our hidden size let's say our hidden size equals 8 as well so you can change this then the output size equals and this is the number of different classes or texts we have so the length of the text and the input size is the number or the length of each pack of words that we created and the bag of words has the same length as the all words array so we could either use the length of all words or we say the length of X train 0 so the first bag of words because they all have the same size this is the input size so now let's for example let's print the input size and let's print the length of all we hurts to see if this is correct then let's print the output size and let's print the length or let's print the whole tax array to see if this matches and then let's simply save this and run this so let's say python drain that pi and then we get an arrow here because of course i forgot to call this super method so here I have to call super neural-net and then it also gets self and then the super init function so let's clear this and run this again and now we see that this is working so we have the correct input size which matches the length of all words and we have the output size seven because we have seven texts in this array so this is working and let me delete this print statement because I don't want this anymore and now we can continue so one more thing that we can do is if we have GPU support so then we can use it so let's check if we have the cheap u available by saying device equals torch dot device and then we say this is the cuda device if torch dot cuda dot is available as a function otherwise we simply use the cpu and then we can push our model to the device if it is available and then let's create the loss and optimizer as always in our PI torch training pipeline so we say criterion equals n n dot cross entropy loss and our optimizer equals torch dot Optim dot atom and we want to optimize model dot para parameters and as a learning rate we say this is the learning rate that we have to specify so let's put them here to our hyper parameter so let's say learning rate equals point zero zero one and let's also define the number of epochs some epochs num epochs equals 1000 so you can try out different ones here and then let's do our actual training loop so we say for epoch in range num epochs and then we use our training loader so we say for and then we can unpack this words and labels in our training loader and then we also want to push it to the device so as we say words equals words dot to device and the same with the labels labels equals labels to device and then we call or do the forward pass by simply saying out out put equals and then we call model with words as the input and then let's calculate the loss or the loss equals the criterion which will get the predicted out and the actual labels then we do the backward pass the backward and optimizer steps so we have to empty the gradients first so this is one thing that we should remember in the PI torch optimizations we say optimizer dot zero zero grat then we call loss dot loss dot back sorry backwards to calculate the back propagation and then we say optimizer dot step and then this is our training loop and now it's also print some things so let's say if epoch plus 1 modulo 100 equals equals zero then so every 100th step then we want to print as an F string we want to print the current epoch which is the epoch plus 1 and we also want to print all epochs a number of epochs and we say loss equals and then here loss dot item and format it and to print let's say four point four F four decimal values then we are done and then let's also print let's grab this and print the final loss here so we say final loss equals the loss at the end and then let's clear this and run this and hope that everything is working so torch is not defined so here we say torch dot cuda so again let's run this and now our training loop starts so we see our loss is 0.7 in the next step it's already decreased alot so only point zero seven and it's further decreasing so now it's done and we could see that our loss successfully decreased with each epoch and at the end it's very low so our example patterns are not very complex but we can see that our neuron that is very good for this purpose so yeah I think that's it for part three and in the last part we will see how we can now save this start model or this train model and then use it in our chat function and then implement the actual chat so see you next time and if you liked this tutorial please like and subscribe

Original Description

In this Python Tutorial we build a simple chatbot using PyTorch and Deep Learning. I will also provide an introduction to some basic Natural Language Processing (NLP) techniques. 1) Theory + NLP concepts (Stemming, Tokenization, bag of words) 2) Create training data 3) PyTorch model and training 4) Save/load model and implement the chat Resource: This tutorial was inspired and adapted from the following article: "Contextual Chatbots with Tensorflow": https://chatbotsmagazine.com/contextual-chat-bots-with-tensorflow-4391749d0077 ✅ Write cleaner code with Sourcery, instant refactoring suggestions in VS Code & PyCharm: https://sourcery.ai/?utm_source=youtube&utm_campaign=pythonengineer * 📚 Get my FREE NumPy Handbook: https://www.python-engineer.com/numpybook 📓 Notebooks available on Patreon: https://www.patreon.com/patrickloeber ⭐ Join Our Discord : https://discord.gg/FHMg9tKFSN If you enjoyed this video, please subscribe to the channel! NLTK: https://www.nltk.org You can find the code on GitHub: https://github.com/patrickloeber/pytorch-chatbot PyTorch Beginner Course: https://www.youtube.com/playlist?list=PLqnslRFeH2UrcDBWF5mfPGpqQDSta6VK4 Please checkout my website to see all tutorials: https://www.python-engineer.com You can find me here: Twitter: https://twitter.com/patloeber GitHub: https://github.com/patrickloeber Icons: https://fontawesome.com/icons/comments https://fontawesome.com/icons/robot #PyTorch #NLP #DeepLearning ---------------------------------------------------------------------------------------------------------- * This is a sponsored or an affiliate link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Patrick Loeber · Patrick Loeber · 0 of 60

← Previous Next →
1 Lists in Python - Advanced Python 01 - Programming Tutorial
Lists in Python - Advanced Python 01 - Programming Tutorial
Patrick Loeber
2 Tuples in Python - Advanced Python 02 - Programming Tutorial
Tuples in Python - Advanced Python 02 - Programming Tutorial
Patrick Loeber
3 Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Patrick Loeber
4 Sets in Python - Advanced Python 04 - Programming Tutorial
Sets in Python - Advanced Python 04 - Programming Tutorial
Patrick Loeber
5 Strings in Python - Advanced Python 05 - Programming Tutorial
Strings in Python - Advanced Python 05 - Programming Tutorial
Patrick Loeber
6 Collections in Python - Advanced Python 06 - Programming Tutorial
Collections in Python - Advanced Python 06 - Programming Tutorial
Patrick Loeber
7 Itertools in Python - Advanced Python 07 - Programming Tutorial
Itertools in Python - Advanced Python 07 - Programming Tutorial
Patrick Loeber
8 Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Patrick Loeber
9 Exceptions in Python - Advanced Python 09 - Programming Tutorial
Exceptions in Python - Advanced Python 09 - Programming Tutorial
Patrick Loeber
10 Logging in Python - Advanced Python 10 - Programming Tutorial
Logging in Python - Advanced Python 10 - Programming Tutorial
Patrick Loeber
11 JSON in Python - Advanced Python 11 - Programming Tutorial
JSON in Python - Advanced Python 11 - Programming Tutorial
Patrick Loeber
12 Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Patrick Loeber
13 Decorators in Python - Advanced Python 13 - Programming Tutorial
Decorators in Python - Advanced Python 13 - Programming Tutorial
Patrick Loeber
14 Generators in Python - Advanced Python 14 - Programming Tutorial
Generators in Python - Advanced Python 14 - Programming Tutorial
Patrick Loeber
15 Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Patrick Loeber
16 Threading in Python - Advanced Python 16 - Programming Tutorial
Threading in Python - Advanced Python 16 - Programming Tutorial
Patrick Loeber
17 Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Patrick Loeber
18 Function arguments in detail - Advanced Python 18 - Programming Tutorial
Function arguments in detail - Advanced Python 18 - Programming Tutorial
Patrick Loeber
19 The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
Patrick Loeber
20 Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Patrick Loeber
21 Context Managers in Python - Advanced Python 21 - Programming Tutorial
Context Managers in Python - Advanced Python 21 - Programming Tutorial
Patrick Loeber
22 KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
Patrick Loeber
23 Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Patrick Loeber
24 Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Patrick Loeber
25 Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Patrick Loeber
26 Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Patrick Loeber
27 Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Patrick Loeber
28 SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
Patrick Loeber
29 Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Patrick Loeber
30 Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Patrick Loeber
31 Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Patrick Loeber
32 PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
Patrick Loeber
33 K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
Patrick Loeber
34 Anaconda Tutorial - Installation and Basic Commands
Anaconda Tutorial - Installation and Basic Commands
Patrick Loeber
35 PyTorch Tutorial 01 - Installation
PyTorch Tutorial 01 - Installation
Patrick Loeber
36 PyTorch Tutorial 02 - Tensor Basics
PyTorch Tutorial 02 - Tensor Basics
Patrick Loeber
37 PyTorch Tutorial 03 - Gradient Calculation With Autograd
PyTorch Tutorial 03 - Gradient Calculation With Autograd
Patrick Loeber
38 PyTorch Tutorial 04 - Backpropagation - Theory With Example
PyTorch Tutorial 04 - Backpropagation - Theory With Example
Patrick Loeber
39 PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
Patrick Loeber
40 PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
Patrick Loeber
41 PyTorch Tutorial 07 - Linear Regression
PyTorch Tutorial 07 - Linear Regression
Patrick Loeber
42 PyTorch Tutorial 08 - Logistic Regression
PyTorch Tutorial 08 - Logistic Regression
Patrick Loeber
43 PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
Patrick Loeber
44 PyTorch Tutorial 10 - Dataset Transforms
PyTorch Tutorial 10 - Dataset Transforms
Patrick Loeber
45 Download Images With Python Automatically - Python Web Scraping Tutorial
Download Images With Python Automatically - Python Web Scraping Tutorial
Patrick Loeber
46 PyTorch Tutorial 11 - Softmax and Cross Entropy
PyTorch Tutorial 11 - Softmax and Cross Entropy
Patrick Loeber
47 Select Movies with Python - Web Scraping Tutorial
Select Movies with Python - Web Scraping Tutorial
Patrick Loeber
48 PyTorch Tutorial 12 - Activation Functions
PyTorch Tutorial 12 - Activation Functions
Patrick Loeber
49 List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
Patrick Loeber
50 PyTorch Tutorial 13 - Feed-Forward Neural Network
PyTorch Tutorial 13 - Feed-Forward Neural Network
Patrick Loeber
51 How To Add A Progress Bar In Python With Just One Line - Python Tutorial
How To Add A Progress Bar In Python With Just One Line - Python Tutorial
Patrick Loeber
52 PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
Patrick Loeber
53 The Walrus Operator - New in Python 3.8 - Python Tutorial
The Walrus Operator - New in Python 3.8 - Python Tutorial
Patrick Loeber
54 PyTorch Tutorial 15 - Transfer Learning
PyTorch Tutorial 15 - Transfer Learning
Patrick Loeber
55 YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
Patrick Loeber
56 YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
Patrick Loeber
57 YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
Patrick Loeber
58 YouTube Data API Tutorial with Python - Analyze the Data - Part 4
YouTube Data API Tutorial with Python - Analyze the Data - Part 4
Patrick Loeber
59 AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
Patrick Loeber
60 Ultimate FREE Study Guide for Machine Learning and Deep Learning
Ultimate FREE Study Guide for Machine Learning and Deep Learning
Patrick Loeber

This video tutorial teaches how to build a simple chatbot using PyTorch and Deep Learning, covering basic NLP techniques and implementing a PyTorch model and training pipeline. The tutorial provides a hands-on approach to building a chatbot, including creating a training dataset, defining a neural network architecture, and training the model using a GPU. The tutorial is suitable for beginners with basic Python knowledge.

Key Takeaways
  1. Create a training dataset
  2. Define a neural network architecture
  3. Implement a PyTorch model
  4. Train the model using a GPU
  5. Evaluate the model's performance
💡 Using a GPU can significantly speed up the training process of a neural network

Related Reads

📰
Want to get started with deep learning
Get started with deep learning by leveraging resources like Andrew Karpathy's playlist and frameworks such as TensorFlow or PyTorch
Reddit r/deeplearning
📰
Building a Deepfake Detector From Scratch — What Nobody Tells You
Learn to build a deepfake detector from scratch and understand the challenges involved in detecting AI-generated fake media
Medium · Deep Learning
📰
Unfolding the Meandering Path: High-Dimensional Invariance and the Flat 2D Plane of Neural…
Learn about high-dimensional invariance and its relation to the flat 2D plane of neural networks, and how to apply these concepts to improve model performance
Medium · Deep Learning
📰
Implementing Neural Style Transfer from Scratch: The Project That Started It All
Learn to implement Neural Style Transfer from scratch and understand its significance in deep learning
Medium · Deep Learning
Up next
Image Classification with ml5.js
The Coding Train
Watch →