Posit AI Blog: TensorFlow and Keras 2.9

Edition Deep Learning with R, 2nd Edition coincides with new versions of TensorFlow and Keras. These releases bring many improvements that allow for more idiomatic and concise R code.

First, the set of Tensor methods for R base generics has expanded considerably. The set of R generics that work with TensorFlow Tensors is now quite extensive:

methods(class = "tensorflow.tensor")
 (1) -           !           !=          (           (<-        
 (6) *           /           &           %/%         %%         
(11) ^           +           <           <=          ==         
(16) >           >=          |           abs         acos       
(21) all         any         aperm       Arg         asin       
(26) atan        cbind       ceiling     Conj        cos        
(31) cospi       digamma     dim         exp         expm1      
(36) floor       Im          is.finite   is.infinite is.nan     
(41) length      lgamma      log         log10       log1p      
(46) log2        max         mean        min         Mod        
(51) print       prod        range       rbind       Re         
(56) rep         round       sign        sin         sinpi      
(61) sort        sqrt        str         sum         t          
(66) tan         tanpi      

This means that you can often write the same code for TensorFlow Tensors as you would for R arrays. Take, for example, this little function from Chapter 11 of the book:

reweight_distribution <-
  function(original_distribution, temperature = 0.5) {
    original_distribution %>%
      { exp(log(.) / temperature) } %>%
      { . / sum(.) }
  }

Note that functions like reweight_distribution() work with both 1D R vectors and 1D TensorFlow tensors, from exp(), log(), /and
sum() are all R generics with methods for TensorFlow Tensors.

In the same vein, this release of Keras brings improvements to the way custom class extensions are defined for Keras. Partly inspired by the novelty R7 syntax, there is a new family of functions: new_layer_class(), new_model_class(),
new_metric_class(), and so on. This new interface greatly simplifies the amount of boilerplate code needed to define custom Keras extensions—a nice R interface that serves as a facade over the mechanics of subclassing Python classes. This new interface is the yang to the yin %py_class%– a way to mimic the Python class definition syntax in R. Of course, the “raw” conversion API
R6Class() to Python via r_to_py() is still available for users who require full control.

This release also brings with it an abundance of small improvements throughout the Keras R interface: updated print() and plot() methods for models, improvements freeze_weights() and load_model_tf()new exported utilities like zip_lists() and %<>%. And let’s not forget to mention a new family of R functions for adjusting the learning rate during training, with a set of built-in plans like
learning_rate_schedule_cosine_decay()supplemented with an interface for creating custom schedules with new_learning_rate_schedule_class().

Full release notes for R packages can be found here:

However, the release notes for the R packages only tell half the story. The R to Keras and TensorFlow interfaces work by injecting the entire Python process into R (via the
reticulate package). One of the main advantages of this design is that R users have full access to everything in both R and Python. In other words, the R interface always has feature parity with the Python interface—anything you can do with TensorFlow in Python, you can do in R just as easily. This means that the release notes for the Python release of TensorFlow are equally relevant for R users:

Thank you for reading!

Photo by Raphael Wilde on Unsplash

Use again

Text and images are licensed under Creative Commons Attribution CC BY 4.0. Figures that have been reused from other sources are not covered by this license and can be identified by the note in their caption: “Image from …”.

Quote

For attribution, please cite this work as

Kalinowski (2022, June 9). Posit AI Blog: TensorFlow and Keras 2.9. Retrieved from https://blogs.rstudio.com/tensorflow/posts/2022-06-09-tf-2-9/

BibTeX citation

@misc{kalinowskitf29,
  author = {Kalinowski, Tomasz},
  title = {Posit AI Blog: TensorFlow and Keras 2.9},
  url = {https://blogs.rstudio.com/tensorflow/posts/2022-06-09-tf-2-9/},
  year = {2022}
}

Leave a Comment