
A (hopefully) Useful Guide to Installing GPU Version of Keras in R
Keras is a fairly new package for R that has existed in Python for a while. It lets you construct, run and evaluate different neural network architectures. The package lets you use Python (and therefore Python packages) as the motor of the analysis while still having all the code in R, which is extremely useful for R users (myself included) who want to learn about or work with neural networks but don’t know Python.
The Keras package can be installed in 2 ways. On one hand we have the CPU version which is fairly easy to install. A quick google search shows various official and unofficial guides on how to do it (the clearest on in my opinion being the one from CRAN). On the other hand we have the GPU version which takes advantage of your computer’s GPU to parallelize the matrix operations and therefore runs faster, but it has 2 downsides. First, you need a CUDA-Enabled GPU, not something everyone has but something you should eventually get if you’re interested in training deep neural networks. The second problem is its a exponentially harder to install and there’s way less information on how to do it. This article aims to, at least, leave a dent on this problem.
Installing GPU Version of Keras in R
Follow these 6 semi-easy steps in order to get the begin achieving your deeplearning goals. These were all tested on Windows 10 using RStudio, but should be the same for other operating systems.
- Make sure you have a CUDA-Enabled GPU, these are all NVIDIA GPU’s and you can check if yours is compatible here.
- Download the Anaconda installer compatible with your operating system that supports Python 3. Install said installer pressing next and agreeing. On the advanced options window tick the first box so Anaconda adds the installation directory to you system PATH. This will allow the Keras in R package to find it, if you don’t tick it you can add it to your system PATH manually later but this is not something trivial for everybody. You can also make Anaconda your default Python, but it shouldn't be necessary.
- Download and install Cuda Toolkit 9.0 (Sept. 2017). Agree to the classic never-read agreements and choose the express installation. This step quite slow, or was for me at least, but straightforward.
- Download the NVIDIA CUDA Deep Neural Network library (cuDNN). This allows you to use the graphics card for more than graphics processing. This step is (unnecessarily) long because it requires a membership of the NVIDIA Developer Program, which is is really just creating an account. Create and account by clicking on the green join button and download cuDNN after login in. Download the latest version compatible with CUDA 9.0 and your operating system which, at the time of writing this, is cuDNN v7.0.5.
- Extract the files from the zip file, you should have 3 folders: bin, include and lib. Go to the NVIDIA GPU Computing Toolkit directory (by default this is something similar to :\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0 ) where you have, among others, three folders with the same names as the ones extractad from the cuDNN. Move the files located in the bin folder of cuDNN to the bin folder in the NVIDIA GPU computing toolkit,choose replace when prompted. Do the same for the include and lib folders.
- Open RStudio (finally) and run the following code. This code first installs and load the devtools package which allows you to install Keras from github. Keras can also be installed for CRAN but you’re not getting the latest version and this may cause problems with Python. Next you need to install the reticulate package which serves as an interface for the Python modules. Next you install the Keras package from github (aided by the previously installed devtools package). After installing Keras form github you should be able to easily install the GPU enabled version of Keras.
install.packages(“devtools”)
library(devtools)
install.packages(“reticulate”)
library(reticulate)
install_github(“rstudio/keras”)
library(keras)
install_keras(tensorflow = “gpu”)reticulate::py_config()
reticulate::py_module_available(“keras”)
The last two lines of code should give you information on your Python configuration and print TRUE ,confirming that everything is working correctly.
Now you can learn from some of the various tutorial around the internet and/ or implement your own neural networks.
Hope this Helps!

