How to Accelerate Your Python Deep Learning with Cloud GPU?

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.

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

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.

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

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.

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.

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.

Then I got 2 hours GPU running time for free!

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

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.

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

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.

In response, we get the following messages from FloydHub.

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.

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.

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.

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:

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.

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.

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.

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

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

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

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

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

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 .

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

Floydhub will ask you how to create the new workspace.

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

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

Hit the Create Workspace.

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

A Jupyter Lab interface is ready.

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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,772评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,458评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,610评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,640评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,657评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,590评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,962评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,631评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,870评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,611评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,704评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,386评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,969评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,944评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,179评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,742评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,440评论 2 342