Elon Musk AI Text Generator with LSTMs in Tensorflow 2

guest_blog 05 Sep, 2022 • 4 min read

Introduction

Elon Musk has become an internet sensation over the past couple of years, with his views about the future, funny personality along with his passion for technology. By now everyone knows him, either as that electric car guy, or that guy who builds flamethrowers. He is mostly active on his Twitter, where he shares everything, Even memes!

AI Text Generator

He inspires a lot of young people in the IT industry, and I wanted to do a fun little project, where I would create an AI that would generate text based on his previous Twitter postings. I wanted to encapsulate his style and see what kind of weird results I would get.

Preparation

The data I am using was scraped directly from Elon Musk’s twitter, both his posts and replies. You can download the dataset at this link.

Importing the libraries:

Now I’m gonna create the function that is going to get rid of all of the links, the hashtags, tags, and all the stuff that’s gonna confuse the model so that we’re left with clean text.

Python Code:

Let’s define a tokenizer, and apply it to the text. That is how we’re mapping all the words into their numeric representations. We do that because neural networks cannot take strings. If you’re new to that, there is a great series on Youtube by Lawrence Moroney, that I would suggest checking out below:

Now we will have to define max_length(all data needs to be padded to a fixed length, just like with Convolutions), and we also need to turn input_sequences into a numpy array.

We’re gonna create sequences of data, where we will use all the elements except the last one as our X, and the last element as the y, of our data. Also, our y is a one-hot representation of total_words, which can sometimes be a lot of data(if total_words is 5952, that means every y is of shape (5952, ))

 

Model

Below is the configuration of our model.

I’ve tried a couple of optimizers, and I’ve found Adam to work the best for this example. Let’s compile and run the model:

Let’s create a ‘for loop’, that will generate new text, based on seed_text and the number of words that we will define. This part of the code can look a little intimidating, but once you read each line carefully, you’ll see that we’ve already done something similar earlier.

Now is the time to play with our model. Woohoo!


Summary

Space is a big conflation of cats!? Who would’ve known! As you can see, the outputs the model gives are silly, and they don’t make a lot of sense. As with all the deep learning models, there are a lot of things that could be tweaked, in order to generate better results. I’ll leave that to you.

guest_blog 05 Sep 2022

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers

Clear

Debasish
Debasish 14 Oct, 2020

Interesting and very original work.. thank you ;)

Shoaib Sabir
Shoaib Sabir 15 Oct, 2020

This is really great article and was really helpful. Need to know something personally. Please contact me on [email protected] or whatsapp me on +923340806660. Your response would be really appreciated. Thanks and Regards. Shoaib Sabir