Python 阻止 TensorFlow 访问 GPU?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44552585/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:08:30  来源:igfitidea点击:

Prevent TensorFlow from accessing the GPU?

pythontensorflow

提问by jasekp

Is there a way to run TensorFlow purely on the CPU. All of the memory on my machine is hogged by a separate process running TensorFlow. I have tried setting the per_process_memory_fraction to 0, unsuccessfully.

有没有办法纯粹在 CPU 上运行 TensorFlow。我机器上的所有内存都被一个运行 TensorFlow 的单独进程占用。我曾尝试将 per_process_memory_fraction 设置为 0,但未成功。

回答by pfm

Have a look to this questionor this answer.

看看这个问题或这个答案

To summarise you can add this piece of code:

总而言之,您可以添加这段代码:

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
import tensorflow as tf

Playing with the CUDA_VISIBLE_DEVICESenvironment variable is one of if not the wayto go whenever you have GPU-tensorflow installed and you don't want to use any GPUs.

与播放CUDA_VISIBLE_DEVICES环境变量是一个如果没有办法去,只要你有安装GPU-tensorflow,你不希望使用任何的GPU。

You to want either export CUDA_VISIBLE_DEVICES=or alternatively use a virtualenv with a non-GPU installation of TensorFlow.

您想要export CUDA_VISIBLE_DEVICES=或选择将 virtualenv 与 TensorFlow 的非 GPU 安装一起使用。

回答by MZHm

You can use only CPUs by openning a session with a GPU limit of 0:

您可以通过打开 GPU 限制为 0 的会话来仅使用 CPU:

sess = tf.Session(config=tf.ConfigProto(device_count={'GPU': 0}))

See https://www.tensorflow.org/api_docs/python/tf/ConfigProtofor more details.

有关更多详细信息,请参阅https://www.tensorflow.org/api_docs/python/tf/ConfigProto

A proof that it works for @Nicolas:

证明它适用于@Nicolas:

In Python, write:

在 Python 中,编写:

import tensorflow as tf
sess_cpu = tf.Session(config=tf.ConfigProto(device_count={'GPU': 0}))

Then in a terminal:

然后在终端中:

nvidia-smi

You will see something like:

你会看到类似的东西:

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID  Type  Process name                               Usage      |
|=============================================================================|
|    0     24869    C   /.../python                 99MiB                     |
+-----------------------------------------------------------------------------+

Then repeat the process: In Python, write:

然后重复这个过程: 在 Python 中,写:

import tensorflow as tf
sess_gpu = tf.Session()

Then in a terminal:

然后在终端中:

nvidia-smi

You will see something like:

你会看到类似的东西:

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID  Type  Process name                               Usage      |
|=============================================================================|
|    0     25900    C   /.../python                                   5775MiB |
+-----------------------------------------------------------------------------+