How to Accelerate Your Python Deep Learning with Cloud GPU?

  1. 云栖社区>
  2. 博客>
  3. 正文

How to Accelerate Your Python Deep Learning with Cloud GPU?

王树义 2018-08-05 14:44:00 浏览1011
展开阅读全文
img_7a4acdab4d8437ba8465b0aa668cf060.png

Overloaded

This afternoon, I trained a 3-layers neural network as a regression model to predict the house price in Boston district with Python and Keras.

img_72e01b0bf66d01bdf45ff5cd48b595a7.jpe

The example case came from the book "Deep Learning with Python".

img_874ab54c2deeee132d307762a5f23f94.jpe

There were 2 big loop during the running procedure.

The first one went through the data for 100 times (epochs), while the second one ran 500 epochs.

My poor laptop was apparently overladed in such a hot summer weather and the fan was roaring.

It seems the laptop is not the best choice to train deep neural models.

It would be so great if I have got a GPU.

Suddenly, it occurs to me that it is not necessary to train the model locally. It's a cloud computing age!

How about to run the code on cloud GPU to save my laptop's effort?

Encounter

It reminds me a video clip post by Siraj Raval on Youtube recently.

img_f32fe3264341b63f08c7a1e23bdcab52.jpe

He recommended cloud GPU platform, namely Floydhub, in this video.

img_188a0c3e25a03f545455db598310a626.jpe

Actually, I once tried AWS GPU product in a online deep learning course. The instructor collaborated with AWS and provided all the students with AWS Computing power to solve the exercise as well as the homework.

However, it was not a very good experience, since he had to make a long video to show the students how to configure the AWS instance.

Indeed, comparing with some other solutions, the AWS was simple enough, yet still not so simple for the new newbies.

img_c2ec94e140819ac09254e11907e7b448.jpe

The website FloydHub, on the other hand, solved the pain point well.

Firstly, it is wrapper over AWS, and filtered out a lot of complex operations.

Secondly, FloydHub is batteries-included with a lot of main stream machine learning frameworks.

img_4081a4d7e6f94cf6a0b835054935d5e7.jpe

Besides, it is well-documented and friendly to the new users.

The slogan is:

Focus on what matters. Let FloydHub handle the grunt work.

Honestly, I like all the things designed for the lazy folks.

So I registered immediately and validated my email.

img_2a72d48d3fed25e764ff41457e7e3348.jpe

Then I got 2 hours GPU running time for free!

img_6f6c76651c8efb94a1ed08b6650bc299.jpe

To spend the precious GPU running time on something import, I read the Quick Start Tutorial eagerly.

img_fee2795f466e4eb3ed13c1e84be0e5e0.jpe

Several minutes later, I feel confident to use it.

Trial

I created a new job from personal control panel on FloydHub and named it "try-keras-boston-house-regression".

Then I exported a Python Script file from my local Jupyter Notebook.

img_9a26a0a511ee43b9b84f31a4a97b7c43.png

I created a new directory and copied the script file into it.

img_b93532288172649b6d055e07b1942908.jpe

To save the Evaluation Metrics of the training and evaluation process, I added 3 lines of code in the end of the Python Script.

import pickle

with open('data.pickle', 'wb') as f:
    pickle.dump([all_scores, all_mae_histories], f)

In this way, we can save all_scores and all_mae_histories data into a file named data.pickle with the Pickle Module in Python.

Then let's dive into the shell and navigate to this new created folder with cd command and execute the following command:

pip install floyd-cli

The command line interface of FloydHub is ready to use.

We can login the FloydHub account with:

floyd login

Then input your FloydHub username and password.

When it's ready, run:

floyd init try-keras-boston-house-regression

Please notice the last parameter should be identical to the title you input just now when created the new job from control panel.

Now we can run the Python script with following command:

floyd run --gpu --env tensorflow-1.8 "python 03-house-price.py"

In this command, --gpu means that we ask the FloydHub to run the script in a GPU environment instead of a default CPU one, and --env tensorflow-1.8 means it will use Tensorflow version 1.8, and the Keras version is 2.1.6 accordingly.

If you want to use other framework or choose a different version, please refer to this link.

img_67711fa866b92fc93a2c656c70be6288.jpe

In response, we get the following messages from FloydHub.

img_14b0ed4a58ee568bec45bf76ab00ecb9.jpe

It's all set.

Yes, so easy. And your learning job is already running in the cloud.

Results

While the job was running, I drank some tea, read several pages of books and browsed some news on Social Media with my phone.

When the running job is done, it will terminate the environment and will not charge you any extra GPU running time. So you don't need to keep an eye on it.

When I came back to my computer, the job's already fininished.

img_5932744ce53b1cf3411ba540dcf220e9.jpe

GPU memory was busy during the whole procedure, as the Utilization was above 90% most of the time.

The GPU, on the other hand, was not busy at all.

Maybe my neural network was too simple.

Scrolling down the page, we can see the logs.

img_3d8d25cf53ddb84d23119e1bb7a56144.jpe

The output was similar to the one when you train the model locally. Besides, it showed you extra information about GPU resource allocation.

To see the saved file, you can open the Files tag.

img_4593f2f801f4b6976bbc36f0447e551d.jpe

The pickle file's already there.

FloydHub helped us with all the hard computing job, and my laptop is much cooler this time.

You can download the pickle file, and put it back into the original working directory.

Let's go back to the Jupyter Lab page on the laptop and open a new ipynb file.

The following code can check the running results.

import pickle
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

with open('data.pickle', 'rb') as f:
    [all_scores, all_mae_histories] = pickle.load(f)

num_epochs = 500
average_mae_history = [
    np.mean([x[i] for x in all_mae_histories]) for i in range(num_epochs)
]

plt.plot(range(1, len(average_mae_history) + 1), average_mae_history)
plt.xlabel('Epochs')
plt.ylabel('Validation MAE')
plt.show()

Please notice these codes will only do some drawings.

Here is the result:

img_9e4395b9f4c37f8b0f1b1e9c166f7542.jpe

The visualization result is identical to the textbook which shows the code ran smoothly on the Cloud GPU environment.

You can check the remaining GPU running time easily.

img_51f7ae5e1274bf28e557fb6025291a1f.png

There's still more than 1 hour to play with. Great!

Workspace

Just now, I showed you how to run FloydHub in Command Line Interface. If you are familiar with bash command, it will be great.

However, for the new users who do not want to use the shell command, I recommend you to try an easier way.

Click the Workspace tab.

img_b9dac38da70905df5888b77fe02efb75.jpe

You will see two existing Workspace examples.

Try to open the first one and check it out.

Hit the green Resume button on top right, the system will try to provide us the environment.

img_97c825d0f52207273e65713ce33f9d1d.jpe

When it's done, you'll see the familiar Jupyter lab interface.

Open the dog-breed-classification.ipynb from the left side file list.

img_82d7f1c183c5bf11a81bd649565a46b1.jpe

It's a complete example to separate different dog breeds.

Hit Run -> Restart Kernel and Run All Cells from the menu.

img_6107e79f2598a2ef9f22b700e86c1016.jpe

You'll figure out there is no significant difference with running the code locally.

img_bc2d40f2d290e90c96a8585fe225156e.jpe

However, this time, you are using GPU!

What if you want to set up a new workspace yourself?

You can go back to the Project page .

img_246af2d8d47edbb2db41884cd9e56783.jpe

For each project, you can create new workspace with the Create Workspace button.

Floydhub will ask you how to create the new workspace.

img_25c5999fa7da205329ce97a19d5cc4ca.jpe

Let's select Start from scratch on the left side and choose the environment.

img_ad2199ae8763466135fbd5e84b3c95f1.jpe

Let's change the default one into Tensorflow 1.9 and GPU.

img_aa660690ce7021163822c3e464ffd8fc.jpe

Hit the Create Workspace.

img_1546fe3e18daf061ae95f9ae8357158a.jpe

Then click on the link try-keras-boston-house-regression workspace.

A Jupyter Lab interface is ready.

img_bd74229d5709b9e9bdb82f18cd84ab48.jpe

You don't need to install Tensorflow or configure the GPU yourself.

Even better, you don't need to run bash commands this time. Just input the Python code, and use Keras and Tensorflow freely.

That's cool!

Start your own Deep Learning Journey with Floydhub.

Summary

You don't need to buy your own expensive deep learning device if you just need GPU computing power occasionally. It will be a waste, and you'll not get a good price when you want to sell it to make an upgrade. In this case, Cloud GPU is a better choice.

Have you ever used any other Cloud GPUs? What are the pros and cons comparing with Floydhub?

I would like to have your feedbacks.

网友评论

登录后评论
0/500
评论
王树义
+ 关注