Teaching A Computer To Be A Poet
The Internet already has several popular poetry generators, but many of them don’t sound very real, so I decided to create my own: The BoredHumans.com Poetry Generator
Poetry generators are usually set up using one of these methods:
A) It has a fill-in-the-blank type system (like Mad Libs). For example, a Robert Frost-style poem is used as a template where some words are changed based on various nouns and adjectives that you choose. This makes all the poems sound very similar.
B) It takes lines from existing poems and rearranges them into a new poem. This makes it sound very impressive, but it is not the same as what a real poet would do.
C) An AI is trained on poems that use old-fashioned English such as:
And canst thou mock mine agony, thus calm
In cloudless radiance, Queen of silver night?
Can you, ye flow’rets, spread your perfumed balm
Mid pearly gems of dew that shine so bright?
(From “Dispair” by Percy Bysshe Shelley)
There is a great posting by Gwern on how to do that, but while most older people consider this to be classic poetry, I don’t think many young people want to read poems like that. Using recently released NLP (natural language processing) technology, I set out to develop something better.
Build better voice apps. Get more articles & interviews from voice technology experts at voicetechpodcast.com
To start things off, I downloaded GPT-2 Simple by Max Woolf, which lets you use OpenAI’s GPT-2 pretrained language model to train your own text prediction model. You can see a great example of GPT-2 in action at Adam King’s TalkToTransformer.com website, where you type something, and the neural network automatically completes it for you. So, theoretically, you could start typing a poem, and it would finish it for you. But it was not trained on poems, it was trained on a giant database of text from millions of web sites, so it is not very poetic.
To turn it into a poet, I fine-tuned the model by feeding it a database of modern poems. The problem with these poems, though, is that many times they are too modern sounding in that they lack the sound and structure of a classic poem. They sound more like a story, and sometimes use r-rated words or talk about inappropriate topics. I wanted something more in the middle of all of that, so after training it on modern poems, I then fine-tuned it even more by training it on thousands of song lyrics from artists that I think of as poetic (such as Bruce Springsteen and Coldplay).
Next, I filter out swears, delete poems that don’t have enough unique lines, delete weird punctuation the AI sometimes adds, and make other little adjustments like that to make the poems sound better.
Giving a good title to the poem is also important, so I put a lot of effort into that part. I decided right from that start that trying to train a machine learning model to make a title would probably not work very well, so instead, I programmed a bunch of rules to use to pick the title. The general idea is that it picks the line from the poem that is repeated the most, and if that line is too long or too short, it chooses an alternate line.
Once I had my ML model producing decent poems, I still needed to turn it into a web page so other people could use it. ML programs are mainly written in Python, and there is no way to make that directly work with a website. The model has to be “deployed,” which usually means putting in on a server (such as AWS or Google Cloud) and then setting it up as a backend service (like an API) using Flask.
In the end, I was able to get my poem generator to do exactly what I wanted, which is to make modern-sounding poems that don’t use recycled lines or templates from old poems. Much like a real poet, the AI has learned to create something totally new.