Keras对GPU的训练速度没有改进(部分GPU使用?!)

Ish*_*wal 10 keras tensorflow

我试图在我的Jupyter笔记本上的GPU p2.xlarge实例上的GPU而不是CPU上训练我的模型.我正在使用tensorflow-gpu后端(仅tensorflow-gpu安装和提及requirements.txt而不是tensorflow).

与使用CPU相比,在这些实例上训练模型时,我没有看到任何速度提升,事实上我得到的每个时期的训练速度几乎与我在4核笔记本电脑CPU上获得的相同(p2.xlarge也有4个vCPU)搭配特斯拉K80 GPU).我不确定是否需要对我的代码进行一些更改以适应GPU可以提供的更快/并行处理.我粘贴到我的模型的代码下面:

model = Sequential()
model.add(recurrent.LSTM(64, input_shape=(X_np.shape[1], X_np.shape[2]),
                        return_sequences=True))
model.add(recurrent.LSTM(64, return_sequences = False))
model.add(core.Dropout(0.1))
model.add(core.Dense(3, activation='softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = 'rmsprop', metrics=['accuracy'])

model.fit(X_np, y_np, epochs=100, validation_split=0.25)
Run Code Online (Sandbox Code Playgroud)

同样有趣的是,每次使用GPU检查GPU状态时,GPU似乎都在利用其处理能力的50%-60%和几乎所有内存nvidia-smi(但在未经过培训时,它们分别降至0%和1MiB):

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 384.81                 Driver Version: 384.81                    |
|-------------------------------+----------------------+----------------------+
| 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 K80           On   | 00000000:00:1E.0 Off |                    0 |
| N/A   47C    P0    73W / 149W |  10919MiB / 11439MiB |     52%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1665      C   ...ubuntu/aDash/MLenv/bin/python 10906MiB |
+-----------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

此外,如果您想查看有关使用Jupyter Notebook的GPU的日志:

[I 04:21:59.390 NotebookApp] Kernel started: c17bc4d1-fa15-4b0e-b5f0-87f90e56bf65
[I 04:22:02.241 NotebookApp] Adapting to protocol v5.1 for kernel c17bc4d1-fa15-4b0e-b5f0-87f90e56bf65
2017-11-30 04:22:32.403981: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2017-11-30 04:22:33.653681: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2017-11-30 04:22:33.654041: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: Tesla K80 major: 3 minor: 7 memoryClockRate(GHz): 0.8235
pciBusID: 0000:00:1e.0
totalMemory: 11.17GiB freeMemory: 11.10GiB
2017-11-30 04:22:33.654070: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: Tesla K80, pci bus id: 0000:00:1e.0, compute capability: 3.7)
2017-11-30 04:22:34.014329: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: Tesla K80, pci bus id: 0000:00:1e.0, compute capability: 3.7)
Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K80, pci bus id: 0000:00:1e.0, compute capability: 3.7
2017-11-30 04:22:34.015339: I tensorflow/core/common_runtime/direct_session.cc:299] Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K80, pci bus id: 0000:00:1e.0, compute capability: 3.7

2017-11-30 04:23:22.426895: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: Tesla K80, pci bus id: 0000:00:1e.0, compute capability: 3.7)
Run Code Online (Sandbox Code Playgroud)

请提出可能存在的问题.非常感谢大家看这个!

Dan*_*ler 19

发生这种情况是因为您正在使用LSTM图层.

Tensorflow对LSTM层的实现并不是那么好.原因可能是循环计算不是并行计算,GPU非常适合并行处理.

我根据自己的经验确认:

  • 在我的模型中使用LSTM获得了可怕的速度
  • 决定测试模型去除所有LSTM(获得纯卷积模型)
  • 由此产生的速度简直令人惊讶!

这篇关于使用GPU和tensorflow的文章也证实了这一点:

可能的解决方案?

您可以尝试使用新的CuDNNLSTM,它似乎专门用于使用GPU.

我从来没有对它进行过测试,但你最有可能获得更好的性能.

我没有测试过的另一件事,我不确定它是出于这个原因设计的,但我怀疑它是:你可以放入unroll=True你的LSTM层.有了这个,我怀疑经常性的计算将会以并行的方式进行转换.