我知道如何在运行时类型中激活 GPU,但我习惯于使用自动利用 GPU 的 sklearn 或 XGBoost 进行机器学习。现在我已经制作了自己的机器学习算法,但我不知道如何强制它在 GPU 上进行计算。我需要 GPU 运行时类型的额外 RAM,但我不知道如何从 GPU 的速度中受益...
@jit(target ="cuda")
popsize = 1000
File "<ipython-input-82-7cb543a75250>", line 2
popsize = 1000
^
SyntaxError: invalid syntax
正如您在此处看到的, Numba 和 Jit 是将脚本放在 GPU 上的方法,如下所示:
from numba import jit, cuda
import numpy as np
# to measure exec time
from timeit import default_timer as timer
# normal function to run on cpu
def func(a):
for i in range(10000000):
a[i]+= 1
# function optimized to run on gpu
@jit(target ="cuda")
def func2(a):
for i in range(10000000):
a[i]+= 1
if __name__=="__main__":
n = 10000000
a = np.ones(n, dtype = np.float64)
b = np.ones(n, dtype = np.float32)
start = timer()
func(a)
print("without GPU:", timer()-start)
start = timer()
func2(a)
print("with GPU:", timer()-start)
您可以使用另外一个参考链接
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句