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.
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Python中*和**的使用
在为函数传递参数和函数定义时使用参数的时候,时常会看到有和*和**,下面分别讲解其作用。 调用函数时使用*和 ** 假设有函数 def test(a, b, c) test(*args):* 的作用其实就是把序列 args 中的每个元素,当作位置参数传进去。比如上面这个代码,如果 args 等于 (1,2,3) ,那么这个代码就等价于 test(1, 2, 3) 。 test(**kwargs):** 的作用则是把字典 kwargs 变成关键字参数传递。比如上面这个代码,如果 kwargs 等于 {‘a’:1,’b’:2,’c’:3} ,那这个代码就等价于 test(a=1,b=2,c=3) 。 定义函数参数时使用*和** def test(*args): 定义函数参数时* 的含义又要有所不同,在这里 *args 表示把传进来的位置参数都装在元组 args 里面。比如说上面这个函数,调用 test(1, 2, 3) 的话, args 的值就是 (1, 2, 3) 。: def test(**kwargs): 类似的, ** 就是针对关键字参数和字典的了。 调用 test(a=1,b=2...
- 下一篇
Java----------LeetCode----------46. 全排列
说起回溯算法,渊源颇深,我上次做过一道,本应该这次能拿下但是,又没拿下,不过这次有点理解它的思想了, 每次的回溯就是一个二叉树,简单的说,就是你执行的操作需要回溯时它会返回调用它的方法哪里的状态,进行下一步的执行。 public List<List<Integer>> permute(int[] nums) { List<List<Integer>> temp = new ArrayList<>(20); zuhe(temp,0,nums); return temp; } private void zuhe(List<List<Integer>> temp, int n, int[] nums) { /* * 每次让其加一个就行了,就空了, * */ if (nums.length == n) { List<Integer> integers =new ArrayList<>(20); for (int r:nums){ integers.add(r); } temp.add(i...
相关文章
文章评论
共有0条评论来说两句吧...