本文介绍了Tensorflow后端的Keras能否被迫随意使用CPU或GPU?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我安装了Tensorflow后端和CUDA的Keras.我有时想按需强迫Keras使用CPU.不用说在虚拟环境中安装单独的仅CPU的Tensorflow就能做到吗?如果可以,怎么办?如果后端是Theano,则可以设置标志,但是我还没有听说过可以通过Keras访问Tensorflow标志.
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.
推荐答案
如果要强制Keras使用CPU
If you want to force Keras to use CPU
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"] = ""
在导入Keras/Tensorflow之前.
before Keras / Tensorflow is imported.
运行脚本为
$ CUDA_VISIBLE_DEVICES="" ./your_keras_code.py
另请参见
- https://github.com/keras-team/keras/issues/152
- https://github.com/fchollet/keras/issues/4613
这篇关于Tensorflow后端的Keras能否被迫随意使用CPU或GPU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!