Skip to content

Instantly share code, notes, and snippets.

@cabral
Created March 3, 2020 10:04
Show Gist options
  • Save cabral/e3f804dbc4f7ca74b2715521da15911b to your computer and use it in GitHub Desktop.
Save cabral/e3f804dbc4f7ca74b2715521da15911b to your computer and use it in GitHub Desktop.
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
# Restrict TensorFlow to only use the fourth GPU
tf.config.experimental.set_visible_devices(gpus[0], 'GPU')
# Currently, memory growth needs to be the same across GPUs
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
logical_gpus = tf.config.experimental.list_logical_devices('GPU')
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
except RuntimeError as e:
# Memory growth must be set before GPUs have been initialized
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment