10.11.2016 Views

Learning Data Mining with Python

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 11<br />

Finally, we create Theano-based functions that perform this training and also obtain<br />

the output of the network for testing purposes:<br />

import theano<br />

train = theano.function([net_input, true_output], loss,<br />

updates=updates)<br />

get_output = theano.function([net_input], net_output)<br />

We can then call our train function, on our training data, to perform one iteration<br />

of training the network. This involves taking each sample, computing the predicted<br />

class of it, comparing those predictions to the expected classes, and updating<br />

the weights to minimize the loss function. We then perform this 1,000 times,<br />

incrementally training our network over those iterations:<br />

for n in range(1000):<br />

train(X_train, y_train)<br />

Next, we can evaluate by computing the F-score on the outputs. First, we obtain<br />

those outputs:<br />

y_output = get_output(X_test)<br />

Note that get_output is a Theano function we obtained from our<br />

neural network, which is why we didn't need to add our network as a<br />

parameter to this line of code.<br />

This result, y_output, is the activation of each of the neurons in the final output<br />

layer. The actual prediction itself is created by finding which neuron has the<br />

highest activation:<br />

import numpy as np<br />

y_pred = np.argmax(y_output, axis=1)<br />

Now, y_pred is an array of class predictions, like we are used to in classification<br />

tasks. We can now compute the F-score using these predictions:<br />

from sklearn.metrics import f1_score<br />

print(f1_score(y_test, y_pred))<br />

The result is impressively perfect—1.0! This means all the classifications were<br />

correct in the test data: a great result (although this is a simpler dataset).<br />

As we can see, while it is possible to develop and train a network using just Lasagne,<br />

it can be a little awkward. To address this, we will be using nolearn, which is a<br />

package that further wraps this process in code that is conveniently convertible <strong>with</strong><br />

the scikit-learn API.<br />

[ 253 ]

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

Saved successfully!

Ooh no, something went wrong!