www.allitebooks.com

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

24.07.2016 Views

Chapter 11 Finally, we set the verbosity as equal to 1, which will give us a printout of the results of each epoch. This allows us to know the progress of the model and also that it is still running. Another feature is that it tells us the time it takes for each epoch to run. This is pretty consistent, so you can compute the time left in training by multiplying this value by the number of remaining epochs, giving a good estimate on how long you need to wait for the training to complete: verbose=1) Putting it all together Now that we have our network, we can train it with our training dataset: nnet.fit(X_train, y_train) This will take quite a while to run, even with the reduced dataset size and the reduced number of epochs. Once the code completes, you can test it as we did before: from sklearn.metrics import f1_score y_pred = nnet.predict(X_test) print(f1_score(y_test.argmax(axis=1), y_pred.argmax(axis=1))) The results will be terrible—as they should be! We haven't trained the network very much—only for a few iterations and only on one fifth of the data. First, go back and remove the break line we put in when creating the dataset (it is in the batches loop). This will allow the code to train on all of the samples, not just some of them. Next, change the number of epochs to 100 in the neural network definition. Now, we upload the script to our virtual machine. As with before, click on File | Download as, Python, and save the script somewhere on your computer. Launch and connect to the virtual machine and upload the script as you did earlier (I called my script chapter11cifar.py—if you named yours differently, just update the following code). The next thing we need is for the dataset to be on the virtual machine. The easiest way to do this is to go to the virtual machine and type: wget http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz [ 267 ]

Classifying Objects in Images Using Deep Learning This will download the dataset. Once that has downloaded, you can extract the data to the Data folder by first creating that folder and then unzipping the data there: mkdir Data tar -zxf cifar-10-python.tar.gz -C Data Finally, we can run our example with the following: python3 chapter11cifar.py The first thing you'll notice is a drastic speedup. On my home computer, each epoch took over 100 seconds to run. On the GPU-enabled virtual machine, each epoch takes just 16 seconds! If we tried running 100 epochs on my computer, it would take nearly three hours, compared to just 26 minutes on the virtual machine. This drastic speedup makes trailing different models much faster. Often with trialing machine learning algorithms, the computational complexity of a single algorithm doesn't matter too much. An algorithm might take a few seconds, minutes, or hours to run. If you are only running one model, it is unlikely that this training time will matter too much—especially as prediction with most machine learning algorithms is quite quick, and that is where a machine learning model is mostly used. However, when you have many parameters to run, you will suddenly need to train thousands of models with slightly different parameters—suddenly, these speed increases matter much more. After 100 epochs of training, taking a whole 26 minutes, you will get a printout of the final result: 0.8497 Not too bad! We can increase the number of epochs of training to improve this further or we might try changing the parameters instead; perhaps, more hidden nodes, more convolution layers, or an additional dense layer. There are other types of layers in Lasagne that could be tried too; although generally, convolution layers are better for vision. [ 268 ]

Chapter 11<br />

Finally, we set the verbosity as equal to 1, which will give us a printout of the results<br />

of each epoch. This allows us to know the progress of the model and also that it is<br />

still running. Another feature is that it tells us the time it takes for each epoch to run.<br />

This is pretty consistent, so you can <strong>com</strong>pute the time left in training by multiplying<br />

this value by the number of remaining epochs, giving a good estimate on how long<br />

you need to wait for the training to <strong>com</strong>plete:<br />

verbose=1)<br />

Putting it all together<br />

Now that we have our network, we can train it with our training dataset:<br />

nnet.fit(X_train, y_train)<br />

This will take quite a while to run, even with the reduced dataset size and the<br />

reduced number of epochs. Once the code <strong>com</strong>pletes, you can test it as we did before:<br />

from sklearn.metrics import f1_score<br />

y_pred = nnet.predict(X_test)<br />

print(f1_score(y_test.argmax(axis=1), y_pred.argmax(axis=1)))<br />

The results will be terrible—as they should be! We haven't trained the network very<br />

much—only for a few iterations and only on one fifth of the data.<br />

First, go back and remove the break line we put in when creating the dataset<br />

(it is in the batches loop). This will allow the code to train on all of the samples,<br />

not just some of them.<br />

Next, change the number of epochs to 100 in the neural network definition.<br />

Now, we upload the script to our virtual machine. As with before, click on<br />

File | Download as, Python, and save the script somewhere on your <strong>com</strong>puter.<br />

Launch and connect to the virtual machine and upload the script as you did earlier<br />

(I called my script chapter11cifar.py—if you named yours differently, just update<br />

the following code).<br />

The next thing we need is for the dataset to be on the virtual machine. The easiest<br />

way to do this is to go to the virtual machine and type:<br />

wget http://<strong>www</strong>.cs.toronto.edu/~kriz/cifar-10-python.tar.gz<br />

[ 267 ]

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

Saved successfully!

Ooh no, something went wrong!