fbpx
Using Keras and TensorFlow in R Using Keras and TensorFlow in R
Keras and Tensorflow are two very powerful packages that are normally accessed via python. Since the packages were developed for python... Using Keras and TensorFlow in R

Keras and Tensorflow are two very powerful packages that are normally accessed via python. Since the packages were developed for python they may have the illusion of being out of reach for R users. However, this is not the case as the Keras and Tensorflow packages may be set up to be used in an R environment. This article will detail the setup process required to run Keras and Tensorflow in an R environment. 

[Related Article: What is TensorFlow?]

Why is it useful to know how to use Tensorflow and Keras in R? Being able to combine the robustness of R’s statistical capabilities with the power of Tensorflow and Keras, allows for some great benefits in data science projects. Tensorflow is the foundation on which Keras runs. Tensorflow does much of the heavy lifting while Keras is a high-level API that accesses Tensorflow. The combination of the packages allows you to access many cutting edge deep learning and ML methods. 

The first step is to install python on your local machine. My preferred method is to install Anaconda Navigator which allows you to manage python packages, environments, and channels without using a command line. Navigate to the Anaconda Navigator home page and download the client to your local machine. As a heads up, the download for this includes a large number of extra packages which means the size of the download is a bit large. 

Once Anaconda Navigator has downloaded you can open the client. It should open to something similar to the following:

Keras and tensorflow 

A great benefit of Anaconda Navigator is that it makes accessing python related environments rather easy. For example, we see a tile for jupyter notebooks on the home page. To launch a jupyter notebook we simply would need to click on the launch button within the jupyter tile and the notebook would open in our browser. However, our purpose here is to access Tensorflow and Keras in R. 

Now that we have python installed on our machine, the next step is to create a python environment that contains the TensorFlow package. We will create this environment directly from R by installing the TensorFlow and keras packages into our R environment. 

zinstall.packages(‘keras’)

library(keras)

install_keras()

 

Running these commands in R will create a python environment titled “r-reticulate”. Which we can see in our Anaconda Navigator.

Keras and tensorflow

Now, we can run the python environment from our R studio session using the following commands.

#launch python environment using reticulate

reticulate::use_condaenv(“r-reticulate”)

require(keras)

require(tensorflow)

 

These are now available to use in our R session. This means that we can begin creating fantastic deep learning models that utilize the cutting-edge power of these packages. 

[Related Article: Deep Learning in R with Keras]

Jacey Heuer

Jacey Heuer

Jacey Heuer is a data scientist currently working in the retail and e-commerce industry. He holds master’s degrees from Iowa State University in business and data science. He has analytics experience across many industries including energy, financial services and real estate. Jacey also is a data science author publishing educational content for the PluralSight.com platform. He enjoys reading, writing and learning about all data science topics. His particular interests are in probability theory, machine learning, classical statistics and the practical application of it in business.

1