Neural Network Programming - Part 4 - A Musical Chord Generator


Language: PHP 5.6

Well, now that we have fixed up the neural network, it's time to use it to try to solve more practical problems. I decided on a use case near and dear to my heart. Let's build a chord predictor! When fed with 3 chords, it should output what a good option for a fourth chord should be. I'm just going to use the 7 basic major chords for this example: I, II, III, IV, V, VI, VII. We really only need 3 neurons per input to encode this, but to allow myself the ability to add minor chords later, I'm going to set this up using 4 neurons. The fourth bit will later act as a minor/major flag. The output is a chord as well and thus will utilize 4 neurons.

Training Data

To build up a set of training data, I used my own knowledge of music theory and input sequences of 3 input chords and a reasonable output chord in excel. Excel, with the use of cell formulas, would build up an array of input and expected output bits. Remember I'm using the term bits and neurons interchangeably here.


I ran this training data on the PHP neural network with 50000 iterations and got it to output an array of weights. Then, obviously, I fed that same array back in and attempted to use arbitrary chord inputs to see what chord it would give back to me.

As a side note, the combination of possible inputs (Without minor chords) is: 343, but since each of those combinations can have 1 of 7 possible output chords, the true amount of possibilities is: 2401. That number may seem large, but remember, a great deal of those possibilities won't sound very good.

Using the Network to Build a Song

I decided I wanted to use the Neural Network to build a 16 measure chord progression. This requires me to come up with 3 starting chords off the top of my head. This isn't too hard, I'll use an input pattern of: I, IV, V.

To be continued in the next part (As it will take me a few days to generate an mp3 for this).

Thanks for reading!

Shawn Clake

Freelance Developer

Software Engineering Student - U of R

Current: Assistant to Manager of Instructional Tech - U of R




Email 2:

Posted in Technology on Feb 14, 2017