Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set dynamic memory growth on TF 2.1?

With previous versions of tensorflow+keras I was able to set an 'allow_growth' option and view realtime memory usage with nvidia-smi. Otherwise it will all gett allocated immediately by the process. Now, using tf.keras in tensorflow 2.1 I can't find a way to do this. Any help is appreciated!

like image 330
user3810965 Avatar asked Feb 03 '20 22:02

user3810965


People also ask

How do I disable GPU usage in TensorFlow?

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.


1 Answers

In case you have several GPUs, you will allow memory growth only for the first GPU.

physical_devices = tf.config.list_physical_devices('GPU') 
tf.config.experimental.set_memory_growth(physical_devices[0], True)

If you want to do it for all GPUs you need to set it for every instance.

physical_devices = tf.config.list_physical_devices('GPU') 
for gpu_instance in physical_devices: 
    tf.config.experimental.set_memory_growth(gpu_instance, True)
like image 89
user3810965 Avatar answered Sep 28 '22 01:09

user3810965