Use Machine Learning to Make a Program Write in Any Desired Style

Note: This guide was made for Windows 10 machines, but can be adapted for other OSs.

Ready to take your first step into the exciting world of AI?

The writing bot we’re going to make won’t get you a passing English grade, and will surely get you fired if you try to use it at work. But if you want an introduction into the concepts of machine learning and a pretty fun bot to play with on the side, you’ve come to the right place.

In the end, you’ll have a command line tool that you can feed sample text and make write in a similar style.

1. System Setup

For this project, we’ll be using something called TensorFlow. TensorFlow is an open source library for machine intelligence, and for our purposes is sufficient, and free. But first you’ll need an install of Python and a package installation manager called Pip. Also, TensorFlow requires a 64-bit processor. If you want to confirm you have a 64-bit processor, you can check for the folder C:\Program Files (x86). If it exists, you’re in good shape.

Choose Custimize Installation.

On the Optional Features screen, click Next.

On the Advanced Options screen, change the install location to C:\python3 and click install.

Open up a Command Prompt and copy and paste the following command.

pip3 install --upgrade tensorflow

You now have TensorFlow installed.

Next you’ll need the Python code that uses TensorFlow. You can get it here by clicking the green button that says “Clone or download”. Next, click the Download Zip file, as seen in the picture.

Once downloaded, extract it by opening the zip folder and clicking the Extract All button.

3. Change the Sample Text

With your extracted folder under the name word-rnn-tensorflow-master, go into it and open the data sub-folder. The input.txt file in this directory is what the program will read for its deep learning. Change it to whatever you want for your experiment. Keep in mind that the more text that you supply, the better your machine can learn and accurately mimic. Aim for at least a whole book.

Open the Command Prompt and change into your word-rnn-tensorflow-master directory. You can do this by entering:

cd C:\path\to\word-rnn-tensorflow-master

Note that you need to adapt that example do reflect the folder’s location on your computer.

Start the program with:

C:\python3\python train.py

It will take quite a few hours for the machine to train. Leave the terminal window open and if you’re on a laptop make sure it’s plugged in.

5. Start Writing

Now it’s time to make the program write. In your word-rmn-tensorflow-master directory, open the sample.py file. Find the line that says:

parser.add_argument('-n', type=int, default=200, help='number of words to sample')

The default=200 parameter specified how many words your program will write. Change it to your liking.

You are now ready to write. In the terminal, type

python sample.py

print(model.sample(sess, words, vocab, args.n, args.prime, args.sample, args.pick, args.width)) 
print(model.sample(sess, words, vocab, args.n, args.prime, args.sample, args.pick, args.width).encode('utf-8'))