Created
April 10, 2015 15:04
-
-
Save bmcfee/ad323c86bb34d25fbacf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 15, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import numpy as np\n", | |
| "import theano.tensor as T\n", | |
| "import theano\n", | |
| "import lasagne\n", | |
| "try:\n", | |
| " from lasagne.layers.dnn import Conv2DDNNLayer as Conv2DLayer\n", | |
| " from lasagne.layers.dnn import MaxPool2DDNNLayer as MaxPool2DLayer\n", | |
| "except ImportError:\n", | |
| " from lasagne.layers import Conv2DLayer, MaxPool2DLayer" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 126, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Hyperparameters\n", | |
| "num_filters = [1, 1]\n", | |
| "\n", | |
| "# First layer is 8 frames wide and 12 semitones tall (for example)\n", | |
| "filter_size = [(4, 12), (4, 3)]\n", | |
| "\n", | |
| "# Pooling sizes\n", | |
| "ds = [(1, 4), (1, 4)]\n", | |
| "\n", | |
| "hidden_layer_sizes = [128, 128]\n", | |
| "\n", | |
| "output_dim = 3\n", | |
| "\n", | |
| "batch_size = 50\n", | |
| "\n", | |
| "sequence_length = 128\n", | |
| "\n", | |
| "learning_rate = 10**-3\n", | |
| "\n", | |
| "momentum = .5" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 141, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "num_channels = 1\n", | |
| "num_features = 216" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 142, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Create network, starting with input layer\n", | |
| "layers = [lasagne.layers.InputLayer(shape=(None,\n", | |
| " num_channels,\n", | |
| " None,\n", | |
| " num_features))]\n", | |
| "\n", | |
| "# Add each convolutional and pooling layer recursively\n", | |
| "for n in range(len(num_filters)):\n", | |
| " layers.append(Conv2DLayer(layers[-1],\n", | |
| " num_filters=num_filters[n],\n", | |
| " filter_size=filter_size[n],\n", | |
| " nonlinearity=lasagne.nonlinearities.rectify,\n", | |
| " border_mode='same'))\n", | |
| " \n", | |
| " layers.append(MaxPool2DLayer(layers[-1], ds[n]))\n", | |
| " \n", | |
| "# The next few lines basically take the time step dimension and squash it into\n", | |
| "# the first (batch) dimension, which is what we want for dense layers.\n", | |
| "layers.append(lasagne.layers.DimshuffleLayer(layers[-1], (0, 2, 1, 3)))\n", | |
| "\n", | |
| "conv_output_shape = layers[-1].get_output_shape()\n", | |
| "\n", | |
| "layers.append(lasagne.layers.ReshapeLayer(layers[-1],\n", | |
| " (-1, conv_output_shape[2] * conv_output_shape[3])))\n", | |
| "\n", | |
| "# Add dense hidden layers\n", | |
| "for hidden_layer_size in hidden_layer_sizes:\n", | |
| " layers.append(lasagne.layers.DenseLayer(layers[-1],\n", | |
| " num_units=hidden_layer_size,\n", | |
| " nonlinearity=lasagne.nonlinearities.rectify))\n", | |
| "\n", | |
| " # Add output layer (there are other nonlinearities depending on what you want)\n", | |
| "layers.append(lasagne.layers.DenseLayer(layers[-1], num_units=output_dim,\n", | |
| " nonlinearity=lasagne.nonlinearities.sigmoid))\n", | |
| "\n", | |
| "\n", | |
| "# Now, define a cost using theano... something like\n", | |
| "Y = T.imatrix('target')\n", | |
| "\n", | |
| "cost = T.nnet.binary_crossentropy(layers[-1].get_output(), Y).mean()\n", | |
| "\n", | |
| "# Compute updates, there's also SGD with momentum, Adagrad, etc.\n", | |
| "updates = lasagne.updates.rmsprop(cost,\n", | |
| " lasagne.layers.get_all_params(layers[-1]),\n", | |
| " learning_rate, momentum)\n", | |
| "\n", | |
| "# Compile theano functions for train/test\n", | |
| "# input_var is a theano variable automatically created by the InputLayer\n", | |
| "train = theano.function([layers[0].input_var, Y], cost, updates=updates)\n", | |
| "\n", | |
| "# Other useful functions\n", | |
| "compute_cost = theano.function([layers[0].input_var, Y], cost)\n", | |
| "\n", | |
| "output = theano.function([layers[0].input_var], layers[-1].get_output())" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 188, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Load in training data here as X_train; a list of np.ndarrays each with shape\n", | |
| "# (num_channels, num_frames, num_features)\n", | |
| "# num_channels is probably 1 in your case\n", | |
| "\n", | |
| "X_train = np.random.randn(*(5, num_channels, sequence_length, num_features))\n", | |
| "Y_train = np.random.randint(0, high=2, size=(len(X_train), X_train.shape[2], output_dim)).astype(np.int32)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 189, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "(5, 1, 128, 216) (5, 128, 3)\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print X_train.shape, Y_train.shape" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 190, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "ename": "TypeError", | |
| "evalue": "('Bad input argument to theano function with name \"<ipython-input-142-e5b726cf2525>:52\" at index 1(0-based)', 'Wrong number of dimensions: expected 2, got 3 with shape (5, 128, 3).')", | |
| "output_type": "error", | |
| "traceback": [ | |
| "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
| "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", | |
| "\u001b[1;32m<ipython-input-190-2f6a822651f9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mcompute_cost\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_train\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mY_train\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", | |
| "\u001b[1;32m/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.pyc\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 511\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 512\u001b[0m s.storage[0] = s.type.filter(arg, strict=s.strict,\n\u001b[1;32m--> 513\u001b[1;33m allow_downcast=s.allow_downcast)\n\u001b[0m\u001b[0;32m 514\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 515\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
| "\u001b[1;32m/usr/local/lib/python2.7/dist-packages/theano/tensor/type.pyc\u001b[0m in \u001b[0;36mfilter\u001b[1;34m(self, data, strict, allow_downcast)\u001b[0m\n\u001b[0;32m 167\u001b[0m raise TypeError(\"Wrong number of dimensions: expected %s,\"\n\u001b[0;32m 168\u001b[0m \" got %s with shape %s.\" % (self.ndim, data.ndim,\n\u001b[1;32m--> 169\u001b[1;33m data.shape))\n\u001b[0m\u001b[0;32m 170\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mflags\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0maligned\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 171\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
| "\u001b[1;31mTypeError\u001b[0m: ('Bad input argument to theano function with name \"<ipython-input-142-e5b726cf2525>:52\" at index 1(0-based)', 'Wrong number of dimensions: expected 2, got 3 with shape (5, 128, 3).')" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "compute_cost(X_train, Y_train)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 182, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "ename": "TypeError", | |
| "evalue": "('Bad input argument to theano function with name \"<ipython-input-142-e5b726cf2525>:49\" at index 1(0-based)', 'Wrong number of dimensions: expected 2, got 3 with shape (5, 128, 3).')", | |
| "output_type": "error", | |
| "traceback": [ | |
| "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
| "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", | |
| "\u001b[1;32m<ipython-input-182-9aa45b9a33d3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtrain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX_train\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mY_train\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", | |
| "\u001b[1;32m/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.pyc\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 511\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 512\u001b[0m s.storage[0] = s.type.filter(arg, strict=s.strict,\n\u001b[1;32m--> 513\u001b[1;33m allow_downcast=s.allow_downcast)\n\u001b[0m\u001b[0;32m 514\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 515\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
| "\u001b[1;32m/usr/local/lib/python2.7/dist-packages/theano/tensor/type.pyc\u001b[0m in \u001b[0;36mfilter\u001b[1;34m(self, data, strict, allow_downcast)\u001b[0m\n\u001b[0;32m 167\u001b[0m raise TypeError(\"Wrong number of dimensions: expected %s,\"\n\u001b[0;32m 168\u001b[0m \" got %s with shape %s.\" % (self.ndim, data.ndim,\n\u001b[1;32m--> 169\u001b[1;33m data.shape))\n\u001b[0m\u001b[0;32m 170\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mflags\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0maligned\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 171\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
| "\u001b[1;31mTypeError\u001b[0m: ('Bad input argument to theano function with name \"<ipython-input-142-e5b726cf2525>:49\" at index 1(0-based)', 'Wrong number of dimensions: expected 2, got 3 with shape (5, 128, 3).')" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "train(X_train, Y_train)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 183, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "5 (1, 128, 216)\n", | |
| "[(128, 3), (128, 3), (128, 3), (128, 3), (128, 3)]\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print len(X_train), X_train[-1].shape\n", | |
| "print [output([x]).shape for x in X_train]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 161, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(1, 128, 216)" | |
| ] | |
| }, | |
| "execution_count": 161, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "X_train[-1].shape" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 159, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(128, 3)" | |
| ] | |
| }, | |
| "execution_count": 159, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "output(X_train[-1:]).shape" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "---" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 174, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "X_train = np.asarray([np.random.randn(*(num_channels, sequence_length*3, num_features)) for _ in range(5)])\n", | |
| "Y_train = np.random.randint(0, high=2, size=(len(X_train), X_train.shape[2], output_dim)).astype(np.int32)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 175, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "5 (1, 384, 216)\n", | |
| "[(384, 3), (384, 3), (384, 3), (384, 3), (384, 3)]\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print len(X_train), X_train[-1].shape\n", | |
| "print [output([x]).shape for x in X_train]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 176, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(5, 1, 384, 216)" | |
| ] | |
| }, | |
| "execution_count": 176, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "X_train.shape" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 177, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(5, 384, 3)" | |
| ] | |
| }, | |
| "execution_count": 177, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "Y_train.shape" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 179, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(384, 3)" | |
| ] | |
| }, | |
| "execution_count": 179, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "output(X_train[-1:]).shape" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 2", | |
| "language": "python", | |
| "name": "python2" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 2 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython2", | |
| "version": "2.7.8" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment