Question
Asked By – mikal94305
I have Keras installed with the Tensorflow backend and CUDA. I’d like to sometimes on demand force Keras to use CPU. Can this be done without say installing a separate CPU-only Tensorflow in a virtual environment? If so how? If the backend were Theano, the flags could be set, but I have not heard of Tensorflow flags accessible via Keras.
Now we will see solution for issue: Can Keras with Tensorflow backend be forced to use CPU or GPU at will?
Answer
If you want to force Keras to use CPU
Way 1
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"] = ""
before Keras / Tensorflow is imported.
Way 2
Run your script as
$ CUDA_VISIBLE_DEVICES="" ./your_keras_code.py
See also
This question is answered By – Martin Thoma
This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0