Prevent TensorFlow from accessing the GPU? [duplicate]

☆樱花仙子☆ 提交于 2021-02-05 13:13:45

问题


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.


回答1:


Have a look to this question or 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_DEVICES environment variable is one of if not the way to go whenever you have GPU-tensorflow installed and you don't want to use any GPUs.

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




回答2:


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

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

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

A proof that it works for @Nicolas:

In Python, write:

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:

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 |
+-----------------------------------------------------------------------------+


来源:https://stackoverflow.com/questions/44552585/prevent-tensorflow-from-accessing-the-gpu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!