Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Is there a way to force a Python script on GPU? In my code I use tensorflow and keras, and I have already the tensorflow-gpu version, but my code runs on CPU anyway. I'd like to know if there is a way to force the running on GPU independently on Tensorflow, Numpy or others.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
182 views
Welcome To Ask or Share your Answers For Others

1 Answer

For TensorFlow (but not python in general) there is a good description of how to do this here: https://www.tensorflow.org/guide/gpu

To force a function to be performed on a specific processor (CPU or GPU) use the TensorFlow call to tf.device() as follows:

import tensorflow as tf
with tf.device('/GPU:0'):
  a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
  b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
  c = tf.matmul(a, b)

in this case the data for a and b will be stored on GPU0, and the operation "matmul" will be performed on the GPU0 as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...