2020年10月14日 星期三

[ Python 常見問題 ] Can Keras with Tensorflow backend be forced to use CPU or GPU at will?

 Source From Here

Question
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.

HowTo
If you want to force Keras to use CPU:

Way1
  1. import os  
  2. os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"   # see issue #152  
  3. 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
https://github.com/keras-team/keras/issues/152
https://github.com/fchollet/keras/issues/4613

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...