This tutorial to get tensorflow-gpu working in R was realized in Ubuntu 18.04 LTS
Check if your graphic card is Cuda-GPU enabled: https://developer.nvidia.com/cuda-gpus
Check your graphic card and the nvidia driver version installed in your computer
$ nvidia-smi
If you don't have the nvidia driver, you can download the required driver for your graphic card from here: https://www.nvidia.com/Download/index.aspx?lang=en-us
You can download the Cuda Toolkit from:
https://developer.nvidia.com/cuda-90-download-archive? (Cuda 9 recommended)
https://developer.nvidia.com/cuda-downloads (Cuda 10)
I recommended Cuda 9 because TensorFlow don't support 10 version: https://www.tensorflow.org/install/source#tested_build_configurations
Once you download it, it is important to follow the installation instructions provided in the web (to install also the keys)
$ sudo dpkg -i cuda-repo-ubuntu1704-9-0-local_9.0.176-1_amd64.deb
$ sudo apt-key add /var/cuda-repo-/7fa2af80.pub
$ sudo apt-get update
$ sudo apt-get install cuda
Now, you can check the Cuda version (normally in /usr/local/cuda):
$ cat /usr/local/cuda/version.txt
Check the nvcc compiler:
$ nvcc --version
And the cuda installation:
$ cd /usr/local/cuda-9.0/samples/1_Utilities/deviceQuery
$ sudo make
$ ./deviceQuery
You can download the cuda deep neural network library from here (nvidia login requested): https://developer.nvidia.com/cudnn
You should select the download for the previous installed cuda version and download:
cuDNN v7.x.x Library for Linux
cuDNN v7.x.x Runtime Library for Ubuntuxx.xx (Deb)
cuDNN v7.x.x Developer Library for Ubuntuxx.xx (Deb)
cuDNN v7.x.x Code Samples and User Guide for Ubuntuxx.xx (Deb)
Once downloaded, unpack the first archive and copy the following files to local cuda distribution:
$ sudo cp cuda/include/cudnn.h /usr/local/cuda/include
$ sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
Give read access to all users:
$ sudo chmod a+r /usr/local/cuda-9.0/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
$ sudo chmod a+r /usr/local/cuda/include/cudnn.h
Next, install in this order:
$ sudo dpkg -i libcudnn7_7.0.5.15-1+cuda9.0_amd64.deb
$ sudo dpkg -i libcudnn7-dev_7.0.5.15-1+cuda9.0_amd64.deb
$ sudo dpkg -i libcudnn7-doc_7.0.5.15-1+cuda9.0_amd64.deb
And finally, install the nvidia cuda Profile Tools:
$ sudo apt-get install libcupti-dev
Check the cudnn installation:
$ cd /usr/src/cudnn_samples_v7/
$ cd cudnn_samples_v7/mnistCUDNN
$ make clean && make
$ sudo ./mnistCUDNN
To ensure that Tensorflow can find your CUDA installation and use it properly, you need to add the path to the CUDA binaries to your PATH and LD_LIBRARY_PATH (for more information see the references).
It's important to modify the .profile if you will use RStudio, otherwise the .bashrc file.
Example; In .profile or in .bashrc add:
export CUDA_HOME=/usr/local/cuda/ (local for profile and lib for bashrc?)
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${CUDA_HOME}/lib64
PATH=${CUDA_HOME}/bin:${PATH}
export PATH
Install keras from github repository (in R console):
devtools::install_github("rstudio/keras")
Install tensorflow in gpu (in R console):
keras::install_keras(tensorflow = "gpu")
Example for verifying correct operation:
library(keras)
library(tensorflow)
sess = tf$Session()
hello <- tf$constant('Hello, TensorFlow!')
sess$run(hello)
By default, R selects GPU0 to compute and allocates the memory in all GPUs availables (GPU0, GPU1, ...). For use only one specific GPU and not the others, we have to change the devices visibles in cuda:
Sys.setenv(CUDA_VISIBLE_DEVICES = "0")
In this way, the others GPU devices will be availables for new executions at the same time. De otra manera, si quieres calcular usando todas las GPUs, también es posible:
https://keras.rstudio.com/articles/faq.html#how-can-i-run-a-keras-model-on-multiple-gpus
https://keras.rstudio.com/reference/multi_gpu_model.html
One possible failure is that there is no virtual environment on your system where you can run python. To solve this problem, a conda or virtualenv environment must be installed on your computer, or you can also install it together with tensorflow. It may also be necessary to add the location of the virtual environment to the $PATH variable. You can check that everything is correct from the R console:
To check that the tensorflow module is loaded correctly:
reticulate::py_module_available("tensorflow")
Information about the python version:
reticulate::py_config()
Information about the conda environment:
reticulate::conda_list()
Information about the modules:
reticulate::py_discover_config("keras") reticulate::py_discover_config("tensorflow")
...
https://tensorflow.rstudio.com/tools/local_gpu.html
https://medium.com/@taylordenouden/installing-tensorflow-gpu-on-ubuntu-18-04-89a142325138
https://medium.com/codezillas/step-by-step-guide-to-install-tensorflow-gpu-on-ubuntu-18-04-lts-6feceb0df5c0
https://sndean.github.io/blog/2018/03/10/setting-up-keras-for-r-configured-to-run-on-gpus/
https://medium.com/intro-to-artificial-intelligence/install-tensorflow-gpu-on-ubuntu18-04-ff311a8bd4d6
https://tensorflow.rstudio.com/tensorflow/articles/installation.html
https://github.com/rstudio/reticulate
https://cran.r-project.org/web/packages/tensorflow/tensorflow.pdf
https://cran.r-project.org/web/packages/keras/keras.pdf
https://cran.r-project.org/web/packages/reticulate/reticulate.pdf