Friday, December 8, 2023

So, how come we will use TensorFlow from R?

Which pc language is most intently related to TensorFlow? Whereas on the TensorFlow for R weblog, we might after all like the reply to be R, chances are high it’s Python (although TensorFlow has official bindings for C++, Swift, Javascript, Java, and Go as nicely).

So why is it you possibly can outline a Keras mannequin as

mannequin <- keras_model_sequential() %>%
  layer_dense(models = 32, activation = "relu") %>%
  layer_dense(models = 1)

(good with %>%s and all!) – then prepare and consider it, get predictions and plot them, all that with out ever leaving R?

The brief reply is, you’ve got keras, tensorflow and reticulate put in.
reticulate embeds a Python session inside the R course of. A single course of means a single tackle area: The identical objects exist, and may be operated upon, no matter whether or not they’re seen by R or by Python. On that foundation, tensorflow and keras then wrap the respective Python libraries and allow you to write R code that, in actual fact, seems like R.

This publish first elaborates a bit on the brief reply. We then go deeper into what occurs within the background.

One notice on terminology earlier than we bounce in: On the R aspect, we’re making a transparent distinction between the packages keras and tensorflow. For Python we’re going to use TensorFlow and Keras interchangeably. Traditionally, these have been totally different, and TensorFlow was generally considered one attainable backend to run Keras on, moreover the pioneering, now discontinued Theano, and CNTK. Standalone Keras does nonetheless exist, however latest work has been, and is being, accomplished in tf.keras. After all, this makes Python Keras a subset of Python TensorFlow, however all examples on this publish will use that subset so we will use each to seek advice from the identical factor.

So keras, tensorflow, reticulate, what are they for?

Firstly, nothing of this might be attainable with out reticulate. reticulate is an R bundle designed to permit seemless interoperability between R and Python. If we completely wished, we might assemble a Keras mannequin like this:

<class 'tensorflow.python.keras.engine.sequential.Sequential'>

We might go on including layers …

m$add(tf$keras$layers$Dense(32, "relu"))


However who would wish to? If this had been the one means, it’d be much less cumbersome to immediately write Python as an alternative. Plus, as a person you’d must know the whole Python-side module construction (now the place do optimizers reside, at present: tf.keras.optimizers, tf.optimizers …?), and sustain with all path and identify adjustments within the Python API.

That is the place keras comes into play. keras is the place the TensorFlow-specific usability, re-usability, and comfort options reside.
Performance supplied by keras spans the entire vary between boilerplate-avoidance over enabling elegant, R-like idioms to offering technique of superior function utilization. For example for the primary two, contemplate layer_dense which, amongst others, converts its models argument to an integer, and takes arguments in an order that enable it to be “pipe-added” to a mannequin: As a substitute of

mannequin <- keras_model_sequential()
mannequin$add(layer_dense(models = 32L))

we will simply say

mannequin <- keras_model_sequential()
mannequin %>% layer_dense(models = 32)

Whereas these are good to have, there may be extra. Superior performance in (Python) Keras principally depends upon the power to subclass objects. One instance is customized callbacks. When you had been utilizing Python, you’d must subclass tf.keras.callbacks.Callback. From R, you possibly can create an R6 class inheriting from KerasCallback, like so

CustomCallback <- R6::R6Class("CustomCallback",
    inherit = KerasCallback,
    public = checklist(
      on_train_begin = operate(logs) {
        # do one thing
      on_train_end = operate(logs) {
        # do one thing

It is because keras defines an precise Python class, RCallback, and maps your R6 class’ strategies to it.
One other instance is customized fashions, launched on this weblog a couple of 12 months in the past.
These fashions may be educated with customized coaching loops. In R, you utilize keras_model_custom to create one, for instance, like this:

m <- keras_model_custom(identify = "mymodel", operate(self) {
  self$dense1 <- layer_dense(models = 32, activation = "relu")
  self$dense2 <- layer_dense(models = 10, activation = "softmax")
  operate(inputs, masks = NULL) {
    self$dense1(inputs) %>%

Right here, keras will be certain that an precise Python object is created which subclasses tf.keras.Mannequin and when known as, runs the above nameless operate().

In order that’s keras. What concerning the tensorflow bundle? As a person you solely want it when you must do superior stuff, like configure TensorFlow gadget utilization or (in TF 1.x) entry parts of the Graph or the Session. Internally, it’s utilized by keras closely. Important inside performance contains, e.g., implementations of S3 strategies, like print, [ or +, on Tensors, so you can operate on them like on R vectors.

Now that we know what each of the packages is “for”, let’s dig deeper into what makes this possible.

Show me the magic: reticulate

Instead of exposing the topic top-down, we follow a by-example approach, building up complexity as we go. We’ll have three scenarios.

First, we assume we already have a Python object (that has been constructed in whatever way) and need to convert that to R. Then, we’ll investigate how we can create a Python object, calling its constructor. Finally, we go the other way round: We ask how we can pass an R function to Python for later usage.

Scenario 1: R-to-Python conversion

Let’s assume we have created a Python object in the global namespace, like this:

So: There is a variable, called x, with value 1, living in Python world. Now how do we bring this thing into R?

We know the main entry point to conversion is py_to_r, defined as a generic in conversion.R:

py_to_r <- function(x) {

… with the default implementation calling a function named py_ref_to_r:

Rcpp : You simply write your C++ operate, and Rcpp takes care of compilation and offers the glue code essential to name this operate from R.

So py_ref_to_r actually is written in C++:

.Name(`_reticulate_py_ref_to_r`, x)

which lastly wraps the “actual” factor, the C++ operate py_ref_to_R we noticed above.

By way of py_ref_to_r_with_convert in #1, a one-liner that extracts an object’s “convert” function (see under)

Extending Python Information.

In official phrases, what reticulate does it embed and prolong Python.
Embed, as a result of it permits you to use Python from inside R. Prolong, as a result of to allow Python to name again into R it must wrap R features in C, so Python can perceive them.

As a part of the previous, the specified Python is loaded (Py_Initialize()); as a part of the latter, two features are outlined in a brand new module named rpycall, that shall be loaded when Python itself is loaded.

International Interpreter Lock, this isn’t routinely the case when different implementations are used, or C is used immediately. So call_python_function_on_main_thread makes certain that except we will execute on the principle thread, we wait.

That’s it for our three “spotlights on reticulate”.


It goes with out saying that there’s quite a bit about reticulate we didn’t cowl on this article, equivalent to reminiscence administration, initialization, or specifics of information conversion. Nonetheless, we hope we had been in a position to shed a bit of sunshine on the magic concerned in calling TensorFlow from R.

R is a concise and chic language, however to a excessive diploma its energy comes from its packages, together with people who permit you to name into, and work together with, the skin world, equivalent to deep studying frameworks or distributed processing engines. On this publish, it was a particular pleasure to concentrate on a central constructing block that makes a lot of this attainable: reticulate.

Thanks for studying!

Related Articles


Please enter your comment!
Please enter your name here

Latest Articles

Canada’s oil and fuel emissions cap is important and honest

OTTAWA — Mark Zacharias, govt director at Clear Power Canada, made the next assertion in response to the oil and fuel emissions cap framework launched...

Thursday Night time Soccer: The best way to Watch, Stream Patriots vs. Steelers Tonight on Prime Video or Twitch

Sling TV Blue Carries Fox and NBC for $45 a month in choose markets ...

BlackSuit ransomware – what that you must know

What is going on on? A cybercriminal group calling itself BlackSuit has claimed accountability for a collection of ransomware assaults, together with breaches at...

Finest Web Suppliers in Fayetteville, North Carolina

What's the greatest web supplier in Fayetteville?Metronet is the perfect web service supplier in Fayetteville. It is the world's largest fiber web supplier...