似乎它会自动使用 gpu,但我不知道为什么。
首先,我声明如下
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)。
但是后来,我删除了该行
with K.tf.device('/gpu:0'):
并重新缩进所有 keras 模型。我运行了代码,它似乎仍然使用第一个 gpu(索引为 0)。
在我的 ubuntu 上,我使用 nvidia-smi 命令检查 GPU 内存使用情况,并查看了 Windows 上的进程管理器。
它们都占用 gpu 内存及其用法。
据我所知,如果我不将它们分配给它的模型,tensorflow 不会使用 GPU。但是使用 Keras 似乎它会自动使用 gpu ......是因为我运行了代码
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)
还是我失踪的其他原因?
最佳答案
根据documentation如果 GPU 存在,TensorFlow 将默认使用它:
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
关于tensorflow - keras 会自动使用 gpu 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53926627/