Cifar

imports


In [1]:
import pandas as pd
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from theano.tensor.nnet import softmax
from skimage.color import rgb2gray

In [2]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [54]:
import nolearn
reload(nolearn)
from nolearn.nntools import NeuralNet
from nolearn.nntools import BatchIterator
from nolearn.nntools import FlipBatchIterator
from nolearn.nntools import LabelEncoder
from nntools import layers
import visualize
reload(visualize)
from visualize import PlotLoss
from visualize import PlotWeights
from visualize import PlotWeightChanges

load and prepare data

Use Cifar-10 images as provided by kaggle: http://www.kaggle.com/c/cifar-10/data


In [4]:
filepath = '../data/cifar10-train/'  # filepath of images
filelabels = '../data/cifar10-trainLabels.csv'

In [5]:
df_labels = pd.read_csv(filelabels, index_col=0)

In [6]:
df_labels['idx'] = np.array(df_labels.index) - 1
df_labels.set_index('idx', drop=True, inplace=True)

In [7]:
encoder = LabelEncoder()

In [8]:
def load_images(n=50000, grayscale=True):
    X = np.zeros((n, 32, 32))
    for i in range(n):
        img = Image.open('{}{}.png'.format(filepath, i + 1))
        if grayscale:
            X[i] = rgb2gray(np.array(img))
        else:
            X[i] = np.array(img)
    return X

In [9]:
%time X = load_images()


CPU times: user 13 s, sys: 659 ms, total: 13.6 s
Wall time: 13.6 s

In [10]:
y = encoder.fit_transform(df_labels.values.reshape(-1))

In [11]:
X = X.astype(np.float32)

In [12]:
# use flat images for fully connected layers
X_flat = X.reshape(-1, 32 * 32)

In [13]:
# reshape X to be bc01 compliant
Xr = X.reshape(X.shape[0], 1, X.shape[1], X.shape[2])

Show some images


In [13]:
def plot_cifar(data, label=None):
    plt.figure(figsize=(2, 2))
    plt.imshow(data, cmap=plt.get_cmap('gray'), interpolation='bessel')
    if label is None:
        plt.title(df_labels.ix[i])
    else:
        if not isinstance(label, tuple):
            plt.title(label)
        else:
            plt.title("pred: {}, true: {}".format(*label))
    plt.xticks([])
    plt.yticks([])

In [14]:
for i in range(5):
    plot_cifar(X[i])
    plt.show()


Fully connected neural network

Initialize network


In [14]:
%pylab qt


Populating the interactive namespace from numpy and matplotlib

In [15]:
BATCHSIZE = 128

In [16]:
the_layers = [
    ('input', layers.InputLayer),
    ('hidden1', layers.DenseLayer),
    ('hidden2', layers.DenseLayer),
    ('output', layers.DenseLayer),
]

fit

Here visualize the progress of the loss function using PlotLoss

In [44]:
nn = NeuralNet(
    layers=the_layers,
    update_learning_rate=0.02,
    update_momentum=0.9,
    input_shape=(BATCHSIZE, X.shape[1] ** 2),
    hidden1_num_units=200,
    hidden2_num_units=200,
    output_num_units=10,
    output_nonlinearity=softmax,
    max_epochs=5,
    verbose=1,
    on_epoch_finished=PlotLoss(update_every=1),
)

In [45]:
%time nn.fit(X_flat, y)


  InputLayer        	(128, 1024)         	produces    1024 outputs
  DenseLayer        	(128, 200)          	produces     200 outputs
  DenseLayer        	(128, 200)          	produces     200 outputs
  DenseLayer        	(128, 10)           	produces      10 outputs
Epoch   1 of 5	(6.24 sec)
  training loss:        2.026985
  validation loss:      1.909388
  validation accuracy:    32.81% !!!

Epoch   2 of 5	(6.06 sec)
  training loss:        1.865886
  validation loss:      1.839246
  validation accuracy:    35.01% !!!

Epoch   3 of 5	(5.82 sec)
  training loss:        1.799596
  validation loss:      1.784848
  validation accuracy:    37.00% !!!

Epoch   4 of 5	(5.89 sec)
  training loss:        1.750677
  validation loss:      1.761356
  validation accuracy:    38.18% !!!

Epoch   5 of 5	(5.69 sec)
  training loss:        1.708917
  validation loss:      1.733961
  validation accuracy:    39.30% !!!

CPU times: user 29.8 s, sys: 1.55 s, total: 31.4 s
Wall time: 31.4 s
Out[45]:
NeuralNet(X_tensor_type=<function matrix at 0x7ffbdb682a28>,
     batch_iterator=<nolearn.nntools.BatchIterator object at 0x7ffbd926aad0>,
     eval_size=0.2, hidden1_num_units=200, hidden2_num_units=200,
     input_shape=(128, 1024),
     layers=[('input', <class 'nntools.layers.base.InputLayer'>), ('hidden1', <class 'nntools.layers.base.DenseLayer'>), ('hidden2', <class 'nntools.layers.base.DenseLayer'>), ('output', <class 'nntools.layers.base.DenseLayer'>)],
     loss=<function negative_log_likelihood at 0x7ffbd9251e60>,
     max_epochs=5, more_params={},
     on_epoch_finished=<visualize.PlotLoss object at 0x7ffbd118e290>,
     on_training_finished=None,
     output_nonlinearity=<theano.tensor.nnet.nnet.Softmax object at 0x7ffbdb2c6c50>,
     output_num_units=10,
     update=<function nesterov_momentum at 0x7ffbd9251c80>,
     update_learning_rate=0.02, update_momentum=0.9, verbose=1)
Here visualize the change in weights using PlotWeightChanges

In [55]:
nn = NeuralNet(
    layers=the_layers,
    update_learning_rate=0.02,
    update_momentum=0.9,
    input_shape=(BATCHSIZE, X.shape[1] ** 2),
    hidden1_num_units=200,
    hidden2_num_units=200,
    output_num_units=10,
    output_nonlinearity=softmax,
    max_epochs=5,
    verbose=1,
    on_epoch_finished=PlotWeightChanges(update_every=1),
)

In [56]:
%time nn.fit(X_flat, y)


  InputLayer        	(128, 1024)         	produces    1024 outputs
  DenseLayer        	(128, 200)          	produces     200 outputs
  DenseLayer        	(128, 200)          	produces     200 outputs
  DenseLayer        	(128, 10)           	produces      10 outputs
Epoch   1 of 5	(5.95 sec)
  training loss:        2.020911
  validation loss:      1.902772
  validation accuracy:    32.31% !!!

Epoch   2 of 5	(5.98 sec)
  training loss:        1.857592
  validation loss:      1.820374
  validation accuracy:    34.96% !!!

Epoch   3 of 5	(5.77 sec)
  training loss:        1.790426
  validation loss:      1.779298
  validation accuracy:    36.61% !!!

Epoch   4 of 5	(5.77 sec)
  training loss:        1.739517
  validation loss:      1.761547
  validation accuracy:    37.53% !!!

Epoch   5 of 5	(5.75 sec)
  training loss:        1.698315
  validation loss:      1.704730
  validation accuracy:    40.16% !!!

CPU times: user 29.5 s, sys: 1.47 s, total: 31 s
Wall time: 31 s
Out[56]:
NeuralNet(X_tensor_type=<function matrix at 0x7ffbdb682a28>,
     batch_iterator=<nolearn.nntools.BatchIterator object at 0x7ffbd926aad0>,
     eval_size=0.2, hidden1_num_units=200, hidden2_num_units=200,
     input_shape=(128, 1024),
     layers=[('input', <class 'nntools.layers.base.InputLayer'>), ('hidden1', <class 'nntools.layers.base.DenseLayer'>), ('hidden2', <class 'nntools.layers.base.DenseLayer'>), ('output', <class 'nntools.layers.base.DenseLayer'>)],
     loss=<function negative_log_likelihood at 0x7ffbd9251e60>,
     max_epochs=5, more_params={},
     on_epoch_finished=<visualize.PlotWeightChanges object at 0x7ffbd10f01d0>,
     on_training_finished=None,
     output_nonlinearity=<theano.tensor.nnet.nnet.Softmax object at 0x7ffbdb2c6c50>,
     output_num_units=10,
     update=<function nesterov_momentum at 0x7ffbd9251c80>,
     update_learning_rate=0.02, update_momentum=0.9, verbose=1)
Here visualize the first hidden layer by using PlotWeights

nrows x ncols will be the number of visualized units shown in a grid. figsize is the size of the figure


In [18]:
nn = NeuralNet(
    layers=the_layers,
    update_learning_rate=0.02,
    update_momentum=0.9,
    input_shape=(BATCHSIZE, X.shape[1] ** 2),
    hidden1_num_units=200,
    hidden2_num_units=200,
    output_num_units=10,
    output_nonlinearity=softmax,
    max_epochs=5,
    verbose=1,
    on_epoch_finished=PlotWeights(nrows=10, ncols=10, figsize=(10, 10)),
)

In [19]:
%time nn.fit(X_flat, y)


  InputLayer        	(128, 1024)         	produces    1024 outputs
  DenseLayer        	(128, 200)          	produces     200 outputs
  DenseLayer        	(128, 200)          	produces     200 outputs
  DenseLayer        	(128, 10)           	produces      10 outputs
Epoch   1 of 5	(6.03 sec)
  training loss:        2.024346
  validation loss:      1.898514
  validation accuracy:    33.60% !!!

Epoch   2 of 5	(5.97 sec)
  training loss:        1.862655
  validation loss:      1.809481
  validation accuracy:    35.67% !!!

Epoch   3 of 5	(5.80 sec)
  training loss:        1.793713
  validation loss:      1.782977
  validation accuracy:    36.52% !!!

Epoch   4 of 5	(5.72 sec)
  training loss:        1.746586
  validation loss:      1.753584
  validation accuracy:    38.02% !!!

Epoch   5 of 5	(5.76 sec)
  training loss:        1.704998
  validation loss:      1.732696
  validation accuracy:    38.76% !!!

CPU times: user 37.2 s, sys: 2.11 s, total: 39.3 s
Wall time: 39.5 s
/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/matplotlib/backend_bases.py:2380: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented
  warnings.warn(str, mplDeprecation)
Out[19]:
NeuralNet(X_tensor_type=<function matrix at 0x7fd4c7313a28>,
     batch_iterator=<nolearn.nntools.BatchIterator object at 0x7fd4c4efbad0>,
     eval_size=0.2, hidden1_num_units=200, hidden2_num_units=200,
     input_shape=(128, 1024),
     layers=[('input', <class 'nntools.layers.base.InputLayer'>), ('hidden1', <class 'nntools.layers.base.DenseLayer'>), ('hidden2', <class 'nntools.layers.base.DenseLayer'>), ('output', <class 'nntools.layers.base.DenseLayer'>)],
     loss=<function negative_log_likelihood at 0x7fd4c4ee1e60>,
     max_epochs=5, more_params={},
     on_epoch_finished=<visualize.PlotWeights object at 0x7fd4c4bf38d0>,
     on_training_finished=None,
     output_nonlinearity=<theano.tensor.nnet.nnet.Softmax object at 0x7fd4c6f57c50>,
     output_num_units=10,
     update=<function nesterov_momentum at 0x7fd4c4ee1c80>,
     update_learning_rate=0.02, update_momentum=0.9, verbose=1)

In [20]:
plt.close()

conv2d

flat convolutional net


In [25]:
%pylab qt


Populating the interactive namespace from numpy and matplotlib

In [21]:
BATCHSIZE = 100

In [22]:
the_layers = [
    ('input', layers.InputLayer),
    ('dropout1', layers.DropoutLayer),
    ('conv2d', layers.Conv2DLayer),
    ('dropout2', layers.DropoutLayer),
    ('hidden1', layers.DenseLayer),
    ('output', layers.DenseLayer),
]

In [23]:
nn = NeuralNet(
    layers=the_layers,
    update_learning_rate=0.02,
    update_momentum=0.9,
    batch_iterator=BatchIterator(BATCHSIZE),
    input_shape=(BATCHSIZE, Xr.shape[1], Xr.shape[2], Xr.shape[3]),
    hidden1_num_units=100,
    conv2d_num_filters=9,
    conv2d_filter_size=(8, 8),
    output_num_units=10,
    output_nonlinearity=softmax,
    max_epochs=5,
    verbose=1,
    on_epoch_finished=PlotWeights(nrows=3, ncols=3, figsize=(3, 3)),
)

In [24]:
%time nn.fit(Xr, y)


  InputLayer        	(100, 1, 32, 32)    	produces    1024 outputs
  DropoutLayer      	(100, 1, 32, 32)    	produces    1024 outputs
  Conv2DLayer       	(100, 9, 25, 25)    	produces    5625 outputs
  DropoutLayer      	(100, 9, 25, 25)    	produces    5625 outputs
  DenseLayer        	(100, 100)          	produces     100 outputs
  DenseLayer        	(100, 10)           	produces      10 outputs
Epoch   1 of 5	(44.71 sec)
  training loss:        2.289783
  validation loss:      2.138076
  validation accuracy:    23.74% !!!

Epoch   2 of 5	(44.38 sec)
  training loss:        2.061444
  validation loss:      1.924396
  validation accuracy:    30.97% !!!

Epoch   3 of 5	(44.13 sec)
  training loss:        1.966302
  validation loss:      1.854174
  validation accuracy:    33.72% !!!

---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-24-8e5515b47f6b> in <module>()
----> 1 get_ipython().magic(u'time nn.fit(Xr, y)')

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s)
   2203         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2204         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2205         return self.run_line_magic(magic_name, magic_arg_s)
   2206 
   2207     #-------------------------------------------------------------------------

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2124                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2125             with self.builtin_trap:
-> 2126                 result = fn(*args,**kwargs)
   2127             return result
   2128 

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/IPython/core/magics/execution.pyc in time(self, line, cell, local_ns)

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/IPython/core/magics/execution.pyc in time(self, line, cell, local_ns)
   1123         if mode=='eval':
   1124             st = clock2()
-> 1125             out = eval(code, glob, local_ns)
   1126             end = clock2()
   1127         else:

<timed eval> in <module>()

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/nolearn-0.5b2dev-py2.7.egg/nolearn/nntools.pyc in fit(self, X, y)
    115         self.train_iter_, self.eval_iter_, self.predict_iter_ = iter_funcs
    116 
--> 117         self.train_loop(X, y)
    118         return self
    119 

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/nolearn-0.5b2dev-py2.7.egg/nolearn/nntools.pyc in train_loop(self, X, y)
    138 
    139             for Xb, yb in self.batch_iterator(X_train, y_train):
--> 140                 batch_train_loss = self.train_iter_(Xb, yb)
    141                 train_losses.append(batch_train_loss)
    142 

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/compile/function_module.pyc in __call__(self, *args, **kwargs)
    593         t0_fn = time.time()
    594         try:
--> 595             outputs = self.fn()
    596         except Exception:
    597             if hasattr(self.fn, 'position_of_error'):

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/gof/op.pyc in rval(p, i, o, n)
    750 
    751         def rval(p=p, i=node_input_storage, o=node_output_storage, n=node):
--> 752             r = p(n, [x[0] for x in i], o)
    753             for o in node.outputs:
    754                 compute_map[o][0] = True

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/tensor/blas.pyc in perform(self, node, inp, out)
   1881         z, = out
   1882         try:
-> 1883             z[0] = numpy.asarray(scalar * numpy.dot(x, y))
   1884         except ValueError, e:
   1885             # The error raised by numpy has no shape information, we

KeyboardInterrupt: 

deeper net


In [39]:
the_layers = [
    ('input', layers.InputLayer),
    ('conv2d1', layers.Conv2DLayer),
    ('maxpool1', layers.MaxPool2DLayer),
    ('conv2d2', layers.Conv2DLayer),
    ('maxpool2', layers.MaxPool2DLayer),
    ('dropout1', layers.DropoutLayer),
    ('hidden1', layers.DenseLayer),
    ('output', layers.DenseLayer),
]

In [76]:
nn = NeuralNet(
    layers=the_layers,
    update_learning_rate=0.02,
    update_momentum=0.9,
    batch_iterator=BatchIterator(BATCHSIZE),
    input_shape=(BATCHSIZE, Xr.shape[1], Xr.shape[2], Xr.shape[3]),
    conv2d1_num_filters=25,
    conv2d1_filter_size=(9, 9),
    maxpool1_ds=(2, 2),
    conv2d2_num_filters=16,
    conv2d2_filter_size=(8, 8),
    maxpool2_ds=(2, 2),
    hidden1_num_units=100,
    output_num_units=10,
    output_nonlinearity=softmax,
    max_epochs=25,
    verbose=1,
    on_epoch_finished=PlotWeights(nrows=4, ncols=4, figsize=(4, 4),
                                  vis_layer='conv2d2', nchannel=24),
)

You can also visualize a specific layer by indicating the vis_layer argument as the layer's name. Furthermore, indicate which channel you like to visualize by giving nchannel.


In [63]:
%time nn.fit(Xr, y)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-63-8e5515b47f6b> in <module>()
----> 1 get_ipython().magic(u'time nn.fit(Xr, y)')

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s)
   2203         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2204         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2205         return self.run_line_magic(magic_name, magic_arg_s)
   2206 
   2207     #-------------------------------------------------------------------------

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2124                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2125             with self.builtin_trap:
-> 2126                 result = fn(*args,**kwargs)
   2127             return result
   2128 

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/IPython/core/magics/execution.pyc in time(self, line, cell, local_ns)

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/IPython/core/magics/execution.pyc in time(self, line, cell, local_ns)
   1123         if mode=='eval':
   1124             st = clock2()
-> 1125             out = eval(code, glob, local_ns)
   1126             end = clock2()
   1127         else:

<timed eval> in <module>()

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/nolearn-0.5b2dev-py2.7.egg/nolearn/nntools.pyc in fit(self, X, y)
    107         self.classes_ = self.enc_.classes_
    108 
--> 109         out = self.output_layer_ = self._initialize_layers(self.layers)
    110         if self.verbose:
    111             self._print_layer_info(self.get_all_layers())

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/nolearn-0.5b2dev-py2.7.egg/nolearn/nntools.pyc in _initialize_layers(self, layer_types)
    272         for (layer_name, layer_factory) in layer_types[1:]:
    273             layer_params = self._get_params_for(layer_name)
--> 274             layer = layer_factory(layer, **layer_params)
    275 
    276         return layer

TypeError: __init__() takes at least 4 arguments (2 given)
> /home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/nolearn-0.5b2dev-py2.7.egg/nolearn/nntools.py(274)_initialize_layers()
    273             layer_params = self._get_params_for(layer_name)
--> 274             layer = layer_factory(layer, **layer_params)
    275 

ipdb> layer_name
'conv2d1'
ipdb> q

In [32]:
%pdb on


Automatic pdb calling has been turned ON

cuda conv2d


In [34]:
BATCHSIZE = 128

In [37]:
the_layers = [
    ('input', layers.InputLayer),
    ('conv1', layers.cuda_convnet.Conv2DCCLayer),
    ('maxpool1', layers.cuda_convnet.MaxPool2DCCLayer),
    ('conv2', layers.cuda_convnet.Conv2DCCLayer),
    ('maxpool2', layers.cuda_convnet.MaxPool2DCCLayer),
    ('hidden1', layers.DenseLayer),
]

In [38]:
nn = NeuralNet(
    layers=the_layers,
    update_learning_rate=0.02,
    update_momentum=0.9,
    batch_iterator=BatchIterator(BATCHSIZE),
    input_shape=(BATCHSIZE, Xr.shape[1], Xr.shape[2], Xr.shape[3]),
    conv1_num_filters=32,
    conv1_filter_size=(8, 8),
    maxpool1_ds=(2, 2),
    conv2_num_filters=16,
    conv2_filter_size=(8, 8),
    maxpool2_ds=(2, 2),
    hidden1_num_units=100,
    output_num_units=10,
    output_nonlinearity=softmax,
    max_epochs=5,
    verbose=1,
    # on_epoch_finished=PlotLoss(update_every=1),
    on_epoch_finished=PlotWeights(nrows=2, ncols=2, figsize=(6, 6)),
)

In [127]:
%time nn.fit(Xr, y)


  InputLayer        	(123, 1, 32, 32)    	produces    1024 outputs
  Conv2DCCLayer     	(123, 32, 25, 25)   	produces   20000 outputs
  MaxPool2DCCLayer  	(123, 32, 13, 13)   	produces    5408 outputs
  Conv2DCCLayer     	(123, 16, 6, 6)     	produces     576 outputs
  MaxPool2DCCLayer  	(123, 16, 3, 3)     	produces     144 outputs
  DenseLayer        	(123, 100)          	produces     100 outputs
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-127-395a6f939758> in <module>()
----> 1 get_ipython().magic(u'time nn.fit(floatX(Xr), y.astype(np.int32))')

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s)
   2203         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2204         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2205         return self.run_line_magic(magic_name, magic_arg_s)
   2206 
   2207     #-------------------------------------------------------------------------

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2124                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2125             with self.builtin_trap:
-> 2126                 result = fn(*args,**kwargs)
   2127             return result
   2128 

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/IPython/core/magics/execution.pyc in time(self, line, cell, local_ns)

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/IPython/core/magics/execution.pyc in time(self, line, cell, local_ns)
   1123         if mode=='eval':
   1124             st = clock2()
-> 1125             out = eval(code, glob, local_ns)
   1126             end = clock2()
   1127         else:

<timed eval> in <module>()

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/nolearn-0.5b2dev-py2.7.egg/nolearn/nntools.pyc in fit(self, X, y)
    112 
    113         iter_funcs = self._create_iter_funcs(
--> 114             out, self.loss, self.update, self.X_tensor_type)
    115         self.train_iter_, self.eval_iter_, self.predict_iter_ = iter_funcs
    116 

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/nolearn-0.5b2dev-py2.7.egg/nolearn/nntools.pyc in _create_iter_funcs(self, output_layer, loss_func, update, input_type)
    214 
    215         loss_train = loss_func(
--> 216             output_layer.get_output(X_batch), y_batch)
    217         loss_eval = loss_func(
    218             output_layer.get_output(X_batch, deterministic=True), y_batch)

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/nntools-0.1dev-py2.7.egg/nntools/layers/base.pyc in get_output(self, input, *args, **kwargs)
     97             return input[self] # this layer is mapped to an expression
     98         else: # in all other cases, just pass the network input on to the next layer.
---> 99             layer_input = self.input_layer.get_output(input, *args, **kwargs)
    100             return self.get_output_for(layer_input, *args, **kwargs)
    101 

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/nntools-0.1dev-py2.7.egg/nntools/layers/base.pyc in get_output(self, input, *args, **kwargs)
     97             return input[self] # this layer is mapped to an expression
     98         else: # in all other cases, just pass the network input on to the next layer.
---> 99             layer_input = self.input_layer.get_output(input, *args, **kwargs)
    100             return self.get_output_for(layer_input, *args, **kwargs)
    101 

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/nntools-0.1dev-py2.7.egg/nntools/layers/base.pyc in get_output(self, input, *args, **kwargs)
     97             return input[self] # this layer is mapped to an expression
     98         else: # in all other cases, just pass the network input on to the next layer.
---> 99             layer_input = self.input_layer.get_output(input, *args, **kwargs)
    100             return self.get_output_for(layer_input, *args, **kwargs)
    101 

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/nntools-0.1dev-py2.7.egg/nntools/layers/base.pyc in get_output(self, input, *args, **kwargs)
     97             return input[self] # this layer is mapped to an expression
     98         else: # in all other cases, just pass the network input on to the next layer.
---> 99             layer_input = self.input_layer.get_output(input, *args, **kwargs)
    100             return self.get_output_for(layer_input, *args, **kwargs)
    101 

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/nntools-0.1dev-py2.7.egg/nntools/layers/base.pyc in get_output(self, input, *args, **kwargs)
     98         else: # in all other cases, just pass the network input on to the next layer.
     99             layer_input = self.input_layer.get_output(input, *args, **kwargs)
--> 100             return self.get_output_for(layer_input, *args, **kwargs)
    101 
    102     def get_output_shape_for(self, input_shape):

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/nntools-0.1dev-py2.7.egg/nntools/layers/cuda_convnet.pyc in get_output_for(self, input, *args, **kwargs)
    118             filters = filters[:, ::-1, ::-1, :] # flip width, height
    119 
--> 120         contiguous_filters = gpu_contiguous(filters)
    121         contiguous_input = gpu_contiguous(input)
    122         conved = self.filter_acts_op(contiguous_input, contiguous_filters)

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/gof/op.pyc in __call__(self, *inputs, **kwargs)
    490         """
    491         return_list = kwargs.pop('return_list', False)
--> 492         node = self.make_node(*inputs, **kwargs)
    493         if self.add_stack_trace_on_call:
    494             self.add_tag_trace(node)

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/sandbox/cuda/basic_ops.pyc in make_node(self, input)
   3361 
   3362     def make_node(self, input):
-> 3363         input = as_cuda_ndarray_variable(input)
   3364         return Apply(self, [input], [input.type()])
   3365 

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/sandbox/cuda/basic_ops.pyc in as_cuda_ndarray_variable(x)
     39         return x._as_CudaNdarrayVariable()
     40     tensor_x = tensor.as_tensor_variable(x)
---> 41     return gpu_from_host(tensor_x)
     42 
     43 

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/gof/op.pyc in __call__(self, *inputs, **kwargs)
    490         """
    491         return_list = kwargs.pop('return_list', False)
--> 492         node = self.make_node(*inputs, **kwargs)
    493         if self.add_stack_trace_on_call:
    494             self.add_tag_trace(node)

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/sandbox/cuda/basic_ops.pyc in make_node(self, x)
    131                                                                  x.type))
    132         return Apply(self, [x], [CudaNdarrayType(broadcastable=x.broadcastable,
--> 133                                                  dtype=x.dtype)()])
    134 
    135     def perform(self, node, inp, out):

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/sandbox/cuda/type.pyc in __init__(self, broadcastable, name, dtype)
     68             raise TypeError('%s only supports dtype float32 for now. Tried '
     69                             'using dtype %s for variable %s' %
---> 70                             (self.__class__.__name__, dtype, name))
     71         self.broadcastable = tuple(broadcastable)
     72         self.name = name

TypeError: CudaNdarrayType only supports dtype float32 for now. Tried using dtype float64 for variable None
> /home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/sandbox/cuda/type.py(70)__init__()
     69                             'using dtype %s for variable %s' %
---> 70                             (self.__class__.__name__, dtype, name))
     71         self.broadcastable = tuple(broadcastable)

ipdb> q

In [44]:
%pdb on


Automatic pdb calling has been turned ON

conv2d + max pooling


In [109]:
the_layers = [
    ('input', layers.InputLayer),
    ('conv2d', layers.Conv1DLayer),
    ('maxpool2d', layers.MaxPool2DLayer),
    ('hidden1', layers.DenseLayer),
    ('output', layers.DenseLayer),
]

In [32]:
nn = NeuralNet(
    layers=the_layers,
    update_learning_rate=0.02,
    update_momentum=0.9,
    update_weight_decay=0.001,
    input_shape=(BATCHSIZE, X.shape[1] ** 2),
    hidden1_num_units=512,
    conv2d_num_filter=4,
    conv2d_filter_size=(4, 4),
    maxpool2d_ds=(2, 2),
    output_num_units=10,
    output_nonlinearity=softmax,
    max_epochs=5,
    verbose=1,
    # on_epoch_finished=PlotLoss(update_every=1),
    on_epoch_finished=PlotWeights(nrows=10, ncols=10, figsize=(10, 10)),
)

In [ ]:
%time nn.fit(X_flat, y)


  InputLayer        	(128, 1024)         	produces    1024 outputs
  DenseLayer        	(128, 512)          	produces     512 outputs
  DenseLayer        	(128, 512)          	produces     512 outputs
  DenseLayer        	(128, 10)           	produces      10 outputs
Epoch   1 of 10	(18.96 sec)
  training loss:        1.994960
  validation loss:      1.868434
  validation accuracy:    34.69% !!!

Epoch   2 of 10	(19.77 sec)

Analysis


In [162]:
preds = encoder.inverse_transform(nn.predict(Xr[:1000]))
trues = list(df_labels.values.reshape(-1))

In [157]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [158]:
ranges = range(10, 15)
for i in ranges:
    plot_cifar(Xr[i], (preds[i], trues[i]))


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-158-cfa41a1bcbcc> in <module>()
      1 ranges = range(10, 15)
      2 for i in ranges:
----> 3     plot_cifar(Xr[i], (preds[i], trues[i]))

<ipython-input-151-ad627c942272> in plot_cifar(data, label)
      1 def plot_cifar(data, label=None):
      2     plt.figure(figsize=fsize)
----> 3     plt.imshow(data, cmap=plt.get_cmap('gray'), interpolation='bessel')
      4     if label is None:
      5         plt.title(df_labels.ix[i])

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/matplotlib/pyplot.pyc in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, hold, **kwargs)
   2953                         vmax=vmax, origin=origin, extent=extent, shape=shape,
   2954                         filternorm=filternorm, filterrad=filterrad,
-> 2955                         imlim=imlim, resample=resample, url=url, **kwargs)
   2956         draw_if_interactive()
   2957     finally:

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
   4628                        filterrad=filterrad, resample=resample, **kwargs)
   4629 
-> 4630         im.set_data(X)
   4631         im.set_alpha(alpha)
   4632         if im.get_clip_path() is None:

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/matplotlib/image.pyc in set_data(self, A)
    432         if (self._A.ndim not in (2, 3) or
    433             (self._A.ndim == 3 and self._A.shape[-1] not in (3, 4))):
--> 434             raise TypeError("Invalid dimensions for image data")
    435 
    436         self._imcache = None

TypeError: Invalid dimensions for image data
> /home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/matplotlib/image.py(434)set_data()
    433             (self._A.ndim == 3 and self._A.shape[-1] not in (3, 4))):
--> 434             raise TypeError("Invalid dimensions for image data")
    435 

ipdb> q

In [160]:
def plot_confusion_matrix(y_true, y_pred, encoder, figsize=(8, 8), colors='Blues'):
    n_labels = len(np.unique(y_true))
    fig, ax = plt.subplots(figsize=figsize)
    ax.imshow(confusion_matrix(trues, preds), interpolation='nearest', cmap=plt.get_cmap(colors))
    ax.grid(False)
    labels = encoder.inverse_transform(range(10))
    plt.xticks(range(n_labels), labels, rotation='vertical')
    plt.yticks(range(n_labels), labels)
    plt.title('confusion matrix')
    return fig, ax

In [164]:
plot_confusion_matrix(trues, preds, encoder)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-164-c0ef0ac4e545> in <module>()
----> 1 plot_confusion_matrix(trues[:1000], preds[:1000], encoder)

<ipython-input-160-9afb7561c79e> in plot_confusion_matrix(y_true, y_pred, encoder, figsize, colors)
      2     n_labels = len(np.unique(y_true))
      3     fig, ax = plt.subplots(figsize=figsize)
----> 4     ax.imshow(confusion_matrix(trues, preds), interpolation='nearest', cmap=plt.get_cmap(colors))
      5     ax.grid(False)
      6     labels = encoder.inverse_transform(range(10))

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/sklearn/metrics/metrics.pyc in confusion_matrix(y_true, y_pred, labels)
    957 
    958     """
--> 959     y_type, y_true, y_pred = _check_clf_targets(y_true, y_pred)
    960     if y_type not in ("binary", "multiclass"):
    961         raise ValueError("%s is not supported" % y_type)

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/sklearn/metrics/metrics.pyc in _check_clf_targets(y_true, y_pred)
    107     y_pred : array or indicator matrix
    108     """
--> 109     y_true, y_pred = check_arrays(y_true, y_pred, allow_lists=True)
    110     type_true = type_of_target(y_true)
    111     type_pred = type_of_target(y_pred)

/home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/sklearn/utils/validation.pyc in check_arrays(*arrays, **options)
    252         if size != n_samples:
    253             raise ValueError("Found array with dim %d. Expected %d"
--> 254                              % (size, n_samples))
    255 
    256         if not allow_lists or hasattr(array, "shape"):

ValueError: Found array with dim 1000. Expected 50000
> /home/vinh/anaconda/envs/nntools/lib/python2.7/site-packages/sklearn/utils/validation.py(254)check_arrays()
    253             raise ValueError("Found array with dim %d. Expected %d"
--> 254                              % (size, n_samples))
    255 

ipdb> q

In [ ]: