I have a machine with 2 GPUs.
Quite often, one is used in production (i.e doing predictions with the already trained model), while the other is used for training and experimenting new models.
While I was using theano, I had no problem running my scripts on only one GPU by specifying a flag as follow
THEANO_FLAGS="device=cuda0" training_script.py
THEANO_FLAGS="device=cuda1" prediction_script.py
Is there a simple way to do the same in Keras with a Tensorflow backend ? Default behavior seem to map all the memory of all the GPUs for one session
(Please note that I don't really care if each script maps a whole GPU separately, even if they could work using less memory)
If your system has an NVIDIA® GPU and you have the GPU version of TensorFlow installed then your Keras code will automatically run on the GPU.
Then, TensorFlow runs operations on your GPUs by default. You can control how TensorFlow uses CPUs and GPUs: Logging operations placement on specific CPUs or GPUs. Instructing TensorFlow to run certain operations in a specific “device context”—a CPU or a specific GPU, if there are multiple GPUs on the machine.
allow_soft_placement allows for operations to be run on the CPU if any of the following criterion are met: there is no GPU implementation for the operation. there are no GPU devices known or registered.
You can easily choose one gpu. Just fill 0 or 1 on CUDA_VISIBLE_DEVICES
import os
os.environ["CUDA_VISIBLE_DEVICES"]="1"
Furthermore if you want to spesify a portion of gpu for the selected gpu above, add:
from keras import backend as K
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4 #what portion of gpu to use
session = tf.Session(config=config)
K.set_session(session)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With