Python 如何在 CPU 上运行 Tensorflow
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37660312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run Tensorflow on CPU
提问by Alexander R Johansen
I have installed the GPU version of tensorflow on an Ubuntu 14.04.
我已经在 Ubuntu 14.04 上安装了 GPU 版本的 tensorflow。
I am on a GPU server where tensorflow can access the available GPUs.
我在 GPU 服务器上,tensorflow 可以在其中访问可用的 GPU。
I want to run tensorflow on the CPUs.
我想在 CPU 上运行 tensorflow。
Normally I can use env CUDA_VISIBLE_DEVICES=0
to run on GPU no. 0.
通常我可以env CUDA_VISIBLE_DEVICES=0
用来在 GPU 上运行。0.
How can I pick between the CPUs instead?
我该如何在 CPU 之间进行选择?
I am not intersted in rewritting my code with with tf.device("/cpu:0"):
我对重写我的代码不感兴趣 with tf.device("/cpu:0"):
采纳答案by Ivan Aksamentov - Drop
You can apply device_count
parameter per tf.Session
:
您可以应用device_count
参数 per tf.Session
:
config = tf.ConfigProto(
device_count = {'GPU': 0}
)
sess = tf.Session(config=config)
See also protobuf config file:
另请参阅 protobuf 配置文件:
回答by fabrizioM
You can also set the environment variable to
您还可以将环境变量设置为
CUDA_VISIBLE_DEVICES=""
without having to modify the source code.
无需修改源代码。
回答by Aravindh Kuppusamy
If the above answers don't work, try:
如果上述答案不起作用,请尝试:
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
回答by Andrzej Gis
For me, only setting CUDA_VISIBLE_DEVICES
to precisely -1
works:
对我来说,只有设置CUDA_VISIBLE_DEVICES
为精确-1
有效:
Works:
作品:
import os
import tensorflow as tf
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
if tf.test.gpu_device_name():
print('GPU found')
else:
print("No GPU found")
# No GPU found
Does notwork:
难道不工作:
import os
import tensorflow as tf
os.environ['CUDA_VISIBLE_DEVICES'] = ''
if tf.test.gpu_device_name():
print('GPU found')
else:
print("No GPU found")
# GPU found
回答by u9560971
Just using the code below.
只需使用下面的代码。
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
回答by Fabrizio
In some systems one have to specify:
在某些系统中,必须指定:
import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="" # or even "-1"
BEFORE importing tensorflow.
在导入张量流之前。