tensorflow
tensorflow可以指定訓練使用的顯卡。如果一臺電腦具有多個NVIDIA的GPUs,用戶想要指定需要使用的GPU,那麼在python中可以寫如下語句。這個語句設置了當前程序可見的顯卡。
import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"
然後使用tf.device來指定訓練的gpu ID。但是如果只指定ID,而沒有屏蔽掉其他的顯卡的話,TF依然會佔用所有的顯卡資源,但是隻在當前顯卡下運行。
tf.device('/gpu:0')
比如
import tensorflow as tf
with tf.device('/gpu:1'):
v1 = tf.constant([1.0, 2.0, 3.0], shape=[3], name='v1')
v2 = tf.constant([1.0, 2.0, 3.0], shape=[3], name='v2')
sumV12 = v1 + v2
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
print sess.run(sumV12)
此外,還可以指定多顯卡同時工作
參考:https://blog.csdn.net/u011961856/article/details/78011270
for gpu_ind in range(0, num_gpus):
with tf.device("/gpu:{}".format(gpu_ind)):
或者
for i in xrange(FLAGS.num_gpus):
with tf.device('/gpu:%d' % i):
爲了避免佔用所有的顯存,可以設置顯存自適應,按比例的自適應方法如下:
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4
session = tf.Session(config=config)
或者使用按照需求的自適應
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config)