24.07.2016 Views

www.allitebooks.com

Learning%20Data%20Mining%20with%20Python

Learning%20Data%20Mining%20with%20Python

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11<br />

First we create the layers of our neural network:<br />

from lasagne import layers<br />

layers=[<br />

('input', layers.InputLayer),<br />

('conv1', layers.Conv2DLayer),<br />

('pool1', layers.MaxPool2DLayer),<br />

('conv2', layers.Conv2DLayer),<br />

('pool2', layers.MaxPool2DLayer),<br />

('conv3', layers.Conv2DLayer),<br />

('pool3', layers.MaxPool2DLayer),<br />

('hidden4', layers.DenseLayer),<br />

('hidden5', layers.DenseLayer),<br />

('output', layers.DenseLayer),<br />

]<br />

We use dense layers for the last three layers, but before that we use convolution<br />

layers <strong>com</strong>bined with pooling layers. We have three sets of these. In addition, we<br />

start (as we must) with an input layer. This gives us a total of 10 layers. As before,<br />

the size of the first and last layers is easy to work out from the dataset, although our<br />

input size will have the same shape as the dataset rather than just the same number<br />

of nodes/inputs.<br />

Start building our neural network (remember to not close the parentheses):<br />

from nolearn.lasagne import NeuralNet<br />

nnet = NeuralNet(layers=layers,<br />

Add the input shape. The shape here resembles the shape of the dataset (three values<br />

per pixel and a 32 by 32 pixel image). The first value, None, is the default batch size<br />

used by nolearn—it will train on this number of samples at once, decreasing the<br />

running time of the algorithm. Setting it to None removes this hard-coded value,<br />

giving us more flexibility in running our algorithm:<br />

input_shape=(None, 3, 32, 32),<br />

To change the batch size, you will need to create a BatchIterator<br />

instance. Those who are interested in this parameter can view the<br />

source of the file at https://github.<strong>com</strong>/dnouri/nolearn/<br />

blob/master/nolearn/lasagne.py, track the batch_iterator_<br />

train and batch_iterator_test parameters, and see how they<br />

are set in the NeuralNet class in this file.<br />

[ 265 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!