{ "metadata": { "name": "", "signature": "sha256:3f785632d8399faeb298103405c9f9b70f1240eca928b05d3549f7900d5465fb" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Now, it's time to do some deep learning. Recall that a RBM (Restricted Boltzmann Machine) takes in a binary vector, and you can use it in the SKLearn Pipeline with a classifier. You'll want to read in Oscar's original data as an array of bitwise note vectors, and from there build the RBM to predict chords (the y's, perhaps build the chord bank and assign a unique number to each). After that, given a note vector (maybe plural?), you should be able to predict the chords for a note (notes?)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from collections import defaultdict\n", "from sklearn.neural_network import BernoulliRBM\n", "import pandas as pd\n", "import numpy as np" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 28 }, { "cell_type": "code", "collapsed": false, "input": [ "# Extract chords into unique ids, e.g. 1, 2, 3, 4, 5\n", "allchords = defaultdict()\n", "with open(\"oscar2chords_extract.txt\", 'rb') as f:\n", " assert len(allchords) == len(set(allchords)) # ensure no duplicate chords\n", " for ix, line in enumerate(f):\n", " allchords[ix] = line.rstrip()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "# Read in Oscar's data.\n", "vectors = []\n", "notedata = pd.read_csv(open(\"oscar2notes.txt\", 'rb'), skiprows=2)\n", "allnotes = []\n", "for note, octave in zip(notedata[\"Note/Rest\"], notedata[\"Octave\"]):\n", " allnotes.append(\"%s%s\" % (note, octave))\n", "print allnotes[:10]\n", "notedata.head()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['B3', 'A5', 'F4', 'G4', 'F4', 'B-5', 'B3', 'B-5', 'C4', 'C5']\n" ] }, { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Note/RestOctaveLenOffset
0 B 3 0.500000 12.625
1 A 5 0.250000 15.000
2 F 4 3.125000 16.000
3 G 4 0.666667 20.625
4 F 4 1.250000 23.875
\n", "

5 rows \u00d7 4 columns

\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 23, "text": [ " Note/Rest Octave Len Offset\n", "0 B 3 0.500000 12.625\n", "1 A 5 0.250000 15.000\n", "2 F 4 3.125000 16.000\n", "3 G 4 0.666667 20.625\n", "4 F 4 1.250000 23.875\n", "\n", "[5 rows x 4 columns]" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "# Given a MUSIC21 note, such as C5 or D#7, convert it\n", "# into a note on the keyboard between 0 and 87 inclusive.\n", "# Don't convert it for mingus; try to use music21 note style\n", "# as much as possible for all this stuff.\n", "def quantify(note):\n", " notevals = {\n", " 'C' : 0,\n", " 'D' : 2,\n", " 'E' : 4,\n", " 'F' : 5,\n", " 'G' : 7,\n", " 'A' : 9,\n", " 'B' : 11\n", " }\n", " quantized = 0\n", " octave = int(note[-1]) - 1\n", " for i in note[:-1]:\n", " if i in notevals: quantized += notevals[i]\n", " if i == '-': quantized -= 1\n", " if i == '#': quantized += 1\n", " quantized += 12 * octave\n", " return quantized\n", "\n", "vect1 = np.array((1, 88)).reshape(88, -1)\n", "note1 = allnotes[0]\n", "print quantify(note1), note1\n", "vect1[quantify(note1)] = 1\n", "print vect1" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "total size of new array must be unchanged", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 22\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mquantized\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 23\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 24\u001b[1;33m \u001b[0mvect1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m88\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m88\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 25\u001b[0m \u001b[0mnote1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mallnotes\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 26\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[0mquantify\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnote1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnote1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mValueError\u001b[0m: total size of new array must be unchanged" ] } ], "prompt_number": 35 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }