How to write with artificial intelligence
An easy guide to “Deep Writing” without writing any code
In the past few days, I’ve taught a machine learning algorithm how to write in the style of Harry Potter, Hamilton (the musical), and HBO’s Silicon Valley. The mostly non-sensical, occasionally human-like, topically-flavored writing seems to be amusing not only to me, but to many others.
“Dumbledore will get out from behind a cream cake” — Harry Potter: Written by Artificial Intelligence
Thus, I’ve made this quick tutorial to teach you how to create your own instances of “Deep Writing”. This is not going to be an in-depth description of the underlying technology — but instead, a step-by-step guide that anybody can follow (even if you have no coding or machine learning experience).
Step 0: Understanding the simple intuition
Here is a very crude approximation of what is involved in the Deep Writing process. More than anything, this is meant to give you enough intuition and appreciation to follow along with the rest of the tutorial.
- You show a computer some sample text (for example, the Harry Potter books).
- The computer identifies all the unique words in the sample text.
- The computer groups words based on how often they appear together in the sample text (using a particular mathematical model). This is the “learning” portion of “Deep Learning”.
- You pick a starting word (for example, “The”).
- Using what it learned in Step 3, you ask the computer to guess the word most likely to come after the starting word (i.e. “The”). This is recorded as the second word.
- Then, based on the first two words, you ask the computer to guess the third word. And so on.
- Eventually, you tell the computer to stop guessing after many words, and you have successfully created your Deep Writing.
Step 1: Download the code
We are going to use code written by Sung Kim (who teaches computer science at HKUST). This code is very similar to the code I used, but is a little bit more generally applicable.
Visit this link, click on the green “Clone or download button”, and then choose “Download Zip”.
Find the zip file in your downloads folder, and double-click to unzip it. Drag the folder to your desktop.
Step 2: Customize the sample text
Open the file input.txt, which you can find in word-rnn-tensorflow-master > data > tinyshakespeare > input.txt. Then, delete all the text in the file, and replace it with the sample text that you want to use. Make sure you save the updated file.
This sample text is the text that your algorithm will read and use as inspiration. For example, you can use the text of Harry Potter, the lyrics to Hamilton, the scripts from Silicon Valley, and so on.
The longer the sample text, the “better” the output will be. Of course, the longer the sample text, the longer it will take to train your model. For comparison, the Harry Potter sample text was 467,678 words long.
Step 3: Install TensorFlow
TensorFlow is a machine learning library made by Google. We need to download it in order to run our code.
To do this, we will use the Terminal. To open the Terminal, click command + space, which will open “Spotlight Search”. Type “Terminal” and click enter.
The terminal should open.
Copy and paste the following line into the Terminal and click enter.
sudo easy_install pip
Then, copy and paste this next line and click enter.
sudo easy_install --upgrade six
These two line prepare your system to install TensorFlow.
Then, copy the below line into the Terminal and click enter. This identifies which version of TensorFlow you want to install.
Copy this final line and click enter. This will start the installation.
sudo pip install --upgrade $TF_BINARY_URL
After a few minutes, the installation will finish and you can now use TensorFlow on your computer.
Step 4: Train the model
With TensorFlow downloaded on your computer, it’s time to train your model (i.e. “group” words based on patterns).
To start, we want to let the computer know that we want to use TensorFlow. Copy and paste the line below into the Terminal and click enter.
Now, we want to run the training file. To do this, we first need to tell the Terminal where the file is. Copy and paste the below line and click enter.
Lastly, start the training file by copying and pasting the following line into the Terminal. Click enter and the training will begin.
Training will take many hours (especially if your sample text is large). It will also drain your battery really fast, so make sure your computer is plugged in. While training, do not close the Terminal window and do not close your computer.
Step 5: Create the “Deep Writing”
Once your model finishes training, it’s finally time to create the Deep Writing.
To prepare, open the file sample.py, which lives inside the folder word-rnn-tensorflow-master. Inside of the file, search for the line of code that says:
parser.add_argument(‘-n’, type=int, default=200, help=’number of words to sample’)
Change the default value to the number of words you want in your instance of Deep Writing. I recommend something between 1000 and 2000.
parser.add_argument(‘-n’, type=int, default=1500, help=’number of words to sample’)
Once you make the change, save the updated file.
Then, go back to the Terminal and copy and paste the following code.
It will take a minute or two, and then the computer will spit out a beautiful instance of Deep Writing.
With a little bit of formatting, it’s ready to be published.
Step 6: Submit your Deep Writing for publication
I created a publication on Medium to collect interesting examples of Deep Writing. If you create Deep Writing that you want to share, Tweet me a link, and I’ll added it the publication.
Stories, songs, and speeches written by deep learning (artificial intelligence)
If you are interested in learning more deeply about machine learning and the underlying math, here’s a good place to start.