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.
Asked
Active
Viewed 6.4k times
2 Answers
108
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.

Ninjakannon
- 3,751
- 7
- 53
- 76

pfm
- 6,210
- 4
- 39
- 44
-
6How do I re-enable my gpu after using this – GILO Dec 15 '19 at 19:54
-
Did you find a solution to on/off TensorFlow GPU inside Python scripts? – Murali Nov 17 '20 at 11:19
-
1@GILO You can change to your GPU number in the next run of the script e.g = "0" or "0,1" – Omar Shabab Mar 11 '21 at 12:52
-
so. from the command line also it is possible, before launching the application: export CUDA_VISIBLE_DEVICES=-1 – daruma Aug 19 '21 at 06:13
14
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 |
+-----------------------------------------------------------------------------+

MZHm
- 2,388
- 1
- 18
- 24
-
According https://github.com/tensorflow/tensorflow/issues/9201 it's not clear if it works. – pfm Jun 14 '17 at 19:25
-
The way I understand it, the goal was to avoid the memory allocation. If that is the case - it will work. See above – MZHm Jun 14 '17 at 19:44
-
1From what I understand, it does use the GPU even if it doesn't use any memory in it. I might be wrong but I believe the question was how to run TensorFlow purely on CPU. – pfm Jun 14 '17 at 19:48
-
@jasekp ? can you clarify the goal? If the goal is similar to using the cpu for testing in parallel to another process running on the gpu for training, then this should work – MZHm Jun 14 '17 at 20:04