Reader small image

You're reading from  R Deep Learning Essentials. - Second Edition

Product typeBook
Published inAug 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788992893
Edition2nd Edition
Languages
Tools
Right arrow
Authors (2):
Mark Hodnett
Mark Hodnett
author image
Mark Hodnett

Mark Hodnett is a data scientist with over 20 years of industry experience in software development, business intelligence systems, and data science. He has worked in a variety of industries, including CRM systems, retail loyalty, IoT systems, and accountancy. He holds a master's in data science and an MBA. He works in Cork, Ireland, as a senior data scientist with AltViz.
Read more about Mark Hodnett

Joshua F. Wiley
Joshua F. Wiley
author image
Joshua F. Wiley

Joshua F. Wiley is a lecturer at Monash University, conducting quantitative research on sleep, stress, and health. He earned his Ph.D. from the University of California, Los Angeles and completed postdoctoral training in primary care and prevention. In statistics and data science, Joshua focuses on biostatistics and is interested in reproducible research and graphical displays of data and statistical models. He develops or co-develops a number of R packages including Varian, a package to conduct Bayesian scale-location structural equation models, and MplusAutomation, a popular package that links R to the commercial Mplus software.
Read more about Joshua F. Wiley

View More author details
Right arrow

Deep Learning Models Using TensorFlow in R

This chapter is about using TensorFlow in R. We have already used TensorFlow quite a lot, as Keras is a high-level neural network API that uses either TensorFlow, CNTK, or Theano. In R, Keras uses TensorFlow in the background. TensorFlow is more difficult to develop deep learning models in. However, there are two interesting packages in TensorFlow that could be overlooked: TensorFlow estimators and TensorFlow runs. We will cover both of these packages in this chapter.

In this chapter, we will cover the following topics:

  • Introduction to TensorFlow
  • Building models using TensorFlow
  • TensorFlow estimators
  • TensorFlow runs packages

Introduction to the TensorFlow library

TensorFlow is not just a deep learning library, but an expressive programming language that can implement various optimization and mathematical transformations on data. While it is mainly used to implement deep learning algorithms, it can perform much more. In TensorFlow, programs are represented as computational graphs, and data in TensorFlow is stored in tensors. A tensor is an array of data that has the same data type, and the rank of a tensor is the number of dimensions. Because all the data in a tensor must have the same type, they are more similar to R matrices than data frames.

Here is an example of tensors of various ranks:

library(tensorflow)

> # tensor of rank-0
> var1 <- tf$constant(0.1)
> print(var1)
Tensor("Const:0", shape=(), dtype=float32)

> sess <- tf$InteractiveSession()
T:\src\github\tensorflow...

TensorFlow models

In this section, we will use TensorFlow to build some machine learning models. First, we will build a simple linear regression model and then a convolutional neural network model, similar to what we have seen in Chapter 5, Image Classification Using Convolutional Neural Networks.

The following code loads the TensorFlow library. We can confirm it loaded successfully by setting and accessing a constant string value:

> library(tensorflow)

# confirm that TensorFlow library has loaded
> sess=tf$Session()
> hello_world <- tf$constant('Hello world from TensorFlow')
> sess$run(hello_world)
b'Hello world from TensorFlow'

Linear regression using TensorFlow

In this first Tensorflow example...

TensorFlow estimators and TensorFlow runs packages

TensorFlow estimators and the TensorFlow runs packages are great packages to use for deep learning. In this section, we will use both to train a model based on our churn prediction data from Chapter 4, Training Deep Prediction Models.

TensorFlow estimators

TensorFlow estimators allow you to build TensorFlow models using a simpler API interface. In R, the tfestimators package allows you to call this API. There are different model types, including linear models and neural networks. The following estimators are available:

  • linear_regressor() for linear regression
  • linear_classifier() for linear classification
  • dnn_regressor() for deep neural network regression
  • dnn_classifier()...

Summary

In this chapter, we developed some TensorFlow models. We looked at TensorBoard, which is a great tool for visualizing and debugging deep learning models. We built a couple of models using TensorFlow, including a basic regression model and a Lenet model for computer vision models. From these examples, we saw that programming in TensorFlow was more complicated and error-prone than using the higher-level APIs (MXNet and Keras) that we used elsewhere in this book.

We then moved onto using TensorFlow estimators, which is a much easier interface than using TensorFlow. We then used that script in another package called tfruns, which stands for TensorFlow runs. This package allows us to call a TensorFlow estimators or Keras script with different flags each time. We used this for hyper-parameter selection, running, and evaluating multiple models. The TensorFlow runs have excellent...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
R Deep Learning Essentials. - Second Edition
Published in: Aug 2018Publisher: PacktISBN-13: 9781788992893
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Mark Hodnett

Mark Hodnett is a data scientist with over 20 years of industry experience in software development, business intelligence systems, and data science. He has worked in a variety of industries, including CRM systems, retail loyalty, IoT systems, and accountancy. He holds a master's in data science and an MBA. He works in Cork, Ireland, as a senior data scientist with AltViz.
Read more about Mark Hodnett

author image
Joshua F. Wiley

Joshua F. Wiley is a lecturer at Monash University, conducting quantitative research on sleep, stress, and health. He earned his Ph.D. from the University of California, Los Angeles and completed postdoctoral training in primary care and prevention. In statistics and data science, Joshua focuses on biostatistics and is interested in reproducible research and graphical displays of data and statistical models. He develops or co-develops a number of R packages including Varian, a package to conduct Bayesian scale-location structural equation models, and MplusAutomation, a popular package that links R to the commercial Mplus software.
Read more about Joshua F. Wiley