keras 不使用 GPU,但 tensorflow 是

Keras 没有使用我的 GPU,即使 tensorflow 似乎运行良好。我按照其他人的建议检查张量流:


import tensorflow

from tensorflow.python.client import device_lib

print(device_lib.list_local_devices())

这使


[name: "/device:CPU:0"

device_type: "CPU"

memory_limit: 268435456

locality {

}

incarnation: 13541243483275802230

, name: "/device:GPU:0"

device_type: "GPU"

memory_limit: 6694395576

locality {

  bus_id: 1

  links {

  }

}

incarnation: 17715053295272939021

physical_device_desc: "device: 0, name: GeForce GTX 1070, pci bus id: 0000:08:00.0, compute capability: 6.1"

]

到目前为止一切顺利,但是当我在 Keras 中指定一个分类器并对其进行训练时,它以极快的速度运行。没有 GPU 加速的迹象:


classifier.fit(X_train, y_train, batch_size = 10, epochs = 100, verbose=1)

我试过这个:


with tensorflow.device('/gpu:0'):

    classifier.fit(X_train, y_train, batch_size = 10, epochs = 100)

同样的结果。除了速度和明显的 CPU 使用率之外,我不知道如何判断 Keras 是否在使用 GPU。


我还从 tensorflow 文档中运行了这个示例,在我的终端中,我可以清楚地看到它使用了 GPU。它比上面的 keras 示例运行得快得多。import tensorflow # 创建一个图。a = tensorflow.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a') b = tensorflow.constant([1.0, 2.0, 3.0, 4.0, 5.0 , 6.0], shape=[3, 2], name='b') c = tensorflow.matmul(a, b) # 创建一个会话,log_device_placement 设置为 True。sess = tensorflow.Session(config=tensorflow.ConfigProto(log_device_placement=True)) # 运行操作。打印(sess.run(c))


我非常感谢您的帮助,以找出 Keras 看不到我的 GPU 的原因


我使用 python 3.6.5,tensorflow-gpu 1.11.0(未安装 tensorflow),keras 2.2.4。我需要提一下,我不得不摆弄一段时间才能让 tensorflow 使用 GPU,我仍然不知道它为什么突然这样做,但现在它一直如此。我的假设是 Keras 会自动继承这一点。


萧十郎
浏览 341回答 2
2回答

泛舟湖上清波郎朗

您可以尝试删除 keras 并安装 keras-gpu(在 anaconda 中可用,也可以在 pip 中)如果您想确保使用with tensorflow.device('/gpu:0'):,请在“定义模型时”使用它:with tensorflow.device('/gpu:0'):    #blablablabla - layers for functional API model    classifier = Model(inputs, outputs) #or classifier = Sequential()    #blablabla - layers for sequential model 
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python