Before implementing a deep neural network, we need to set up our system and configure it so that we can apply a variety of deep learning techniques. This recipe assumes that you have the Anaconda distribution installed on your system.
Setting up the environment
Getting ready
Let's configure our system for deep learning. It is recommended that you create a deep learning environment in Anaconda. If you have an older version of R in the conda environment, you need to update your R version to 3.5.x or above.
You also need to install the CUDA and cuDNN libraries for GPU support. You can read more about the prerequisites at https://tensorflow.rstudio.com/tools/local_gpu.html#prerequisties.
Please note that if your system does not have NVIDIA graphics support, then GPU processing cannot be done.
How to do it...
Let's create an environment in Anaconda (ensure that you have R and Python installed):
- Go to Anaconda Navigator from the Start menu.
- Click on Environments.
- Create a new environment and name it. Make sure that both the Python and R options are selected, as shown in the following screenshot:
- Install the keras library in R using the following command in RStudio or by using the Terminal of the conda environment created in the previous step:
install.packages("keras")
- Install keras with the tensorflow backend.
To install the CPU version, please refer to the following code:
install_keras(method = c("auto", "virtualenv", "conda"), conda = "auto", version = "default", tensorflow = "default", extra_packages = c("tensorflow-hub"))
To install the GPU version, please refer to the following steps:
- Ensure that you have met all the installation prerequisites, including installing the CUDA and cuDNN libraries.
- Set the tensorflow argument's value to gpu in the install_keras() function:
install_keras(tensorflow = "gpu")
The preceding command will install the GPU version of keras in R.
How it works...
Keras and TensorFlow programs can be executed on both CPUs and GPUs, though these programs usually run faster on GPUs. If your system does not support an NVIDIA GPU, you only need to install the CPU version. However, if your system has an NVIDIA GPU that meets all the prerequisites and you need to run performance-critical applications, you should install the GPU version. To run the GPU version of TensorFlow, we need an NVIDIA GPU, and then we need to install a variety of software components (CUDA Toolkit v9.0, NVIDIA drivers, and cuDNN v7.0) on the system.
In steps 1 to 3, we created a new conda environment with both the R and Python kernels installed. In steps 4 and 5, we installed the keras library in the environment we created.
There's more...
The only supported installation method on Windows is conda. Therefore, you should install Anaconda 3.x for Windows before installing keras. The keras package uses the TensorFlow backend by default. If you want to switch to Theano or CNTK, call the use_backend() function after loading the keras library.
For the Theano backend, use the following command:
library(keras)
use_backend("theano")
For the CNTK backend, use the following command:
library(keras)
use_backend("cntk")
Now, your system is ready to train deep learning models.
See also
You can find out more about the GPU version installation of keras and its prerequisites here: https://tensorflow.rstudio.com/tools/local_gpu.html.