【问题标题】:how can I maximize the GPU usage of Tensorflow 2.0 from R (with Keras library)?如何最大限度地利用 R(使用 Keras 库)中的 Tensorflow 2.0 的 GPU 使用率?
【发布时间】:2019-11-21 19:30:20
【问题描述】:

我在 GPU 上使用带有 Keras 和 tensorflow 2.0 的 R。

将第二台显示器连接到我的 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

根据这篇文章: 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 使用率?

谢谢!

【问题讨论】:

  • @Axeman 太棒了!它返回 PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')。当我使用 tf$config$experimental$set_memory_growth(device = 0, enable = TRUE) 它说 Unrecognized device: 0.0
  • @Axeman 非常感谢!!这就是解决方案!
  • @Axeman 优雅的解决方案通过gpu解决它

标签: r tensorflow2.0 tf.keras


【解决方案1】:

要在 R 中使用 kerastensorflow 启用 GPU 内存增长,使用 tensorflow 2.0,您需要在 tf 对象中找到正确的函数。

首先,找到您的 GPU 设备:

library(tensorflow)
gpu <- tf$config$experimental$get_visible_devices('GPU')[[1]]

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

tf$config$experimental$set_memory_growth(device = gpu, enable = TRUE)

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

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

【讨论】:

  • 非常感谢您的解释!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-01
  • 2019-09-25
  • 2019-09-26
  • 2023-03-21
  • 2020-02-05
  • 2018-06-29
相关资源
最近更新 更多