Tensorflow not utilizing GPU

Hello everyone,
I run a code in python 3.9 with tensorflow 2.10 and cuda 11.2. when running the code:

python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

I get this result indicating that everything is alright:

[PhysicalDevice(name=‘/physical_device:GPU:0’, device_type=‘GPU’)]

This code is ran on another environment in anaconda and not on the root environment.
However the GPU is not utilized and the code is ran on on CPU.
I installed cuda toolkit 11.2 on the system but nothing changed.
Can you please help me to fix this problem?

1 Like

Did you try using:

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

with tf.Session() as sess:
    print (sess.run(c))

This function specifies the device to be used for ops created/executed in a particular context.

I’m not saying it’s the right way, but it may help find out the problems.

You may find this documentation useful if that example fails:

1 Like