How to set specific gpu in tensorflow?

There are 3 ways to achieve this:

  1. Using CUDA_VISIBLE_DEVICES environment variable.
    by setting environment variable CUDA_VISIBLE_DEVICES="1" makes only device 1 visible and by setting CUDA_VISIBLE_DEVICES="0,1" makes devices 0 and 1 visible. You can do this in python by having a line os.environ["CUDA_VISIBLE_DEVICES"]="0,1" after importing os package.

  2. Using with tf.device('/gpu:2') and creating the graph. Then it will use GPU device 2 to run.

  3. Using config = tf.ConfigProto(device_count = {'GPU': 1}) and then sess = tf.Session(config=config). This will use GPU device 1.

Leave a Comment