www.allitebooks.com

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

24.07.2016 Views

Chapter 11 Finally, we create Theano-based functions that perform this training and also obtain the output of the network for testing purposes: import theano train = theano.function([net_input, true_output], loss, updates=updates) get_output = theano.function([net_input], net_output) We can then call our train function, on our training data, to perform one iteration of training the network. This involves taking each sample, computing the predicted class of it, comparing those predictions to the expected classes, and updating the weights to minimize the loss function. We then perform this 1,000 times, incrementally training our network over those iterations: for n in range(1000): train(X_train, y_train) Next, we can evaluate by computing the F-score on the outputs. First, we obtain those outputs: y_output = get_output(X_test) Note that get_output is a Theano function we obtained from our neural network, which is why we didn't need to add our network as a parameter to this line of code. This result, y_output, is the activation of each of the neurons in the final output layer. The actual prediction itself is created by finding which neuron has the highest activation: import numpy as np y_pred = np.argmax(y_output, axis=1) Now, y_pred is an array of class predictions, like we are used to in classification tasks. We can now compute the F-score using these predictions: from sklearn.metrics import f1_score print(f1_score(y_test, y_pred)) The result is impressively perfect—1.0! This means all the classifications were correct in the test data: a great result (although this is a simpler dataset). As we can see, while it is possible to develop and train a network using just Lasagne, it can be a little awkward. To address this, we will be using nolearn, which is a package that further wraps this process in code that is conveniently convertible with the scikit-learn API. [ 253 ]

Classifying Objects in Images Using Deep Learning Implementing neural networks with nolearn The nolearn package provides wrappers for Lasagne. We lose some of the fine-tuning that can go with building a neural network by hand in Lasagne, but the code is much more readable and much easier to manage. The nolearn package implements the normal sorts of complex neural networks you are likely to want to build. If you want more control than nolearn gives you, you can revert to using Lasagne, but at the cost of having to manage a bit more of the training and building process. To get started with nolearn, we are going to reimplement the example we used in Chapter 8, Beating CAPTCHAs with Neural Networks, to predict which letter was represented in an image. We will recreate the dense neural network we used in Chapter 8, Beating CAPTCHAs with Neural Networks. To start with, we need to enter our dataset building code again in our notebook. For a description of what this code does, refer to Chapter 8, Beating CAPTCHAs with Neural Networks: import numpy as np from PIL import Image, ImageDraw, ImageFont from skimage.transform import resize from skimage import transform as tf from skimage.measure import label, regionprops from sklearn.utils import check_random_state from sklearn.preprocessing import OneHotEncoder from sklearn.cross_validation import train_test_split def create_captcha(text, shear=0, size=(100, 24)): im = Image.new("L", size, "black") draw = ImageDraw.Draw(im) font = ImageFont.truetype(r"Coval.otf", 22) draw.text((2, 2), text, fill=1, font=font) image = np.array(im) affine_tf = tf.AffineTransform(shear=shear) image = tf.warp(image, affine_tf) return image / image.max() def segment_image(image): labeled_image = label(image > 0) subimages = [] for region in regionprops(labeled_image): start_x, start_y, end_x, end_y = region.bbox subimages.append(image[start_x:end_x,start_y:end_y]) if len(subimages) == 0: [ 254 ]

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, <strong>com</strong>puting the predicted<br />

class of it, <strong>com</strong>paring 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 <strong>com</strong>puting 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 <strong>com</strong>pute 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 with<br />

the scikit-learn API.<br />

[ 253 ]

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

Saved successfully!

Ooh no, something went wrong!