Menu Close

How to prevent tensorflow from allocating all of a GPU’s memory with Python?

Sometimes, we want to prevent tensorflow from allocating all of a GPU’s memory with Python.

In this article, we’ll look at how to prevent tensorflow from allocating all of a GPU’s memory with Python.

How to prevent tensorflow from allocating all of a GPU’s memory with Python?

To prevent tensorflow from allocating all of a GPU’s memory with Python, we can create a GPUOptions object.

For instance, we write

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)

sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

to create a GPUOptions object with the per_process_gpu_memory_fraction argument set to 0.333 to make tensorflow use 33.3% of the GPU’s memory.

Then we start a session with gpu_options with

sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

Conclusion

To prevent tensorflow from allocating all of a GPU’s memory with Python, we can create a GPUOptions object.

Posted in Python, Python Answers