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.

Beating CAPTCHAs with Neural Networks<br />

These neurons made their mistake because of the neurons giving them input, but<br />

more specifically due to the weights on the connections between the neuron and its<br />

inputs. We then alter these weights by altering them by a small amount. The amount<br />

of change is based on two aspects: the partial derivative of the error function of<br />

the neuron's individual weights and the learning rate, which is a parameter to the<br />

algorithm (usually set at a very low value). We <strong>com</strong>pute the gradient of the error<br />

of the function, multiply it by the learning rate, and subtract that from our weights.<br />

This is shown in the following example. The gradient will be positive or negative,<br />

depending on the error, and subtracting the weight will always attempt to correct<br />

the weight towards the correct prediction. In some cases, though, the correction will<br />

move towards something called a local optima, which is better than similar weights<br />

but not the best possible set of weights.<br />

This process starts at the output layer and goes back each layer until we reach the<br />

input layer. At this point, the weights on all connections have been updated.<br />

PyBrain contains an implementation of the backprop algorithm, which is called on<br />

the neural network through a trainer class. The code is as follows:<br />

from pybrain.supervised.trainers import BackpropTrainer<br />

trainer = BackpropTrainer(net, training, learningrate=0.01,<br />

weightdecay=0.01)<br />

The backprop algorithm is run iteratively using the training dataset, and each time<br />

the weights are adjusted a little. We can stop running backprop when the error<br />

reduces by a very small amount, indicating that the algorithm isn't improving the<br />

error much more and it isn't worth continuing the training. In theory, we would run<br />

the algorithm until the error doesn't change at all. This is called convergence, but in<br />

practice this takes a very long time for little gain.<br />

Alternatively, and much more simply, we can just run the algorithm a fixed number<br />

of times, called epochs. The higher the number of epochs, the longer the algorithm<br />

will take and the better the results will be (with a declining improvement for each<br />

epoch). We will train for 20 epochs for this code, but trying larger values will<br />

increase the performance (if only slightly). The code is as follows:<br />

trainer.trainEpochs(epochs=20)<br />

After running the previous code, which may take a number of minutes depending<br />

on the hardware, we can then perform predictions of samples in our testing dataset.<br />

PyBrain contains a function for this, and it is called on the trainer instance:<br />

predictions = trainer.testOnClassData(dataset=testing)<br />

[ 174 ]

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

Saved successfully!

Ooh no, something went wrong!