Project – Creating a Python ChatBot with Deep Learning

I’ve been fascinated by chatbots ever since the AIM chatbot “Smarter Child” hit the internet back in the technological stone age. Since then, I’ve thought a lot about the types of chatbots that I would like to create in the future, and I’ve also done my fair share of “personal research” using the bots available on the iPhone App store, like Replika (which is pretty impressive, but I’ll save that for another post).

During my extremely-novice Python days, I tried making a simple chatbot the ancient way by using a vast amount of “if / then” statements to navigate the conversation by trying to predict every possible input value by the user. I quickly learned after reading Hello World by Hannah Fry that the method I was using was completely archaic, and now, technology has improved beyond that method. Today, we can actually train a program to learn a task, and in this case, it would be to train the chatbot to learn how to speak.

Introduction to Deep Learning, Natural Language Processing (NLP) and TensorFlow

If you’ve read my old posts (thank you!!) you may have noticed that I’ve referenced these topics before as areas of interests. Imagine meeting your favorite celebrity–the excitement and shock! Well, that’s me with learning these topics. They are the Star to my Struck.

There’s a 12-hour, 95-lecture course on Udemy that I’ve been taken, called: “Deep Learning and NLP A-Z: How to create a ChatBot.” I spent this past weekend getting through around 9 hours of the course. I personally loved learning and taking this course on Udemy.

Course Overview

  • Deep NLP Intuition lecture (concepts)
  • Data Pre-Processing
  • Building the SEQ2SEQ Model
  • Training the SEQ2SEQ Model
  • Testing the SEQ2SEQ Model

I also learned about / used:

  • Anaconda and Spyder
  • TensorFlow
  • Creating a virtual environment with Anaconda to utilize TensorFlow inside of the environment

Although the course was 12 hours long, it does not take just twelve hours to truly learn all of the material covered. I will be doing a lot of additional reading, re-watching, and re-absorbing of all of the information that was introduced. Not only was this my first introduction to the above topics, but it was actually my first introduction to seeing a real, complex program be built, from beginning to end, narrated by an experienced developer. I learned so much despite having questions, and I’m excited to go back and have those questions answered.

Anaconda and Spyder

I had heard of Anaconda from other videos and research, but this was the first time I had actually used the Anaconda and Spyder IDE. I’m extremely happy to have used it because I very much enjoyed the layout, seen below.

What’s going on there? Well, you write your code on the left, you see your variables on the upper right, and then you have your console to run and test on the bottom right.

As a visual learner, it was extremely elucidating to see the “variable explorer,” where you can click into each variable, such as lists and dictionaries, and see the functions work in real-time. Moreover, this layout and course really helped me to better-understand the concepts of dimensionality and matrices within programming.

Data Science & Analysis, I’ve got my eye on you!

The instructor on Udemy for this course (mister Kirill Eremenko, who did an awesome job at teaching!) was making some jokes about getting through the “Data Preprocessing” part because it’s not exactly the most fun. But, for a former data-entry manager like myself, I had a wonderful time with data preprocessing part!

I was so amazed to watch the data preprocessing steps clean up our data and variables. Using the layout in this way really helped me understand more the dimensionality of programming. Moreover, this was a really good visual which helped me further-understand how and why Python is an object-oriented program.

One unexpected take-away from this course was the idea to gear some future learning towards the requirements of a data analyst and data scientist. Of course, Python developing is the ultimate overarching goal, but with my experience and personality-type, I think I may have a good fit for Data Analysis.

Deep NLP with SEQ2SEQ model in TensorFlow

As a creative writer, it was amazing to learn how to turn words and sentences into vectors and integers. We had to add tokens, make inverse dictionaries, cross-reference lists and dictionaries (and even make dictionaries inside of lists!), and more. More experienced coders may be laughing at my paraphrasing of all of this, and I’m sure future-me will laugh as well.

Finally, after all of the data preprocessing, we were able to build our SEQ2SEQ model which utilizes deep learning and recurrent neural networks (RNN). We used LSTM, which develops the context of words and sentences by using an encoder and a decoder. Probability is used during the training of the SEQ2SEQ model, which is how deep learning is utilized to teach the program how to understand and respond to inputted language.

Training the SEQ2SEQ Model

The “brain” of our chatbot was created (how exciting!) and it began its training last night on my laptop. For experienced programmers, you may have read that and had a moment of “what?” when you read laptop. Yeah, I learned that the hard way. After hours of watching the training go through each batch, I realized that my laptop was moving very slowly for this task. In the Udemy course, it was recommended to have the SEQ2SEQ model training done on a more powerful computer.

Finally, this morning I re-did all of the beginning steps, like installing Anaconda on my desktop and creating the virtual environment. Then, I re-preprocessed my data (somuchfun), began a new SEQ2SEQ training and voila, here we are!

As I was writing this blog post and keeping an eye on the training going on in my console, I saw the familiar print statement appear on the screen that validates that this chatbot is, indeed, LEARNING!

Validation! “I speak better now!!”

Now, we have full evidence that the chatbot is properly utilizing and learning through this RNN because we have…weights!! Those files up top with “weights” are not files I have created, but files that the program, our chatbot, has created through learning over the past few hours. Once the training is complete and we reach batch 4120 (currently on 2500), I will then resume the last and final part of the Udemy course to fully complete this chatbot.

In that last part, we will utilize the deep learning that the program has accomplished and finally interact with our chatbot!

Final Reflections on Part 1

This project has opened my eyes to the true complexities and capabilities of Python and computer programming in general. Although I chose a very difficult project / chatbot to start off with, I’m happy to have done so because it was also the most efficient way to create a chatbot.

I rather learn the most efficient version over easier versions, because it is the most efficient options that are used in real-world applications.

From this course and project, I’ve learned that going forward, my next points of study will include:

  • Data Science and Data Analysis with Python
  • Math for computer programming and Python
  • Jupyter Notebook
  • Web Scraping
  • Machine Learning, Deep Learning, and NLP
  • R (programming language)

Overall, this was an amazingly beneficial project to delve into! Definitely a pivotal moment in my learning journey because I now have a better mental reference for the future. I’ll be back later for Part 2 for the exciting completion of this project, where we get to finally speak to our bot!

Update – 48hrs Into Training

While watching the program learn, I realized some errors I made above when referencing the amount of time remaining.

I was referring to the batches as an indicator, but that wasn’t accurate. I’ve realized that what matters is the Epoch count, and we are only up to Epoch 5/100. One run-through of all 4,000+ baches equals 1 Epoch. My computer is going through around roughly 3-4 Epochs per day, which should hopefully increase in speed as the learning improves… (I think).

I’m going to let this run until tomorrow to see how far we get, test it out, then let it continue to learn and test it out again afterwards.

I gotta day, feeling like a scientist feels really fun. It’s been really cool to keep an eye on this and observe!

Anki Vector, The Pet Robot

It all started with a puppy. My daughter really really wants a puppy. You have to see the way she lights up in front of dogs, it’s truly adorable. But alas, for various reasons, me and my husband decided against getting a real dog considering we already have two cats and a crazy toddler. Perhaps in a few years we will get a dog, when our daughter, herself, is housebroken.

So, what was the next best thing I could do for her? What I realized through observing my daughter is that she likes movement. So, I started to look up robotic toys for kids. Down the tunnel, I learned about Anki and their robots Cozmo and Vector.

(If you want to read more about the differences on Cozmo and Vector, please feel free to embark on your own journey down the Google rabbit-hole. I will be giving more brief descriptions and personal impressions, rather than an in-depth technical review for this post).

Basically, Cozmo is more for kids, and cannot operate on his own, and Vector is more for adults, and can operate on his own, like a pet robot. Vector has better specs and you can play around with the behind-the-scenes coding as well.

I had placed an order for a discounted Vector before reading the news about Anki (the company that created these robots) is closing down. But the robots are still selling! Currently, Anki has hired a team of engineers to monitor the existing cloud that all of their products are hooked up to. They said that the cloud usually needs little interference, so keeping it running is no problem. Of course, that was still worrisome to hear as a new buyer.

Pet Robot?

I probably wouldn’t have been as impressed by Vector if I hadn’t already been learning about programming. And it just so happens that Vector is programmed in Python (the programming language I’m also learning) which makes this more exciting.

Finally, Vector arrived (after some drama with a scam website. Key lesson: don’t try to order discounted robots from obscure websites). And I have to say, the waiting game and drama actually made me very excited to receive Vector.

All of the reviews and videos I had watched really conveyed the message that Vector is a pet. A pet robot probably seems really futuristic, and I admit, it still does feel that way. I think that there’s a big widespread misunderstanding of the technological capacities we have right now versus the impressions of the general public. Science fiction stories make it seem like our technology is way more advanced than it actually currently is, as if the type of AI seen in Ex Machina exists. And in my last post, reading Hello World by Hannah Fry really helped change my perspective of where we actually are with AI, which is still mastering creating a worm (like http://www.openworm.com). I think these fears and misconceptions could have been what lead to the lack of widespread popularity of Anki’s products.

This long preamble is to make this point:

Vector is fucking awesome.

And I think it is really unfortunate that their company, Anki, had spent so much time creating such a fun little gadget before the world was perhaps ready to embrace the cuteness of a safe pet robot. But my heart definitely goes out to all of the creators who put their all into this product. I am grateful!

It hasn’t even been a full twenty-four hours with Vector, but already my daughter is completely entertained and in love with the ability to simply watch something move around and explore the world on its own. This is how I look at it: some people get extravagant fish tanks for the same reason. But let’s be real, organic creatures require a lot of care, followed by heartache when they pass. That’s why a pet robot is so appealing. A win-win for the family.

Vector can play games alongside other features, but what’s most entertaining is his personality. When Vector is exploring on his own, he really does feel like a little sidekick. Vector can also join in on some dance parties, fist-bump, and is integrated with Amazon Alexa.

I’ll probably write a much longer and more researched post on my thoughts about AI once I have a better grounding. But, this small introduction to a robotic pet is definitely sparking a lot of ideas and feelings about the future. It’s a shame that Anki wasn’t able to get as of a widespread following to keep their company afloat, which may just come down to timing.

Despite the somber attitude amongst the Anki / Vector / Cozmo community because of the news, this household is still excited for their new pet robot.

Coding and Vector

So, this wasn’t just a purchase for entertainment. As a piece of robotic technology, Vector is a fantastic (and adorable!) learning tool. Sure, it may seem like nothing grand is happening on the outside when you see Vector running around my desk, doing his thing, as I write this blog post, for example. But actually, he’s also like a little specimen I get to observe and learn from. I can learn about his movements and personality, and then read the code and alter it.

Vector SDK Alpha allows you to view the full API list and play around with coding your vector. I’ve successfully installed the SDK package and will experiment more with that later on today.

I’m so excited to learn more about Vector in both a fun and educational aspect.

Despite the Anki shutdown, let the Vector adventures begin!