如何最大限度地利用 R 中的 Tensorflow 2.0(使用 Keras 库)的 GPU 使用率?

use*_*379 2 r tf.keras tensorflow2.0

我在 GPU 上使用 R 和 Keras 以及 Tensorflow 2.0。

将第二个显示器连接到我的 GPU 后,我在深度学习脚本期间收到此错误:

在此输入图像描述

我的结论是 GPU 内存不足,解决方案似乎是以下代码:

import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.allow_growth = True  # dynamically grow the memory used on the GPU
config.log_device_placement = True  # to log device placement (on which device the operation ran)
                                    # (nothing gets printed in Jupyter, only if you run it standalone)
sess = tf.Session(config=config)
set_session(sess)  # set this TensorFlow session as the default session for Keras
Run Code Online (Sandbox Code Playgroud)

根据这篇文章: https://github.com/tensorflow/tensorflow/issues/7072#issuecomment-422488354

虽然这个代码不被 R 接受。它说

来自 Tensorflow 的意外令牌。

tf.ConfigProto() 中的错误:找不到函数“tf.ConfigProto”

如果我从这篇文章中理解正确的话,tensorflow 2.0似乎不接受此代码: https: //github.com/tensorflow/tensorflow/issues/33504

有谁知道如何使用 Keras 库和 Tensorflow 2.0 最大限度地提高 R 脚本的 GPU 使用率?

谢谢你!

Axe*_*man 5

要在 R 2.0 中使用keras或实现 GPU 内存增长,您需要在对象中找到正确的函数。tensorflowtensorflowtf

首先,找到您的 GPU 设备:

library(tensorflow)
gpu <- tf$config$experimental$get_visible_devices('GPU')[[1]]
Run Code Online (Sandbox Code Playgroud)

然后启用该设备的内存增长:

tf$config$experimental$set_memory_growth(device = gpu, enable = TRUE)
Run Code Online (Sandbox Code Playgroud)

您可以通过在 Rstudio 中键入tf$config$experimental$然后使用选项卡自动完成来找到更多相关功能。

由于这些功能被标记为实验性的,因此它们将来可能会改变或移动位置。