And here we are, the final project of my Metis Data Science Bootcamp. The first four projects I’ve done are as follows:
This time round, my aim is to generate short poetry by feeding a poetry corpus into a Long-Short-Term Memory (LSTM) neural network.
For my capstone, I wanted to find a way to marry my burgeoning skills in machine learning with my existing interest in poetry (both reading and writing; incidentally a collection of my haikus on Instagram can be found here).
Generative poetry seemed like a good place to start. Beyond the appeal to my interests, however, such a model can have practical applications:
For this project, I used the dataset of 3-line poems put together by Jeremy Neiman for his own post on generative poetry; in his case he focused on generating haikus (hence the restriction to 3 lines).
Using neural networks to generate haiku while enforcing the 5–7–5 syllable structure.
This dataset was in turn put together from various other datasets as follows.
31,410 poems in total
I used Keras to put together an LSTM neural network model that takes in a sequence of 15 tokens and outputs the next token (or more accurately the probabilities of the next token). The model structure is illustrated below.
My LSTM model
Using this model, I tried out two approaches as follows, training for 200 epoches:
With this approach, I first cleaned the words by lower-casing them and removing all non-alphanumeric characters; I kept stop words as they could have meaning in poetic contexts.
Capital letters and punctuation can have poetic meaning as well, though arguably not as much, so I decided to ignore them in this approach.
I then processed each poem into a series of 15-token sequences:
The resulting sequences for all poems were then concatenated. The diagram below illustrates this process.
#nlp #metis #bootcamp #deep-learning #poetry #deep learning