10.11.2016 Views

Learning Data Mining with Python

Create successful ePaper yourself

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

Classifying Objects in Images Using Deep <strong>Learning</strong><br />

Next, we have our output layer that takes its input from the hidden layer, which<br />

has three nodes (which is the same as the number of classes), and uses the softmax<br />

nonlinearity. Softmax is more typically used in the final layer of neural networks:<br />

output_layer = lasagne.layers.DenseLayer(hidden_layer, num_units=3,<br />

nonlinearity=lasagne.<br />

nonlinearities.softmax)<br />

In Lasagne's usage, this output layer is our network. When we enter a sample<br />

into it, it looks at this output layer and obtains the layer that is inputted into it<br />

(the first argument). This continues recursively until we reach an input layer, which<br />

applies the samples to itself, as it doesn't have an input layer to it. The activations<br />

of the neurons in the input layer are then fed into its calling layer (in our case, the<br />

hidden_layer), and that is then propagated up all the way to the output layer.<br />

In order to train our network, we now need to define some training functions,<br />

which are Theano-based functions. In order to do this, we need to define a Theano<br />

expression and a function for the training. We start by creating variables for the<br />

input samples, the output given by the network, and the actual output:<br />

import theano.tensor as T<br />

net_input = T.matrix('net_input')<br />

net_output = output_layer.get_output(net_input)<br />

true_output = T.ivector('true_output')<br />

We can now define our loss function, which tells the training function how to improve<br />

the network—it attempts to train the network to minimize the loss according to this<br />

function. The loss we will use is the categorical cross entropy, a metric on categorical<br />

data such as ours. This is a function of the output given by the network and the actual<br />

output we expected:<br />

loss = T.mean(T.nnet.categorical_crossentropy(net_output,<br />

true_output))<br />

Next, we define the function that will change the weights in our network.<br />

In order to do this, we obtain all of the parameters from the network and create<br />

a function (using a helper function given by Lasagne) that adjusts the weights to<br />

minimize our loss;<br />

all_params = lasagne.layers.get_all_params(output_layer)<br />

updates = lasagne.updates.sgd(loss, all_params, learning_rate=0.1)<br />

[ 252 ]

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

Saved successfully!

Ooh no, something went wrong!