参考:
https://blog.csdn.net/heiheiya/article/details/102776353
发现tensorflow v2版本对应的gpu设置变化较大。这里记一下比价简洁的使用。
1.最小量增长使用GPU
1.1 单个gpu
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
tf.config.experimental.set_memory_growth(gpus[i], True) # 使用单个第i个gpu(0开始)
1.2 指定连续多个gpu
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
for i in range(2, len(gpus)): # 连续多个
tf.config.experimental.set_memory_growth(gpus[i], True)
1.3 指定间断多个gpu
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
for i in [1,3]:
tf.config.experimental.set_memory_growth(gpus[i], True)