Python uses cuml to train your machine learning model

python uses cuml train machine

author |Khuyen Tran
compile |VK
source |Towards Data Science


Sklearn It's a great library , There are various machine learning models , Can be used to train data . But if your data is big , You may need a long time to train your data , Especially when you're looking for the best model with different hyperparameters .

Is there a way to make machine learning models faster than using Sklearn Fast 150 times ? The answer is that you can use cuML.

The following chart compares the use of Sklearn Of RandomForestClassifier and cuML Of RandomForestClassifier The time the first mock exam is needed to train the same model .

cuML It's a fast set ,GPU Accelerated machine learning algorithms , Designed for data science and analysis tasks . its API Be similar to Sklearn Of , That means you can use training Sklearn Model code to train cuML Model of .

from cuml.ensemble import RandomForestClassifier
clf = KNeighborsClassifier(n_neighbors=10), y)

In this paper , I'll compare the performance of the two libraries using different models . I'll also show you how to add a graphics card , Make it faster 10 times .

install cuML

To install cuML, Please follow Rapids The instructions on the page to install . Make sure to check the prerequisites before installing the library . You can install all the packages , You can just install cuML. If your computer space is limited , I suggest installing cuDF and cuML.

Although in many cases , No installation required cuDF To use cuML, however cuDF yes cuML A good complement to , Because it's a GPU Data frame .

Make sure you choose the right option for your computer .

Create data

Because when there's a lot of data ,cuML Often than Sklearn Better , So we will use sklearn.datasets.

from sklearn Import dataset

from sklearn import datasets
X, y = datasets.make_classification(n_samples=40000)

Convert data type to np.float32 Because there are some cuML The model requires input to be np.float32.

X = X.astype(np.float32)
y = y.astype(np.float32)

Support vector machine

We're going to create functions for training models . Using this function will make it easier for us to compare different models .

def train_data(model, X=X, y=y):
clf = model, y)

We use iPython Of magic command %timeit Run each function 7 Time , Take the average of all the experiments .

from sklearn.svm import SVC
from cuml.svm import SVC as SVC_gpu
clf_svc = SVC(kernel='poly', degree=2, gamma='auto', C=1)
sklearn_time_svc = %timeit -o train_data(clf_svc)
clf_svc = SVC_gpu(kernel='poly', degree=2, gamma='auto', C=1)
cuml_time_svc = %timeit -o train_data(clf_svc)
print(f"""Average time of sklearn's {clf_svc.__class__.__name__}""", sklearn_time_svc.average, 's')
print(f"""Average time of cuml's {clf_svc.__class__.__name__}""", cuml_time_svc.average, 's')
print('Ratio between sklearn and cuml is', sklearn_time_svc.average/cuml_time_svc.average)
Average time of sklearn's SVC 48.56009825014287 s
Average time of cuml's SVC 19.611496431714304 s
Ratio between sklearn and cuml is 2.476103668030909

cuML Of SVC Than sklearn Of SVC fast 2.5 times !

Let's visualize it with pictures . We create a function to draw the speed of the model .

!pip install cutecharts
import cutecharts.charts as ctc
def plot(sklearn_time, cuml_time):
chart = ctc.Bar('Sklearn vs cuml')
labels=['sklearn', 'cuml'],
y_label='time (s)',
chart.add_series('time', data=[round(sklearn_time.average,2), round(cuml_time.average,2)])
return chart
plot(sklearn_time_svc, cuml_time_svc).render_notebook()

Better graphics

because cuML When running big data Sklearn The model is fast , Because they use GPU Trained , If we were to GPU What happens if you triple your memory ?

In the previous comparison , I'm using a carrier geforce2060 Of Alienware M15 Laptops and 6.3gb Of the graphics card memory .

Now? , I'm going to use one with Quadro RTX 5000 Of Dell Precision 7740 and 17 GB To test the memory of the graphics card GPU The speed at which memory increases .

Average time of sklearn's SVC 35.791008955999914 s
Average time of cuml's SVC 1.9953700327142931 s
Ratio between sklearn and cuml is 17.93702840535976

When it's in a graphics card memory for 17gb When training on the machine ,cuML The support vector machine is better than Sklearn Support vector machines are fast 18 times ! Its speed is the speed of laptop training 10 times , The memory of the video card is 6.3gb.

That's why if we use things like cuML In this way GPU Acceleration Library .

Random forest classifier

clf_rf = RandomForestClassifier(max_features=1.0,
sklearn_time_rf = %timeit -o train_data(clf_rf)
clf_rf = RandomForestClassifier_gpu(max_features=1.0,
cuml_time_rf = %timeit -o train_data(clf_rf)
print(f"""Average time of sklearn's {clf_rf.__class__.__name__}""", sklearn_time_rf.average, 's')
print(f"""Average time of cuml's {clf_rf.__class__.__name__}""", cuml_time_rf.average, 's')
print('Ratio between sklearn and cuml is', sklearn_time_rf.average/cuml_time_rf.average)
Average time of sklearn's RandomForestClassifier 29.824075075857113 s
Average time of cuml's RandomForestClassifier 0.49404465585715635 s
Ratio between sklearn and cuml is 60.3671646323408

cuML Of RandomForestClassifier Than Sklearn Of RandomForestClassifier fast 60 times ! If you train Sklearn Of RandomForestClassifier need 30 second , So training cuML Of RandomForestClassifier It only takes less than half a second !

Better graphics

Average time of Sklearn's RandomForestClassifier 24.006061030143037 s
Average time of cuML's RandomForestClassifier 0.15141178591425808 s.
The ratio between Sklearn’s and cuML is 158.54816641379068

In my Dell Precision 7740 When training on a laptop ,cuML Of RandomForestClassifier Than Sklearn Of RandomForestClassifier fast 158 times !

Nearest neighbor classifier

Average time of sklearn's KNeighborsClassifier 0.07836367340000508 s
Average time of cuml's KNeighborsClassifier 0.004251259535714585 s
Ratio between sklearn and cuml is 18.43304854518441

notes :y On axis 20m Express 20ms.

cuML Of KNeighborsClassifier Than Sklearn Of KNeighborsClassifier fast 18 times .

More graphics memory

Average time of sklearn's KNeighborsClassifier 0.07511190322854547 s
Average time of cuml's KNeighborsClassifier 0.0015137992111426033 s
Ratio between sklearn and cuml is 49.618141346401956

In my Dell Precision 7740 When training on a laptop ,cuML Of KNeighborsClassifier Than Sklearn Of KNeighborsClassifier fast 50 times .


You can find other comparison code here .

The following two tables summarize the speed of the different models between the two libraries :

  • Alienware M15-GeForce 2060 and 6.3 GB Video card memory
index sklearn(s) cuml(s) sklearn/cuml
SVC 50.24 23.69 2.121
RandomForestClassifier 29.82 0.443 67.32
KNeighborsClassifier 0.078 0.004 19.5
LinearRegression 0.005 0.006 0.8333
Ridge 0.021 0.006 3.5
KNeighborsRegressor 0.076 0.002 38
  • Dell Precision 7740-Quadro RTX 5000 and 17 GB Video card memory
index sklearn(s) cuml(s) sklearn/cuml
SVC 35.79 1.995 17.94
RandomForestClassifier 24.01 0.151 159
KNeighborsClassifier 0.075 0.002 37.5
LinearRegression 0.006 0.002 3
Ridge 0.005 0.002 2.5
KNeighborsRegressor 0.069 0.001 69

It's quite impressive , isn't it? ?


You just learned that in cuML Train different models with Sklearn How fast compared to . If you use Sklearn It takes a long time to train your model , I strongly recommend that you try cuML, Because with Sklearn Of API comparison , There is no change in the code .

Of course , If the library uses GPU To perform something like cuML This code , So the better graphics you have , The faster you train .

More about other machine learning models , see also cuML Documents :

Link to the original text :

Welcome to join us AI Blog station :

sklearn Machine learning Chinese official documents :

Welcome to pay attention to pan Chuang blog resource summary station :

本文为[Artificial intelligence meets pioneer]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database