www.allitebooks.com
Learning%20Data%20Mining%20with%20Python Learning%20Data%20Mining%20with%20Python
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 ]
- Page 225 and 226: Authorship Attribution With our dat
- Page 227 and 228: Authorship Attribution We then reco
- Page 229 and 230: Authorship Attribution If it doesn'
- Page 231 and 232: Authorship Attribution Finally, we
- Page 234 and 235: Clustering News Articles In most of
- Page 236 and 237: Chapter 10 API Endpoints are the ac
- Page 238 and 239: The token object is just a dictiona
- Page 240 and 241: Chapter 10 We then create a list to
- Page 242 and 243: Chapter 10 We are going to use MD5
- Page 244 and 245: Chapter 10 Next, we develop the cod
- Page 246 and 247: Chapter 10 We use clustering techni
- Page 248 and 249: Chapter 10 The k-means algorithm is
- Page 250 and 251: Chapter 10 We only fit the X matrix
- Page 252 and 253: Chapter 10 We then print out the mo
- Page 254 and 255: Chapter 10 Our function definition
- Page 256 and 257: Chapter 10 The result from the prec
- Page 258 and 259: Chapter 10 Implementation Putting a
- Page 260 and 261: Chapter 10 Neural networks can also
- Page 262 and 263: We then call the partial_fit functi
- Page 264 and 265: Classifying Objects in Images Using
- Page 266 and 267: Chapter 11 This dataset comes from
- Page 268 and 269: You can change the image index to s
- Page 270 and 271: Chapter 11 Each of these issues has
- Page 272 and 273: Chapter 11 Using Theano, we can def
- Page 274 and 275: Chapter 11 Building a neural networ
- Page 278 and 279: Chapter 11 return [image,] return s
- Page 280 and 281: Chapter 11 Next, we define how the
- Page 282 and 283: Chapter 11 Getting your code to run
- Page 284 and 285: Chapter 11 Setting up the environme
- Page 286 and 287: This will unzip only one Coval.otf
- Page 288 and 289: Chapter 11 First we create the laye
- Page 290 and 291: Chapter 11 Finally, we set the verb
- Page 292: Chapter 11 Summary In this chapter,
- Page 295 and 296: Working with Big Data Big data What
- Page 297 and 298: Working with Big Data Governments a
- Page 299 and 300: Working with Big Data We start by c
- Page 301 and 302: Working with Big Data The final ste
- Page 303 and 304: Working with Big Data Getting the d
- Page 305 and 306: Working with Big Data If we aren't
- Page 307 and 308: Working with Big Data Before we sta
- Page 309 and 310: Working with Big Data The first val
- Page 311 and 312: Working with Big Data This gives us
- Page 313 and 314: Working with Big Data Next, we crea
- Page 315 and 316: Working with Big Data Then, make a
- Page 317 and 318: Working with Big Data Left-click th
- Page 319 and 320: Working with Big Data The result is
- Page 321 and 322: Next Steps… Extending the IPython
- Page 323 and 324: Next Steps… Chapter 3: Predicting
- Page 325 and 326: Next Steps… Vowpal Wabbit http://
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 ]