Using tensorflow-gpu 2.0.0rc0. I want to choose whether it uses the GPU or the CPU.
I've seen some suggestions elsewhere, but they are old and do not apply very well to newer TF versions. What worked for me was this:
import os os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
When that variable is defined and equal to -1, TF uses the CPU even when a CUDA GPU is available.
try: # Disable all GPUS tf.config.set_visible_devices(, 'GPU') visible_devices = tf.config.get_visible_devices() for device in visible_devices: assert device.device_type != 'GPU' except: # Invalid device or cannot modify virtual devices once initialized. pass