为什么我使用Keras的multi_gpu_model进行训练速度比单个GPU慢?

4
我的Keras版本是2.0.9,并使用tensorflow后端。
我尝试在keras中实现multi_gpu_model。然而,使用4个GPU进行训练在实践中比使用1个GPU还要糟糕。我用1个GPU得到了25秒,而用4个GPU则需要50秒。你能告诉我为什么会出现这种情况吗?
/multi_gpu_model的博客

https://www.pyimagesearch.com/2017/10/30/how-to-multi-gpu-training-with-keras-python-and-deep-learning/

我使用这个命令来控制一个GPU。
CUDA_VISIBLE_DEVICES=0 python gpu_test.py

对于4个GPU,

python gpu_test.py

这是用于训练的源代码。

from keras.datasets import mnist
from keras.layers import Input, Dense, merge
from keras.layers.core import Lambda
from keras.models import Model
from keras.utils import to_categorical
from keras.utils.training_utils import multi_gpu_model
import time

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

inputs = Input(shape=(784,))

x = Dense(4096, activation='relu')(inputs)
x = Dense(2048, activation='relu')(x)
x = Dense(512, activation='relu')(x)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
'''
m_model = multi_gpu_model(model, 4)
m_model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
m_model.summary()
a=time.time()
m_model.fit(x_train, y_train, batch_size=128, epochs=5)
print time.time() - a
a=time.time()
m_model.predict(x=x_test, batch_size=128)
print time.time() - a
'''
model.compile(optimizer='rmsprop',
          loss='categorical_crossentropy',
          metrics=['accuracy'])
model.summary()
a=time.time()
model.fit(x_train, y_train, batch_size=128, epochs=5)
print time.time() - a
a=time.time()
model.predict(x=x_test, batch_size=128)
print time.time() - a

这是使用4个GPU运行的GPU状态。

1个回答

1

我可以告诉你我认为的答案,但我自己没有完全解决。有一个错误报告引起了我的注意,但在multi_gpu_model源代码中它说:

    # Instantiate the base model (or "template" model).
    # We recommend doing this with under a CPU device scope,
    # so that the model's weights are hosted on CPU memory.
    # Otherwise they may end up hosted on a GPU, which would
    # complicate weight sharing.
    with tf.device('/cpu:0'):
        model = Xception(weights=None,
                         input_shape=(height, width, 3),
                         classes=num_classes)

我认为这是问题所在。尽管如此,我仍在努力使其正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接