Package: bach-generator
Bach music generatorA Bach music generator using a neural network trained with a genetic algorithm.MotivationThe neural network was setup and trained from scratch. The main motivation behind this was learning how one might code a neural network without using a library abstracting away all the details.
It's obviously not very efficient, because it is not optimized for speed and doesn't use the GPU (so if you want to run this, I hope you have a fast CPU), but this doesn't matter, as speed is not within the scope of this experiment.Getting startedInstall the package using pip, then run it with no arguments to open the graphical user interface:python -m pip install bach_generator
python -m bach_generatorBypass the graphical user interface by calling the package and passing a midi filepath to it:python -m bach_generator <midi_filepath>For more CLI options, please see theCLI documentationor run the help command:python -m bach_generator -hAlgorithmAt the start, several separate neural networks are instantiated and fed the input data. The top ranked models (based on the correlation of their output to the input) get cloned into the next generation with randomly adjusted weights. This process is repeated over a number of generations.Currently, the neural network iterates through all notes in a given midi file, feeding the neural network the current note and a calibratable number of past notes. Each node in the model is
connected to all nodes from the previous network. When inputs are fed in to the model, they arrive at the input layer, get multiplied by the weights of the input nodes, then get propagated to
the next layer. Each node in the middle layers averages all values it receives, then weights that value and propagates it further.This mixture of past and present inputs makes this stateless model behave as if it was stateful, e.g. like a recurrent neural network (RNN).Finally, the output values are received, decoded from numbers into notes and written to an output midi file.Currently the node decoding happens using a simple frequency ranking analogue to the input note frequency. The rhythms in the input midi file are retained in the output.Training dataTraining data was downloaded fromwww.jsbach.netand is not included in this repository.A pre-trained object model (reaching 78% correlation for the entire Goldberg variations BWV 988, with over 90% correlation for some variations) can be foundhere. Load and run further simulations with it by using the CLI--loadflag:python -m bach_generator --load=models/models.json --layer-type=object <midi_filepath>Command line interfaceThe Bach generator package supports a number of command line interface arguments, including handling output, model save/load, and parameters regarding model size and cloning.A brief overview of the CLI is shown below:usage: __main__.py [-h] [--generations GENERATIONS] [--save]
                   [--load LOAD_FILEPATH] [--load-best LOAD_BEST]
                   [--models MODELS] [--inputs INPUTS] [--layers LAYERS]
                   [--layer-size LAYER_SIZE] [--select-models SELECT_MODELS]
                   [--clones CLONES] [--weight-jumble-by {offset,selection}]
                   [--weight-divergence WEIGHT_DIVERGENCE]
                   [--write-interval WRITE_INTERVAL] [--output-dir OUTPUT_DIR]
                   [--seed SEED]
                   filepath

positional arguments:
  filepath              The filepath to the midi file to be analysed

optional arguments:
  -h, --help            show this help message and exitTo see the full CLI documentation, run the CLI help command:python -m bach_generator -hRunning the package without specifying any CLI arguments withpython -m bach_generatorinstead opens the graphical user interface:ContributionsTo contribute, please read thecontribution guidelines.PythonWritten in Python 3.8.8.LicenseThis repository is open-source software available under theMIT License.ContactPlease raise an issue for code changes. To reach out, please send an email torichard@baltrusch.net.