0

I know how to activate the GPU in the runtime type, but I'm used to doing machine learning with sklearn or XGBoost which automatically make use of the GPU. Now I've made my own machine learning algorithm but I don't know how to force it do the computations on the GPU. I need the extra RAM from the GPU runtime type, but I don't know how to benefit from the speed of the GPU...

@jit(target ="cuda")
popsize = 1000
  
File "<ipython-input-82-7cb543a75250>", line 2
    popsize = 1000
          ^
SyntaxError: invalid syntax
2
  • 1
    Just note that scikit-learn does not have any kind of GPU support, only XGBoost has it. Commented Jun 23, 2020 at 13:09
  • @Dr.Snoopy is right. The program must be written and Executed with TensorFlow or any other Framework that supports GPU usage. Commented Jun 23, 2020 at 13:10

2 Answers 2

1

As you can see here Numba and Jit are ways to put your scripts on GPU like follows:

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) 

There is one more reference link you can utilize

Sign up to request clarification or add additional context in comments.

7 Comments

Also, Installing Tensorflow(GPU version) will ensure the use of GPU.
Does it matter where I insert the @jit(target = "cuda")? Or is just any code after that line is sent to GPU, and anything before it is sent to CPU?
Yes. As it is a Python Decorator.
it doesn, it says invalid syntax on the line after the @jit(target = "cuda")
Could you post the entire traceback.?
|
0

Now its target_background as target is deprecated.

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.