In this article, you will learn how to set up a research environment for modern machine learning techniques, using R, Rstudio, Keras, Tensorflow, and Nvidia GPU.

AWS EC2 users

This is probably the easiest approach, and the following steps are used to set up an RStudio server on an AWS EC2 instance with GPU, Tensorflow and Keras pre-installed. If you use a non-linux operational system, this might be the best choice for you to avoid potential hassles. [Warning: you need to stop the instance to avoid unnecessary costs when you do not use it! But there is still some lower cost even after the instance is stopped. If you choose to terminate it, the files saved will be lost. You need to read AWS’s official documents for the exact pricing.]

  1. Before launching the instance, you probably need to increase the limits (default value: 0) for p2.xlarge instances. The following show where to submit the request and how to fill out the request. After submitting the request, you need to wait until the limit is increased.

  2. Launch a virtual machine With EC2 (you need to wait for 2 hours if you have just registered an AWS account with Amazon).

  3. Choose an Amazon Machine Image (AMI): Search RStudio and select RStudio Server with Tensorflow-GPU for AWS from AWS Marketplace. The Rstudio server software is free to use, the default instance type is GPU Compute Extra Large, and the price is $0.9/hr; there are storage and I/O request fees: $0.05 per GB-month of provisioned storage and $0.05 per 1 million I/O requests.

  4. Use the default settings and then launch the instance

  5. Follow the instruction from AWS, and you will be able to login an RStudio server from a browser. The following was the instruction from AWS in case you don’t know where to find it.
    • Launch product via 1-click
    • Visit http://<ec2_instance_public_dns>:8787 in your browser to access RStudio
    • Log in with the username “rstudio-user” and the instance_id of the instance as the password
    • Once logged in, please set a new password for this user:
      • Click the “Tools” menu dropdown
      • select “shell”
      • type “passwd” into the shell
      • enter the current password (instance_id) and the new password twice, hitting the enter key after each entry.
    • Begin using tensorflow-related tooling in R to make use of the GPU

      library(tensorflow)
      sess <- tf$Session()
      hello <- tf$constant('Hello, TensorFlow!')
      sess$run(hello)
  6. The GPU version Tensorflow and Keras have already been installed, so you can now try the MNIST Example from here to verify whether the deep learning platform works well.

Ubuntu users (organic installation)

In this section, you will learn how to set up a deep learning platform on Ubuntu.

The following is a workflow that has worked smoothly based on my experience. We choose Keras as the main framework and Tensorflow as the backend deep learning engine.

  • Install Ubuntu: If you are already using Ubuntu, just skip this step. Otherwise, it is strongly suggested that you use Ubuntu as the operating system for your research. If you are currently using another operating system, then a feasible solution is to install dual bootable systems for Ubuntu along with you current operating system. One may choose to use virtual machines on the current host system (say, Windows 10), but this is not recommended as deep learning will need Nvidia GPU for handling intensive computation that is not well supported by virtual machines. Moreover, since virtual machines need to share hardware resources with the host computer, the performance is not as good as a standalone Ubuntu system.
  • Install Keras and Tensorflow: If you do not have suitable Nvidia GPU installed on your computer, the following steps lead to a ready-to-go CPU based research environment.

    install.packages("keras")
    library(keras)
    install_keras()

    A CPU-based research environment may be fine for dealing with shallow machine learning when the computation is not very heavy. However, it is strongly suggested to set up a GPU-based system. The following are the steps:

    • Install Nvidia GPU driver based on which GPU installed. Use ubuntu-drivers devices to find the recommended GPU driver version, use sudo apt install nvidia-xxx to install the driver, and use nvidia-smi to verify if the driver has been successfully installed.
    • Install CUDA toolkit and cuDNN library by following the official instructions from Nvidia.
    • Install conda through installing Miniconda.
    • Install Tensorflow (GPU supported) using Keras on R.

      install_keras(method = "conda", tensorflow = "gpu")
      This will install a conda environment r-tensorflow, and tensorflow will be installed in the conda environment. In order to list all the conda environments on Ubuntu, you can use conda env list. Use source activate r-tensorflow and source deactivate to activate and deactivate the tensorflow environment, respectively.
  • Open a terminal, and run source activate r-tensorflow and then rstudio which is a very popular R GUI (graphical user interface). Instead of using the above command to activate r-tensorflow, you can also use use_condaenv("r-tensorflow") to activate it from within R. Now you have a GPU-based deep learning platform, and you can try a small example such as the MNIST Example from here to verify whether the deep learning platform works well.

Ubuntu users (using docker)

Because software and OS have been updated from time to time, you may not be able to smoothly go through all the steps mentioned above. Docker provides an easier way to quickly set up a working environment for R+Rstudio+Keras+Tensorflow+GPU.

The following steps were slightly modified from here, and have been tested successfully on Ubuntu 18.04 with Nvidia GeForce GTX 1070.

  1. Install Nvidia GPU driver (see above); use nvidia-smi to check if you have already installed the GPU driver

  2. Install docker

  3. Install nvidia-docker: if errors of no-permission occur, use sudo usermod -a -G docker $USER, and then reboot the computer.

  4. Build the docker container for deep learning with R using Keras
    • Before build it, change “rstudioTheLegendOfZelda” to “rstudio” in this file “gpu-keras-tidyverse/Dockerfile” so the user name and password will be both rstudio
    • It will take about 1 hour to install everything, and afterwards run rstudio server from http://localhost:8787/
    • Before doing any coding, run the following to update the packages devtools::install_github('rstudio/keras') keras::install_keras(tensorflow = 'gpu')
  5. Now you can try a small example such as the MNIST Example from here to verify whether the deep learning platform works well.