Chapter 1 Installing Keras

Throughout these notes, I use R and RStudio (various versions) running on macOS Catalina (version 10.15.4) on a late-2015 iMac with a 3.2 GHz Intel Core i5, 8 GB memory and an AMD (i.e. non-Nvidia) graphics card. Not having an Nvidia graphics card means I cannot use the GPU version of TensorFlow (the backend, or computational engine for Keras) so we will not be demonstrating this way of accelerating computations.

Chances are this chapter may also be useful if your machine runs Linux, but things may be entirely different on Windows.

Keras is an API (Application Programming Interface) that provides access for the R user to the neural-network computational framework provided by TensorFlow. In other words, using Keras, it is possible to write high-level code in R that executes computations in TensorFlow, without the user needing to interact with TensorFlow directly.

I had a smoother experience installing and using Keras through RStudio than in plain R. In RStudio, I executed the following (as also recommended in Chollet and Allare (2018), Section 3.2.2).

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

The first two lines, respectively, install the keras package plus its dependencies, and load its namespace so it can be used. These two are standard R commands. The dependencies of the keras package include, amongst others, the packages tensorflow and reticulate (more on this package below).

The install_keras routine (from the keras package) can do some things automatically, so there is some scope for errors there. Indeed, there are a multitude of posts on websites such as Stackoverflow where people share their experience with this command and ask for help (try https://stackoverflow.com/search?q=install_keras).

To better understand what exactly happened on my computer when I executed install_keras(), I should say a few words about what was already there when this command was executed.

My machine already had Anaconda installed on it. It comes with conda, a package management system. On installing Anaconda, a base conda environment was created. A conda environment is a directory that contains a particular collection of conda-installed packages. The base environment contains a standard installation of Anaconda.

I then used conda to create an additional environment (which I called tf) in which I installed the Python modules for TensorFlow (incidentally, this already included the Python version of Keras). So before executing install_keras() in RStudio, I had the following conda environments on my machine.

(base) Piotrs-iMac:~ piotr$ conda info --envs
# conda environments:
#
base                  *  /Users/piotr/opt/anaconda3
tf                       /Users/piotr/opt/anaconda3/envs/tf

On execution, install_keras ignored the fact that Python (v3.8.1) and its modules for Tensorflow and Keras were already installed on my machine (as above). This did not surprise me, as I used the default parameters in install_keras (and therefore also in install_tensorflow, which is automatically launched by install_keras). I believe that it should be possible to make use of the existing installation of Python/TensorFlow/Keras by passing suitable parameters to install_keras (and/or to install_tensorflow). For complete lists of parameters, use the below.

help(install_keras)
help(install_tensorflow)

(As is apparent from the help file to install_tensorflow, version 2.2.0 of the tensorflow package installs Python version 3.6 by default.)

For some reason, install_keras also ignored the fact that conda was already installed on my system, and asked me if it could install Miniconda to help it create the right environments and install the appropriate packages for TensorFlow and Keras. I agreed, and on completion, I had more conda environments on my system:

(base) Piotrs-iMac:~ piotr$ conda info --envs
# conda environments:
#
                         /Users/piotr/Library/r-miniconda
                         /Users/piotr/Library/r-miniconda/envs/r-reticulate
base                  *  /Users/piotr/opt/anaconda3
tf                       /Users/piotr/opt/anaconda3/envs/tf

Searching for all files named tensorflow and keras in the r-miniconda directory confirms that the right content has been installed.

(base) Piotrs-iMac:~ piotr$ cd Library/r-miniconda
(base) Piotrs-iMac:r-miniconda piotr$ find . -name "tensorflow" -print
./envs/r-reticulate/lib/python3.6/site-packages/tensorflow
./envs/r-reticulate/lib/python3.6/site-packages/tensorflow/include/tensorflow
./envs/r-reticulate/lib/python3.6/site-packages/tensorflow/xla_aot_runtime_src/tensorflow
(base) Piotrs-iMac:r-miniconda piotr$ find . -name "keras" -print
./envs/r-reticulate/lib/python3.6/site-packages/keras
./envs/r-reticulate/lib/python3.6/site-packages/tensorflow/keras
./envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras
./envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/api/keras
./envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/api/_v2/keras
./envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/api/_v1/keras

However, this is of course entirely Python content. How can we make it usable in R? This is exactly why the keras package imports the reticulate package (https://cran.r-project.org/web/packages/reticulate/index.html), which provides an R interface to Python. Note the corresponding name (r-reticulate) of the Miniconda environment above. The following is from the description of the reticulate package.

Interface to ‘Python’ modules, classes, and functions. When calling into ‘Python,’ R data types are automatically converted to their equivalent ‘Python’ types. When values are returned from ‘Python’ to R they are converted back to R types.

Having done the above, we should now be ready to go. Just a small word of caution: you may have to re-execute

library(keras)

to get things to work, the possible reason being that install_tensorflow restarts the R session (but apparently this only happens in RStudio).

References

Chollet, F., and J. J. Allare. 2018. Deep Learning with R. Manning.