【发布时间】:2018-10-22 16:35:13
【问题描述】:
Keras 没有使用我的 GPU,尽管 tensorflow 似乎运行良好。我按照其他人的建议来检查 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 示例运行得快得多。 导入张量流 # 创建一个图表。 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 会自动继承这一点。
一个。
【问题讨论】:
-
我也试过这个:[
from keras import backend as K with K.tf.device('/gpu:1'): config = tensorflow.ConfigProto(intra_op_parallelism_threads=4,\ inter_op_parallelism_threads=4, allow_soft_placement=True,\ device_count = {'CPU' : 1, 'GPU' : 1}) session = tensorflow.Session(config=config) K.set_session(session)] 但这并没有为 Keras 启用 GPU,即使我在 shell 中确认 tensorflow 设备处于活动状态
标签: python tensorflow keras gpu cpu