Blog

18 mai 2017 · Keras · NLP

The network learned Gainsbourg's words, not his songs

A neural network trained on a rented Tesla K80, at about 90 cents an hour, wrote verse in which nine words in ten exist in Serge Gainsbourg's lyrics. Not one of its lines is his.

Article en anglais.

After 60 rounds of training on Serge Gainsbourg's lyrics, a small neural network wrote a short verse in French. Of the 98 words it produced, 88 exist somewhere in the lyrics it learned from. Yet no passage longer than 19 characters, about four words, appears in them verbatim: the machine had learned his vocabulary and his spelling, not his sentences.

The experiment ran in May 2017 on a rented cloud machine with an Nvidia Tesla K80 graphics card, the "Tesla" of the original title, billed at about 90 cents an hour. Rather than build a deep-learning environment by hand, it used Amazon's ready-made Deep Learning machine image; the only friction was matching the versions of Python, Keras and TensorFlow. The training text was a collection of Gainsbourg's song lyrics, about 536,000 characters and 103,000 words once lower-cased and stripped of accents.

Real words, invented lines

The model reads text one character at a time. Given the previous 40 characters, it predicts the next one, and it writes by feeding each prediction back in. It starts from a seed, 40 characters cut from a real song, which is why the first line below is genuine Gainsbourg.

What follows is pastiche. The ten invented words read like near-misses of real ones, "toujoure" for toujours, "metite" for petite, "histou" for histoire. The network leans on the commonest words of the corpus: 69% of the words it wrote are among the 100 most frequent words in the lyrics, which make up 55% of the lyrics themselves. "Je", "la", "de", "a" and "moi" come back again and again.

Training is what made the difference. Another sample left in the code repository, from a run that had not yet learned and drawn at the lowest sampling temperature, is 73% spaces, and 87% of its other characters are the letter "e". The network had found the most common character and little else.

About the data

Model: two LSTM layers of 512 units each with 20% dropout and a softmax output over the roughly 60 distinct characters of the text, trained with Keras on TensorFlow on sequences of 40 characters taken every three characters, sampling at several temperatures. The verse shown is one sample of the output after 60 iterations; a single sample is an illustration, not a measurement of the model. Word checks split the text on anything that is not a letter, so "l'eau" counts as "l" and "eau"; the seed line is excluded from all counts.

Code: github.com/clementlefevre/keras_playground, adapted from the Keras text-generation example. The lyrics themselves are not republished here.