【问题标题】:Keras ignores the GPU when using TensorFlow backendKeras 在使用 TensorFlow 后端时会忽略 GPU
【发布时间】:2023-03-14 19:43:01
【问题描述】:

文档说,当使用 TensorFlow 后端时,如果检测到 Keras 会自动在 GPU 上运行。我登录到远程 GPU,并尝试运行 Keras 程序,但出于某种原因我只使用 CPU。如何强制我的 Keras 程序在 GPU 上运行以加快速度?

如果有帮助,模型如下所示:

model = Sequential()
model.add(SimpleRNN(out_dim, input_shape = (X_train.shape[1], X_train.shape[2]), return_sequences = False))
model.add(Dense(num_classes, activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer = "adam", metrics = ['accuracy'])
hist = model.fit(X_train, dummy_y, validation_data=(X_test, dummy_y_test), nb_epoch = epochs, batch_size = b_size)

这是which python 的输出以及 Keras 正在使用 TensorFlow 后端的证明:

user@GPU6:~$ which python
/mnt/data/user/pkgs/anaconda2/bin/python
user@GPU6:~$ python
Python 2.7.12 |Anaconda custom (64-bit)| (default, Jul  2 2016, 17:42:40) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import keras
Using TensorFlow backend.

这是nvidia-smi 的输出。我有几个类似上面的进程正在运行,但它们只使用 CPU:

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 367.57                 Driver Version: 367.57                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX TIT...  Off  | 0000:03:00.0     Off |                  N/A |
| 26%   27C    P8    13W / 250W |      9MiB /  6082MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  GeForce GTX TIT...  Off  | 0000:83:00.0     Off |                  N/A |
| 26%   31C    P8    13W / 250W |      9MiB /  6082MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   2  GeForce GTX TIT...  Off  | 0000:84:00.0     Off |                  N/A |
| 26%   31C    P8    14W / 250W |      9MiB /  6082MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID  Type  Process name                               Usage      |
|=============================================================================|
|    0      2408    G   Xorg                                             9MiB |
|    1      2408    G   Xorg                                             9MiB |
|    2      2408    G   Xorg                                             9MiB |
+-----------------------------------------------------------------------------+

我的所有进程都没有在 GPU 上运行。我该如何解决这个问题?

【问题讨论】:

    标签: tensorflow keras


    【解决方案1】:

    你可能安装了 tensorflow 的 CPU 版本。

    由于您似乎使用的是 Anaconda 和 py2.7: 关注 these steps 使用 py2.7 在 conda 环境中安装 GPU 版本的 tensorflow

    conda create -n tensorflow
    source activate tensorflow
    pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.0.1-cp27-none-linux_x86_64.whl
    

    看到这个github issue

    【讨论】:

      猜你喜欢
      • 2018-07-18
      • 2020-06-13
      • 2018-02-15
      • 2021-11-28
      • 1970-01-01
      • 2018-10-02
      • 2017-12-23
      • 1970-01-01
      • 2019-10-09
      相关资源
      最近更新 更多