如何使用特定的GPU';多GPU培训的keras中有哪些?

如何使用特定的GPU';多GPU培训的keras中有哪些?,keras,multi-gpu,Keras,Multi Gpu,我有一个带有4个GPU的服务器。我想使用其中的两个进行多GPU培训 Keras提供的文档提供了一些关于如何使用多个GPU的见解,但我想选择特定的GPU。有没有办法做到这一点 from keras import backend as K import tensorflow as tf c = [] for d in ['/device:GPU:2', '/device:GPU:3']: with K.tf.device(d): config = tf.ConfigProto

我有一个带有4个GPU的服务器。我想使用其中的两个进行多GPU培训

Keras提供的文档提供了一些关于如何使用多个GPU的见解,但我想选择特定的GPU。有没有办法做到这一点

from keras import backend as K
import tensorflow as tf
c = []
for d in ['/device:GPU:2', '/device:GPU:3']:
    with K.tf.device(d):
        config = tf.ConfigProto(intra_op_parallelism_threads=4,\
        inter_op_parallelism_threads=4, allow_soft_placement=True,\
        device_count = {'CPU' : 1, 'GPU' : 2})
        a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])
        b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])
        c.append(tf.matmul(a, b))
with tf.device('/cpu:0'):
    sum = tf.add_n(c)
session = tf.Session(config=config)
K.set_session(session)
我认为这应该行得通。您应该拥有要使用的GPU设备的数量(索引)。在这种情况下,它是2和3。相关链接1)

2) 最好的方法是使用
tf编译Keras模型。通过在策略范围内创建和编译模型来分发
。例如:

import contextlib

def model_scope(devices):
    if 1 < len(devices):
        strategy = tf.distribute.MirroredStrategy(devices)
        scope = strategy.scope()
    else:
        scope = contextlib.supress() # Python 3.4 up
    return scope

devices = ['/device:GPU:2', '/device:GPU:3']

with model_scope(devices):
    # create and compile your model
    model = get_model()
    model.compile(optimizer=optimizer, loss=loss)
导入上下文库
def型号_范围(设备):
如果1
sum=tf.add\n(c)行中的c是什么?感谢您的快速回复。让我试试这个。