Wanderer
Wanderer

Reputation: 1197

Does tensorflow automatically detect GPU or do I have to specify it manually?

I have a code written in tensorflow that I run on CPUs and it runs fine. I am transferring to a new machine which has GPUs and I run the code on the new machine but the training speed did not improve as expected (takes almost the same time).

I understood that Tensorflow automatically detects GPUs and run the operations on them (https://www.quora.com/How-do-I-automatically-put-all-my-computation-in-a-GPU-in-TensorFlow) & (https://www.tensorflow.org/tutorials/using_gpu).

Do I have to change the code to make it manually runs the operations on GPUs (for now I have a single GPU)? and what would be gained by doing that manually?

Thanks

Upvotes: 3

Views: 4795

Answers (2)

Asuzu Kosi
Asuzu Kosi

Reputation: 31

You could try checking what device the operations are being performed on, either CPU or GPU

tf.debugging.set_log_device_placement(True)

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)

Check the logs from the operation, it would specify what device is being used by tensorflow to execute tose instructions.

You could also try viewing all available physical devices that your tensorflow package has access to

tf.config.list_physical_devices()

This would show the list of all devices tensorflow has access to. If it returns a single item with just CPU, then your tensorflow package does not have access to the GPU.

Upvotes: 1

pfm
pfm

Reputation: 6338

If the GPU version of TensorFlow is installed and if you don't assign all your tensors to CPU, some of them should be assigned to GPU.

To find out which devices (CPU, GPU) are available to TensorFlow, you can use this:

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

Regarding the question of the performance, it's quite a broad subject and it really depends of your model, your data and so on. Here are a few and wide remarks on TensorFlow performance.

Upvotes: 2

Related Questions