5 min read

[box type=”info” align=”” class=”” width=””]The following is an excerpt from the book Neural Networks with R, Chapter 7, Use Cases of Neural Networks – Advanced Topics, written by Giuseppe Ciaburro and Balaji Venkateswaran. In this post, we see how to integrate popular deep learning libraries and frameworks like TensorFlow with R for effective neural network modeling.[/box]

TensorFlow is an open source numerical computing library provided by Google for machine intelligence. It hides all of the programming required to build deep learning models and gives the developers a black box interface to program. The Keras API for TensorFlow provides a high-level interface for neural networks. Python is the de facto programming language for deep learning, but R is catching up. Deep learning libraries are now available with R and a developer can easily download TensorFlow or Keras similar to other R libraries and use them.

In TensorFlow, nodes in the graph represent mathematical operations, while the graph edges represent the multidimensional data arrays (tensors) communicated between them. TensorFlow was originally developed by the Google Brain Team within Google’s machine intelligence research for machine learning and deep neural networks research, but it is now available in the public domain. TensorFlow exploits GPU processing when configured appropriately.

The generic use cases for TensorFlow are as follows:

  • Image recognition
  • Computer vision
  • Voice/sound recognition
  • Time series analysis
  • Language detection
  • Language translation
  • Text-based processing
  • Handwriting Recognition (HWR)
  • Many others

Integrating Tensorflow with R

In this section, we will see how we can bring TensorFlow libraries into R. This will open up a huge number of possibilities with deep learning using TensorFlow with R. In order to use TensorFlow, we must first install Python. If you don’t have a Python installation on your machine, it’s time to get it.

Python is a dynamic Object-Oriented Programming (OOP) language that can be used for many types of software development. It offers strong support for integration with other languages and programs, is provided with a large standard library, and can be learned within a few days. Many Python programmers can confirm a substantial increase in productivity and feel that it encourages the development of higher quality code and maintainability. Python runs on Windows, Linux/Unix, macOS X, OS/2, Amiga, Palm Handhelds, and Nokia phones. It also works on Java and .NET virtual machines. Python is licensed under the OSI-approved open source license; its use is free, including for commercial products.

[box type=”shadow” align=”” class=”” width=””]If you do not know which version to use, there is a document that could help you choose. In principle, if you have to start from scratch, we recommend choosing Python 3, and if you need to use third-party software packages that may not be compatible with Python 3, we recommend using Python 2.7. All information about the available versions and how to install Python is given at https://www.python.org/[/box]

After properly installing the Python version of our machine, we have to worry about installing TensorFlow. We can retrieve all library information and available versions of the operating system from the following link: https://www.tensorflow.org/ .

Also, in the install section, we can find a series of guides that explain how to install a version of TensorFlow that allows us to write applications in Python. Guides are available for the following operating systems:

  • Installing TensorFlow on Ubuntu
  • Installing TensorFlow on macOS X
  • Installing TensorFlow on Windows
  • Installing TensorFlow from sources

For example, to install Tensorflow on Windows, we must choose one of the following types:

  • TensorFlow with CPU support only
  • TensorFlow with GPU support

To install TensorFlow, start a terminal with privileges as administrator. Then issue the appropriate pip3 install command in that terminal. To install the CPU-only version, enter the following command:

C:> pip3 install --upgrade tensorflow

A series of code lines will be displayed on the video to keep us informed of the execution of the installation procedure, as shown in the following figure:

Note: The output screenshot has been cropped for clarity purposes

At this point, we can return to our favorite environment; I am referring to the R development environment. We will need to install the interface to TensorFlow. The R interface to TensorFlow lets you work productively using the high-level Keras and Estimator APIs, and when you need more control, it provides full access to the core TensorFlow API. To install the R interface to TensorFlow, follow the steps below

  1. First, install the tensorflow R package from CRAN as follows:
install.packages("tensorflow")

 2. Then, use the install_tensorflow() function to install TensorFlow (for a proper installation procedure, you must have administrator privileges):

library(tensorflow)

install_tensorflow()

 3. We can confirm that the installation succeeded:

sess = tf$Session()

hello <- tf$constant('Hello, TensorFlow!')

sess$run(hello)

This will provide you with a default installation of TensorFlow suitable for use with the tensorflow R package.

Read on if you want to learn about additional installation options.

 4. If you want to install a version of TensorFlow that takes advantage of NVIDIA GPUs, you need to have the correct CUDA libraries installed. In the following code, we can check the success of the installation:

 library(tensorflow)

> sess = tf$Session()

> hello <- tf$constant('Hello, TensorFlow!')

> sess$run(hello)

b'Hello, TensorFlow!'

Integrating Keras with R

Keras is a set of open source neural network libraries coded in Python. It is capable of running on top of MxNet, TensorFlow, or Theano. The steps to install Keras in RStudio are very simple. The following code snippet gives the steps for installation and we can check whether Keras is working by checking the load of the MNIST dataset.

By default, RStudio loads the CPU version of TensorFlow. Once Keras is loaded, we have a powerful set of deep learning libraries that can be utilized by R programmers to execute neural networks and deep learning.

To install Keras for R, follow these steps:

  1. Run the following code
install.packages("devtools")

devtools::install_github("rstudio/keras")

 2. At this point, we load the keras library:

library(keras)

 3. Finally, we check whether keras is installed correctly by loading the MNIST dataset:

> data=dataset_mnist()

 

If you found this excerpt useful, make sure you check out the book Neural Networks with R, containing an interesting coverage of many such useful and insightful topics.

Data Science Enthusiast. A massive science fiction and Manchester United fan. Loves to read, write and listen to music.

LEAVE A REPLY

Please enter your comment!
Please enter your name here