Installing GPU version of TensorFlow™ for use in R on Windows

Intro

The other night I got TensorFlow™ (TF) and Keras-based text classifier in R to successfully run on my gaming PC that has Windows 10 and an NVIDIA GeForce GTX 980 graphics card, so I figured I’d write up a full walkthrough, since I had to make minor detours and the official instructions assume – in my opinion – a certain level of knowledge that might make the process inaccessible to some folks.

Why would you want to install and use the GPU version of TF? “TensorFlow programs typically run significantly faster on a GPU than on a CPU.” Graphics processing units (GPUs) are typically used to render 3D graphics for video games. As a result of the race for real-time rendering of more and more realistic-looking scenes, they have gotten really good at performing vector/matrix operations and linear algebra. While CPUs are still better for general purpose computing and there is some overhead in transferring data to/from the GPU’s memory, GPUs are a more powerful resource for performing those particular calculations.

Notes: For installing on Ubuntu, you can follow RStudio’s instructions. If you’re interested in a Python-only (sans R) installation on Linux, follow these instructions.

Prerequisites

  • An NVIDIA GPU with CUDA Compute Capability 3.0 or higher. Check your GPU’s compute capability here. For more details, refer to Requirements to run TensorFlow with GPU support.
  • A recent version of R – latest version is 3.4.0 at the time of writing.
    • For example, I like using Microsoft R Open (MRO) on my gaming PC with a multi-core CPU because MRO includes and links to the multi-threaded Intel Math Kernel Library (MKL), which parallelizes vector/matrix operations.
    • I also recommend installing and using the RStudio IDE.
    • You will need devtools: install.packages("devtools", repos = c(CRAN = "https://cran.rstudio.com"))
  • Python 3.5 (required for TF at the time of writing) via Anaconda (recommended):
    1. Install Anaconda3 (in my case it was Anaconda3 4.4.0), which will install Python 3.6 (at the time of writing) but we’ll take care of that.
    2. Add Anaconda3 and Anaconda3/Scripts to your PATH environment variable so that python.exe and pip.exe could be found, in case you did not check that option during the installation process. (See [these instructions][19] for how to do that.)
    3. Install Python 3.5 by opening up the Anaconda Prompt (look for it in the Anaconda folder in the Start menu) and running conda install python=3.5
    4. Verify by running python --version

Setting Up

CUDA & cuDNN

Step 0: Presumably you’ve got the latest NVIDIA drivers.

  1. Install CUDA Toolkit 8.0 (or later).
  2. Download and extract CUDA Deep Neural Network library (cuDNN) v5.1 (specifically), which requires signing up for a free NVIDIA Developer account.
  3. Add the path to the bin directory (where the DLL is) to the PATH system environment variable. (See these instructions for how to do that.) For example, mine is C:\cudnn-8.0\bin

TF & Keras in R

Once you’ve got R, Python 3.5, CUDA, and cuDNN installed and configured:

You may need to install the dev version of the processx package: devtools::install_github("r-lib/processx") because everything installed OK for me originally but when I ran devtools::update_packages() it gave me an error about processx missing, so I’m including this optional step.

  1. Install reticulate package for interfacing with Python in R: devtools::install_github("rstudio/reticulate")
  2. Install tensorflow package: devtools::install_github("rstudio/tensorflow")
  3. Install GPU version of TF (see this page for more details):
    library(tensorflow)
    install_tensorflow(gpu = TRUE)
    
  4. Verify by running:
    use_condaenv("r-tensorflow")
    sess = tf$Session()
    hello <- tf$constant('Hello, TensorFlow!')
    sess$run(hello)
    
  5. Install keras package: devtools::install_github("rstudio/keras")

You should be able to run RStudio’s examples now.

Hope this helps! :D

Posted on:
June 11, 2017
Length:
3 minute read, 583 words
Tags:
R ML
See Also:
Wikipedia Preview for R Markdown documents
Even faster matrix math in R on macOS with M1
Making Of: Session Tick visualization