【问题标题】:TensorFlow with 4-GPU doesn't speed up the training带有 4-GPU 的 TensorFlow 不会加快训练速度
【发布时间】:2019-07-11 23:18:19
【问题描述】:

我的代码优先:

from sklearn.datasets.samples_generator import make_blobs
from matplotlib import pyplot
from numpy import where
from keras.utils import to_categorical

from keras.layers import Dense
from keras.models import Sequential
from keras.optimizers import SGD
from keras.utils import multi_gpu_model

X, y = make_blobs(n_samples=1000000, centers=3, n_features=3, cluster_std=2, random_state=2)

y = to_categorical(y)

n_train = 500000
trainX, testX = X[:n_train, :], X[n_train:, :]
trainY, testY = y[:n_train], y[n_train:]

model = Sequential()
model.add(Dense(50, input_dim=3, activation='relu', kernel_initializer='he_uniform'))
model.add(Dense(3, activation='softmax'))
p_model = multi_gpu_model(model, gpus=4)

opt = SGD(lr=0.01, momentum=0.9)
p_model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])

history = p_model.fit(trainX, trainY, validation_data=(testX, testY), epochs=20, verbose=1, batch_size=32)
_, train_acc = p_model.evaluate(trainX, trainY, verbose=0)
_, test_acc = p_model.evaluate(testX, testY, verbose=0)


print("Train: %.3f, Test: %.3f" % (train_acc, test_acc))

使用 4 个强大的 GPU 的方法如下:

p_model = multi_gpu_model(model, gpus=4)

在训练进行时,我可以看到以下内容(GPU 已充分利用?):

(tf_gpu) [martin@A08-R32-I196-3-FZ2LTP2 mlm]$ nvidia-smi 
Wed Jan 23 09:08:24 2019       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 410.79       Driver Version: 410.79       CUDA Version: 10.0     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla P40           Off  | 00000000:02:00.0 Off |                    0 |
| N/A   29C    P0    49W / 250W |  21817MiB / 22919MiB |      9%      Default |
+-------------------------------+----------------------+----------------------+
|   1  Tesla P40           Off  | 00000000:04:00.0 Off |                    0 |
| N/A   34C    P0    50W / 250W |  21817MiB / 22919MiB |      2%      Default |
+-------------------------------+----------------------+----------------------+
|   2  Tesla P40           Off  | 00000000:83:00.0 Off |                    0 |
| N/A   28C    P0    48W / 250W |  21817MiB / 22919MiB |      3%      Default |
+-------------------------------+----------------------+----------------------+
|   3  Tesla P40           Off  | 00000000:84:00.0 Off |                    0 |
| N/A   36C    P0    51W / 250W |  21817MiB / 22919MiB |      3%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0    114918      C   python                                     21807MiB |
|    1    114918      C   python                                     21807MiB |
|    2    114918      C   python                                     21807MiB |
|    3    114918      C   python                                     21807MiB |
+-----------------------------------------------------------------------------+

但是,与单 GPU 运行相比,甚至与我的 Mac 桌面运行相比,它根本没有加速。整个训练大约需要 20 分钟,与单 GPU 训练所用的时间几乎相同,并且比我个人 Mac 上的训练慢很多。这是为什么呢?

【问题讨论】:

  • 您的模型太小,甚至无法充分利用 1 个 GPU。如果您运行 model.summary() ,它将显示模型的参数数量。在您的情况下,它大约有 350 个,而受益于使用 GPU 的典型神经网络将有数百万个参数。

标签: tensorflow gpu


【解决方案1】:

将您的batch_size 增加到 32 以上。继续增加,直到您的 GPU 利用率达到完全。是的,这可能会影响您的模型,但它确实会显着提高您的性能。您必须为该 batch_size 找到最佳位置。

【讨论】:

    猜你喜欢
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2017-09-10
    • 2019-08-05
    相关资源
    最近更新 更多