keras是否会自动使用gpu? [英] Does keras use gpu automatically?

查看:520
本文介绍了keras是否会自动使用gpu?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎它会自动使用gpu,但我不知道为什么.

It seems like it uses gpu automatically, but I do not know why.

首先,我声明如下

tf_config = tf.ConfigProto( allow_soft_placement=True )
tf_config.gpu_options.allow_growth = True
sess = tf.Session(config=tf_config) 
keras.backend.set_session(sess)

然后我定义了以下模型

with K.tf.device('/gpu:0'):
    some keras model

很明显,它将使用gpu,并且我检查了它是否按预期使用了第一个gpu(索引为0).

This is obvious that it will use the gpu and I checked it uses the first gpu(with index 0) as I expected.

但是,我删除了该行

with K.tf.device('/gpu:0'):

并重新缩进所有keras模型.我运行了代码,仍然看起来像是使用第一个gpu(索引为0).

and re-indented all the keras model. I ran the code, it still seems like using first gpu(with index 0).

在我的Ubuntu上,我使用nvidia-smi命令检查gpu的内存使用情况,然后在Windows上查看了进程管理器.

On my ubuntu I used nvidia-smi command to check the gpu memory usage, and I looked on the process manager on my windows.

它们两者都占用gpu内存及其用法.

Both of them take the gpu memory and its usages.

据我所知,如果我不将tensorflow保留到其模型中,则不会使用gpu.但是使用Keras时,它似乎自动使用了gpu ...是因为我运行了代码

As far as I remember, tensorflow does not use gpu if I do not spare them to its model. But with Keras it seems like it uses gpu automatically ... is it because I ran the code

tf_config = tf.ConfigProto( allow_soft_placement=True )
tf_config.gpu_options.allow_growth = True
sess = tf.Session(config=tf_config) 
keras.backend.set_session(sess)

还是我想念其他原因吗?

or is there some other reason I am missing?

推荐答案

根据文档如果存在TensorFlow,它将默认使用GPU:

According to the documentation TensorFlow will use GPU by default if it exist:

如果TensorFlow操作同时具有CPU和GPU实施,则在将操作分配给设备时, GPU设备将被赋予优先级.例如,matmul同时具有CPU和GPU内核. 在具有设备cpu:0和gpu:0的系统上,将选择gpu:0来运行

If a TensorFlow operation has both CPU and GPU implementations, the GPU devices will be given priority when the operation is assigned to a device. For example, matmul has both CPU and GPU kernels. On a system with devices cpu:0 and gpu:0, gpu:0 will be selected to run

这篇关于keras是否会自动使用gpu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆