Book Image

R Deep Learning Cookbook

By : PKS Prakash, Achyutuni Sri Krishna Rao
Book Image

R Deep Learning Cookbook

By: PKS Prakash, Achyutuni Sri Krishna Rao

Overview of this book

Deep Learning is the next big thing. It is a part of machine learning. It's favorable results in applications with huge and complex data is remarkable. Simultaneously, R programming language is very popular amongst the data miners and statisticians. This book will help you to get through the problems that you face during the execution of different tasks and Understand hacks in deep learning, neural networks, and advanced machine learning techniques. It will also take you through complex deep learning algorithms and various deep learning packages and libraries in R. It will be starting with different packages in Deep Learning to neural networks and structures. You will also encounter the applications in text mining and processing along with a comparison between CPU and GPU performance. By the end of the book, you will have a logical understanding of Deep learning and different deep learning packages to have the most appropriate solutions for your problems.
Table of Contents (17 chapters)
Title Page
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Installing TensorFlow in R


This section will cover another very popular open source machine learning package, TensorFlow, which is very effective in building deep learning models.

Getting ready

TensorFlow is another open source library developed by the Google Brain Team to build numerical computation models using data flow graphs. The core of TensorFlow was developed in C++ with the wrapper in Python. The tensorflow package in R gives you access to the TensorFlow API composed of Python modules to execute computation models. TensorFlow supports both CPU- and GPU-based computations.

The tensorflow package in R calls the Python tensorflow API for execution, which is essential to install the tensorflow package in both R and Python to make R work. The following are the dependencies for tensorflow:

  • Python 2.7 / 3.x
  • R (>3.2)
  • devtools package in R for installing TensorFlow from GitHub
  • TensorFlow in Python
  • pip

How to do it...

  1. Once all the mentioned dependencies are installed, tensorflow can be installed from devtools directly using the install_github command as follows:
devtools::install_github("rstudio/tensorflow")
  1. Before loading tensorflow in R, you need to set up the path for Python as the system environment variable. This can be done directly from the R environment, as shown in the following command:
Sys.setenv(TENSORFLOW_PYTHON="/usr/bin/python")
library(tensorflow)

If the Python tensorflow module is not installed, R will give the following error:

Error raised by R if tensorflow in Python is not installed

tensorflow in Python can be installed using pip:

pip install tensorflow # Python 2.7 with no GPU support
pip3 install tensorflow # Python 3.x with no GPU support
pip install tensorflow-gpu # Python 2.7 with GPU support
pip3 install tensorflow-gpu # Python 3.x with GPU support

How it works...

TensorFlow follows directed graph philosophy to set up computational models where mathematical operations are represented as nodes with each node supporting multiple input and output while the edges represent the communication of data between nodes. There are also edges known as control dependencies in TensorFlow that do not represent the data flow; rather the provide information related to control dependence such as node for the control dependence must finish processing before the destination node of control dependence starts executing.

An example TensorFlow graph for logistic regression scoring is shown in the following diagram:

TensorFlow graph for logistic regression

The preceding figure illustrates the TensorFlow graph to score logistic regression with optimized weights:

The MatMul node performs matrix multiplication between input feature matrix X and optimized weight β. The constant C is then added to the output from the MatMul node. The output from Add is then transformed using the Sigmoid function to output Pr(y=1|X).

See also

Get started with TensorFlow in R using resources at https://rstudio.github.io/tensorflow/.