TensorFlow With R
TensorFlow With R
2018-03-27
Andrie de Vries
Solutions Engineer, RStudio
@RevoAndrie
1
Overview
• TensorFlow using R
• Worked example of keras in R StackOverflow: andrie
Twitter: @RevoAndrie
GitHub: andrie
• Demo
• Supporting tools
• Learning more
Slides at https://speakerdeck.com/andrie/londonr-tensorflow
2
What is TensorFlow
3
What is TensorFlow
4
What is a tensor?
1 Vector Weights
5 Array Video
6
Why a dataflow graph?
7
Uses of TensorFlow
• Image classification
• Time series forecasting
• Classifying peptides for cancer immunotherapy
• Credit card fraud detection using an autoencoder
• Classifying duplicate questions from Quora
• Predicting customer churn
• Learning word embeddings for Amazon reviews
https://tensorflow.rstudio.com/gallery/
8
What is deep learning
9
What is deep learning?
10
What are layers?
11
MNIST layers in R
library(keras)
model <- keras_model_sequential() %>%
layer_conv_2d(filters = 32, kernel_size = c(3,3), activation = 'relu',
input_shape = c(28,28,1)) %>%
layer_conv_2d(filters = 64, kernel_size = c(3,3), activation = 'relu') %>%
layer_max_pooling_2d(pool_size = c(2, 2)) %>%
layer_flatten() %>%
layer_dense(units = 128, activation = 'relu') %>%
layer_dense(units = 10, activation = 'softmax')
12
MNIST layers of representation
13
Geometric interpretation
14
How can we do this?
• How can we do this with simple parametric models trained with gradient
descent?
• We just need
• Sufficiently large parametric models,
• trained with gradient descent on
• sufficiently many examples
15
Sufficiently large parametric models
17
Why should R users care about TensorFlow?
• https://tensorflow.rstudio.com
19
Graph is generated automatically from R
20
TensorFlow APIs
21
tensorflow
sess = tf$Session()
sess$run(tf$global_variables_initializer())
library(tfestimators)
linear_regressor()
linear_classifier()
dnn_regressor()
dnn_classifier()
dnn_linear_combined_regressor()
dnn_linear_combined_classifier()
23
keras
library(keras)
24
Worked example using keras
25
Steps in building a keras model
library(keras)
TensorFlow expects row-
# Load MNIST images datasets (built-in to Keras) primary tensors. Use
c(c(x_train, y_train), c(x_test, y_test)) %<-% dataset_mnist() array_reshape() to convert
from (column-primary) R
arrays
# Flatten images and transform RGB values into [0,1] range
x_train <- array_reshape(x_train, c(nrow(x_train), 784))
x_test <- array_reshape(x_test, c(nrow(x_test), 784))
Normalize to [-1; 1] range for
x_train <- x_train / 255
best results
x_test <- x_test / 255
28
Note: Models are modified in-place
• Keras models are directed acyclic graphs of layers whose state is updated
during training.
• Keras layers can be shared by multiple parts of a Keras model.
# Modify model object in place (note that it is not assigned back to)
29
Keras: Model training
$loss
[1] 0.1078904
$acc
[1] 0.9815
[1] 7 2 1 0 4 1 4 9 5 9 0 6 9 0 1 5 9 7 3 4 9 6 6 5 4 0 7 4 0 1 3 1 3 4 7
[36] 2 7 1 2 1 1 7 4 2 3 5 1 2 4 4 6 3 5 5 6 0 4 1 9 5 7 8 9 3 7 4 6 4 3 0
[71] 7 0 2 9 1 7 3 2 9 7 7 6 2 7 8 4 7 3 6 1 3 6 9 3 1 4 1 7 6 9
31
Easy plotting of fitting history
plot(history)
32
Demo
33
34
Supporting tools
35
tfruns
• https://tensorflow.rstudio.com/tools/tfruns/
• Successful deep learning requires a huge
amount of experimentation.
• This requires a systematic approach to
conducting and tracking the results of
experiments.
• The training_run() function is like the
source() function, but it automatically tracks
and records output and metadata for the
execution of the script:
library(tfruns)
training_run("mnist_mlp.R")
36
cloudml
• https://tensorflow.rstudio.com/tools/cloudml/
• Scalable training of models built with the keras,
tfestimators, and tensorflow R packages.
• On-demand access to training on GPUs,
including Tesla P100 GPUs from NVIDIA®.
• Hyperparameter tuning to optimize key
attributes of model architectures in order to
maximize predictive accuracy.
37
tfdeploy
• https://tensorflow.rstudio.com/tools/tfdeploy/
• TensorFlow was built from the ground up to enable
deployment using a low-latency C++ runtime.
• Deploying TensorFlow models requires no runtime R or
Python code.
• Key enabler for this is the TensorFlow SavedModel format:
• a language-neutral format
• enables higher-level tools to produce, consume and
transform models.
• TensorFlow models can be deployed to servers, embedded
devices, mobile phones, and even to a web browser!
38
Resources
39
Recommended reading
Chollet and Allaire Goodfellow, Bengio & Courville
40
R examples in the gallery
• https://tensorflow.rstudio.com/gallery/
41
Keras for R cheat sheet
https://github.com/rstudio/cheatsheets/raw/master/keras.pdf
42
rstudio::conf videos
43
Summary
44
Summary
45
Summary
• Deep learning has made great progress and will likely increase
in importance in various fields in the coming years.
• R now has a great set of APIs and supporting tools for using
TensorFlow and doing deep learning.
Slides at https://speakerdeck.com/andrie/londonr-tensorflow
46