Composing songs with AI: how the genetic algorithm works
Fitness function, crossover and mutation: how a population of cumbia lyrics improves generation after generation until it produces a good song.
A genetic algorithm evolves songs the way nature evolves species. Héctor Cotelo explains the core of the AI that wrote cumbia lyrics at Quanam in 2017: which grammar, rhyme and meter criteria score each song, and how the best ones are combined and mutated into the next generation.
TABLA DE CONTENIDOS
Series «Songwriting with artificial intelligence», part 2 of 3. Part 1 · Part 2 · Part 3
In my previous article I wrote about a project I built to test whether a computer could learn to write songs, and I promised to explain how the algorithm works. This article describes the method at the heart of that AI: a genetic algorithm that evolves a population of songs and keeps the best ones based on grammar, rhyme and meter.
What a genetic algorithm is
A genetic algorithm is inspired by biological evolution. These programs evolve a population (of songs, in this case) through operations analogous to those in nature, such as mutation and recombination, and use predefined criteria to select the fittest individuals, which survive and reproduce.
To explain it, I’ll use the algorithm we built, which starts from an initial population of hundreds of songs generated automatically by a language model built from the corpus (the set of cumbia songs).
The fitness function: what makes a song good
Each song is scored by a fitness function that measures how good it is. The highest-scoring songs are more likely to be selected to produce, by combining and mutating, a new generation of “child” songs. The process repeats until it reaches a solution, in other words, a good song.
The fitness function scores the songs to tell the good ones from the bad. The main criteria it checks are:
- whether the verses are grammatically correct;
- whether verses no more than two lines apart rhyme;
- whether the verses follow a given meter.
The more a song meets these criteria, the higher its score.
Crossover and mutation: the genetic operators
Once the first generation has been scored, the genetic operators produce the second one. Two operations were defined: crossover (or recombination) and mutation.
Crossover takes some high-scoring songs and combines them, swapping verses to create the child song. A randomly chosen “mask” usually determines which parent each verse comes from.
Mutation replaces words using an ontology and a thesaurus. In random “meter” mutation, any word in a verse is swapped at random for one with a similar meaning, aiming for every verse in the stanza to have the same number of syllables. Another operation, targeted “rhyme-seeking” mutation, replaces the last word of nearby verses, trying every combination of their synonyms until they rhyme.
Selection and stopping condition
For each child song, parents are picked at random, preferably from the best, but also from some weaker ones to preserve diversity. “Reproduction” repeats until the second generation reaches an appropriate size.
In general, the average fitness score should rise from one generation to the next, which means each generation has better songs.
The cycle repeats until a stopping condition is met, which is often hard to define. Here it was a fixed number of generations, combined with some manual inspection. In outline, the genetic algorithm works like this:
- Generate the initial population of songs from the language model.
- Score each song with the fitness function.
- Pick parent songs, preferably among the highest-scoring.
- Apply crossover and mutation to form a new generation.
- Go back to step 2 until the stopping condition is met.
What the runs produced
The lyric produced by one of the runs had too much perfect rhyme, which isn’t ideal for a “serious” song unless it’s rap or a children’s song. Still, I think it’s clear that an AI can write song lyrics automatically.
In my next article I’ll explain how the first generation of songs was generated automatically with an n-gram language model.
Héctor Cotelo
Business Analytics & Information Management Consultant