How to reduce GPU memory usage with Keras

If you try to use Keras with the back end as TensorFlow, the default setting is to use all the GPU memory and you can not run multiple experiments, so I will introduce the setting method to reduce the GPU memory usage. [^ 1] [^ 2]

Verification environment

tensorflow==1.3.0
tensorflow-gpu==1.3.0
Keras==2.0.6

Reduce GPU memory usage

You can paste the code below or import it.

Secure only minimal GPU memory

It can be set with gpu_options.allow_growth. It is a method of allocating only the required amount at the time of execution, and the memory area is expanded when it is needed further. However, since the memory is not released automatically, the memory may become fragmented and the performance may deteriorate, so be careful. [^ 1]

import tensorflow as tf
from keras import backend as K
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
K.set_session(sess)

Secured by specifying the ratio of total GPU memory

It can be set with gpu_options.per_process_gpu_memory_fraction. In the example below, 40% of memory is used.

import tensorflow as tf
from keras import backend as K
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4
sess = tf.Session(config=config)
K.set_session(sess)

Run-time status

$ nvidia-smi -l
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID  Type  Process name                               Usage      |
|=============================================================================|
|    0     30654    C   python                                        3527MiB |
|    1     30779    C   python                                        3357MiB |
+-----------------------------------------------------------------------------+

References

Recommended Posts

How to reduce GPU memory usage with Keras
How to update with SQLAlchemy?
How to cast with Theano
How to Alter with SQLAlchemy?
How to separate strings with','
How to RDP with Fedora31
How to Delete with SQLAlchemy?
[TensorFlow 2 / Keras] How to run learning with CTC Loss in Keras
Get the id of a GPU with low memory usage
For beginners, how to deal with common errors in keras
How to cancel RT with tweepy
Python: How to use async with
How to use virtualenv with PowerShell
How to deal with imbalanced data
How to install python-pip with ubuntu20.04LTS
How to deal with imbalanced data
How to get started with Scrapy
How to get started with Python
How to deal with DistributionNotFound errors
How to get started with Django
How to Data Augmentation with PyTorch
How to use FTP with Python
How to calculate date with python
How to install mysql-connector with pip3
How to INNER JOIN with SQLAlchemy
How to install Anaconda with pyenv
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to enable Keras Regressor to be saved with pickle or joblib
How to do arithmetic with Django template
[Blender] How to set shape_key with script
How to get parent id with sqlalchemy
How to add a package with PyCharm
How to install DLIB with 2020 / CUDA enabled
How to use ManyToManyField with Django's Admin
How to use OpenVPN with Ubuntu 18.04.3 LTS
How to use Cmder with PyCharm (Windows)
[TF] How to use Tensorboard from Keras
Attempt to classify emoji fonts with Keras
How to prevent package updates with apt
How to work with BigQuery in Python
How to use Ass / Alembic with HtoA
How to deal with enum compatibility errors
How to use Japanese with NLTK plot
How to do portmanteau test with python
How to search Google Drive with Google Colaboratory
How to display python Japanese with lolipop
How to download youtube videos with youtube-dl
How to use jupyter notebook with ABCI
How to power off Linux with Ultra96-V2
"How to pass PATH" to learn with homebrew
How to scrape websites created with SPA
How to use CUT command (with sample)
How to enter Japanese with Python curses
[Python] How to deal with module errors
Try to predict FX with LSTM using Keras + Tensorflow Part 2 (Calculate with GPU)
How to install zsh (with .zshrc customization)
How to read problem data with paiza
How to use SQLAlchemy / Connect with aiomysql
How to get started with laravel (Linux)
How to install fabric and basic usage