简体   繁体   English

如何在 GPU 中运行机器学习算法

[英]How to run Machine Learning algorithms in GPU

I have used backward elimination algorithm to reduce features.我使用反向消除算法来减少特征。 But the point is with large amount of features and samples, it run on CPU and pretty slow.但关键是有大量的特征和样本,它在 CPU 上运行并且相当慢。

How could i run it multithreading on GPU like i train Deep Learning.我怎么能像训练深度学习一样在 GPU 上运行它的多线程。 This is my code这是我的代码

import pandas as pd
data = pd.read_csv('data.csv')
X = data.drop(['Path','id','label'], axis=1)
y = data['label']

from mlxtend.feature_selection import SequentialFeatureSelector as sfs
from sklearn.linear_model import LinearRegression
lreg = LinearRegression()
new_sfs = sfs(lreg, k_features=1600, forward=False, verbose=1, scoring='neg_mean_squared_error')
new_sfs = new_sfs.fit(X, y)
feat_names = list(sfs1.k_feature_names_)

Have you considered using JAX ?你考虑过使用JAX吗? You can easily use numpy routines (written in jax) to offload your calculations to GPU (or any opther device like TPU).您可以轻松地使用 numpy 例程(用 jax 编写)将计算卸载到 GPU(或任何其他设备,如 TPU)。 Here's an example of basic linear regression in JAX:这是 JAX 中基本线性回归的示例:

import jax.numpy as jnp
import jax
import matplotlib.pyplot as plt

@jax.jit
def gpu_linear(x,y):
    return jnp.linalg.lstsq(x, y, rcond=None)[0]

# Setting key for random number generation
key_ = jax.random.PRNGKey(seed=1)

# Generating toy example
key_, subkey_ = jax.random.split(key=key_)
x = jax.random.normal(key=subkey_, shape=(100000,))
x = jnp.vstack([x,jnp.ones(len(x))]).T

key_, subkey_ = jax.random.split(key=key_)
y = 1.6*x[:,0] + jax.random.normal(key=subkey_, shape=(100000,))


m, c = gpu_linear(x,y)

plt.plot(x[:,0], y, 'o', label='original data')
plt.plot(x[:,0], m*x[:,0] + c, 'r', label='fitted line')
plt.legend()
plt.show()

在此处输入图像描述

I timed the above implementation using %timeit :我使用%timeit对上述实现进行计时:

%timeit gpu_linear(x,y)
243 µs ± 691 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

Here's the same thing implemented in numpy (just for reference).这是在 numpy 中实现的相同的东西(仅供参考)。

import numpy as np
import matplotlib.pyplot as plt

def cpu_linear(x,y):
    return np.linalg.lstsq(x, y, rcond=None)[0]

# Generating toy example
x = np.random.normal(0, 1, 100000)
x = np.vstack([x,jnp.ones(len(x))]).T

y = 1.6*x[:,0] + np.random.normal(0, 1, 100000)


m, c = cpu_linear(x,y)

plt.plot(x[:,0], y, 'o', label='original data')
plt.plot(x[:,0], m*x[:,0] + c, 'r', label='fitted line')
plt.legend()
plt.show()

%timeit cpu_linear(x,y)
1.52 ms ± 22.3 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

As you can see there's a speed up when I'm using JAX as all my calculations are offloaded to GPU.正如您所看到的,当我使用 JAX 时速度会有所提高,因为我的所有计算都已卸载到 GPU。 Even if you're not using GPU, JAX can cache your functions (when I decorate the function using @jax.jit ) and you'll see speedups on CPU as well (compared to naive numpy or scikit learn).即使你不使用 GPU,JAX 也可以缓存你的函数(当我使用@jax.jit装饰函数时),你也会看到 CPU 的加速(与幼稚的 numpy 或 scikit learn 相比)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM