how can I maximize the GPU usage of Tensorflow 2.0 from R (with Keras library)?

流过昼夜 提交于 2019-12-02 07:32:11

问题


I use R with Keras and tensorflow 2.0 on the GPU.

After connecting a second monitor to my GPU, I receive this error during a deep learning script:

I concluded that the GPU is short of memory and a solution seems to be this code:

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

According to this post: https://github.com/tensorflow/tensorflow/issues/7072#issuecomment-422488354

Although this code is not accepted by R. It says

unexpecterd token from Tensorflow.

Error in tf.ConfigProto() : could not find function "tf.ConfigProto"

It seems that tensorflow 2.0 does not accept this code if I understand correct from this post: https://github.com/tensorflow/tensorflow/issues/33504

Does anyone know how I can maximize the GPU usage from my R script with Keras library and Tensorflow 2.0 ?

Thank you!


回答1:


To enable GPU memory growth using keras or tensorflow in R, with tensorflow 2.0, you need to find the correct functions in the tf object.

First, find your GPU device:

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

Then enable memory growth for that device:

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

You can find more relevant functions by typing tf$config$experimental$ and then using tab autocomplete in Rstudio.

Since these functions are labeled as experimental, they will likely change or move location in the future.



来源:https://stackoverflow.com/questions/58982467/how-can-i-maximize-the-gpu-usage-of-tensorflow-2-0-from-r-with-keras-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!