There are 3 ways to achieve this:
-
Using
CUDA_VISIBLE_DEVICESenvironment variable.
by setting environment variableCUDA_VISIBLE_DEVICES="1"makes only device 1 visible and by settingCUDA_VISIBLE_DEVICES="0,1"makes devices 0 and 1 visible. You can do this in python by having a lineos.environ["CUDA_VISIBLE_DEVICES"]="0,1"after importingospackage. -
Using
with tf.device('/gpu:2')and creating the graph. Then it will use GPU device 2 to run. -
Using
config = tf.ConfigProto(device_count = {'GPU': 1})and thensess = tf.Session(config=config). This will use GPU device 1.