R interface for Tensorflow
#sdss2019
Kevin Kuo @kevinykuo
Artificial
intelligence?
Artificial
Intelligence?
Getty Images
Getty Images
Artificial
Intelligence?
Rule #1 of AI:
Rule #1 of AI:
Someone talk to you about AI
with a straight face? They
tryna hustle ya.
ELI5
TensorFlow?
ELI5 a stats
undergrad
TensorFlow
What’s a tensor?
What’s a tensor?
What’s flowing?
Tensors & Ops
Tensors are just
multidimensional
arrays
Tensors & Ops
Tensors are just
multidimensional
arrays
You apply Ops
(transformations
) to Tensors to
get more Tensors
Deep Learning?
Neural networks?
Find a function F
such that Y ≈ f(x)
Find a function F
such that Y ≈ f(x)
(at least some of the
time)
Find a function F
such that Y ≈ f(x)
(at least some of the
time, hopefully)
Really just
straightforward
matrix algebra
Resources
tensorflow
https://www.tensorflow.org/guide/extend/ar
chitecture
tensorflow
tf.keras
tensorflow
tf.keras
library(keras)
library(tfdatasets)
library(tfprobability)
library(tensorflow)
GLM is a neural net!
We’re doing AI!
Proof (by example).
library(keras)
library(tfprobability)
model <- keras_model_sequential() %>%
layer_dense(units = 1, input_shape = 1,
name = "parameters") %>%
layer_lambda(f = k_exp) %>%
layer_distribution_lambda(tfd_poisson)
library(keras)
library(tfprobability)
model <- keras_model_sequential() %>%
layer_dense(units = 1, input_shape = 1,
name = "parameters") %>%
layer_lambda(f = k_exp) %>%
layer_distribution_lambda(tfd_poisson)
neg_loglik <- function(y_true, y_pred) {
- tfd_log_prob(y_pred, y_true)
}
model %>%
compile(optimizer = optimizer_sgd(lr = 0.1),
loss = neg_loglik)
model <- keras_model_sequential() %>%
layer_dense(units = 1, input_shape = 1,
name = "parameters") %>%
layer_lambda(f = k_exp) %>%
layer_distribution_lambda(tfd_poisson)
neg_loglik <- function(y_true, y_pred) {
- tfd_log_prob(y_pred, y_true)
}
model %>%
compile(optimizer = optimizer_sgd(lr = 0.1),
loss = neg_loglik)
model %>%
fit(x, y, epochs = 15)
> parameters <- model %>%
+ get_layer("parameters") %>%
+ (function(x) x$get_weights())
> parameters
[[1]]
[,1]
[1,] 1.985788
[[2]]
[1] 1.090006
> glm(y ~ x, family = poisson())
Call: glm(formula = y ~ x, family = poisson())
Coefficients:
(Intercept) x
1.090 1.986
Degrees of Freedom: 99 Total (i.e. Null); 98 Residual
Null Deviance: 359
Residual Deviance: 90.1 AIC: 484.2
> parameters <- model %>%
+ get_layer("parameters") %>%
+ (function(x) x$get_weights())
> parameters
[[1]]
[,1]
[1,] 1.985788
[[2]]
[1] 1.090006
model <- keras_model_sequential() %>%
layer_dense(units = 8675309, input_shape = 1,
name = "parameters") %>%
layer_dense(units = 8675309) %>%
layer_lambda(f = k_exp) %>%
layer_distribution_lambda(tfd_poisson)
NOW YOU HAVE A DEEP NEURAL NET
model <- keras_model_sequential() %>%
layer_dense(units = 8675309, input_shape = 1,
name = "parameters") %>%
layer_dense_variational(
units = 1,
make_posterior_fn = posterior_mean_field,
make_prior_fn = prior_trainable,
kl_weight = 1 / n_rows,
activation = "linear"
) %>%
layer_lambda(f = k_exp) %>%
layer_distribution_lambda(tfd_poisson)
NOW YOUR MODEL IS A RANDOM
VARIABLE ZOMG
model <- keras_model_sequential() %>%
layer_dense(units = 8675309, input_shape = 1,
name = "parameters") %>%
layer_dense_variational(
units = 2,
make_posterior_fn = posterior_mean_field,
make_prior_fn = prior_trainable,
kl_weight = 1 / n_rows,
activation = "linear"
) %>%
layer_distribution_lambda(function(t) {
tfd_normal(t[,1], k_softplus(t[,2]))
})
HETEROSCEDASTIC ERRORS OMGWTFBBQ
model <- keras_model_sequential() %>%
____ %>%
____ %>%
____
model %>%
compile(optimizer = ____,
loss = ____)
THE POSSIBILITIES ARE ENDLESS
model <- keras_model_sequential() %>%
____ %>%
____ %>%
____
model %>%
compile(optimizer = ____,
loss = ____)
THE POSSIBILITIES ARE ENDLESS
Why should I give a
___ about deep
learning?
Enabling new
applications?
1.123%
Percentage of data scientists who
work with image data on the job
Things neural nets can help with
- Images (yeah, sometimes we get them)
- Natural language / Time series
- High dimensional categorical predictors
- Multi-task learning, i.e. when we want to predict
multiple outputs with a single model
- Combining different types of inputs into the same model
- Making predictions on devices without docker containers
(e.g. your phone)
Resources
● https://tensorflow.rstudio.com/
● https://keras.rstudio.com/
● https://blogs.rstudio.com/tensorflow/
● https://github.com/rstudio/tfprobability

R Interface for TensorFlow