Release Deep Learning with R, 2nd edition It coincides with new releases of TensorFlow and Keras. This release includes many improvements that allow for more idiomatic and concise R code.
First, the set of Tensor methods for basic R generics has been significantly expanded. The set of R generics that work with TensorFlow Tensors is now very 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. For example, consider the following small function from Chapter 11 of this book:
reweight_distribution <-
function(original_distribution, temperature = 0.5) {
original_distribution %>%
{ exp(log(.) / temperature) } %>%
{ . / sum(.) }
}
Please note the following features: reweight_distribution()
You can use both 1D R vectors and 1D TensorFlow Tensors. exp()
, log()
, /
and
sum()
An R generic containing methods for TensorFlow Tensors.
Along the same lines, this release of Keras has improved the way custom class extensions to Keras are defined. Inspired in part by something new R7
The syntax has a new family of functions: new_layer_class()
, new_model_class()
,
new_metric_class()
, etc. This new interface greatly simplifies the amount of boilerplate code required to define custom Keras extensions. This is a nice R interface that acts as a façade for the Python class subclass mechanism. This new interface acts as both a positive and a negative. %py_class%
–A way to mimic Python class definition syntax in R. of course,
R6Class()
Via Python r_to_py()
Users who require full access will still be able to use it.
This release includes minor improvements throughout the Keras R interface. updated print()
and plot()
Model methods, improvements freeze_weights()
and load_model_tf()
New export utilities including: zip_lists()
and %<>%
. And don't forget to mention the new family of R functions for modifying the learning rate during training using a collection of built-in schedules:
learning_rate_schedule_cosine_decay()
Complemented with an interface for creating custom schedules new_learning_rate_schedule_class()
.
Full release notes for R packages can be found here.
However, the release notes for an R package only tell half the story. The R interface to Keras and TensorFlow works by embedding the entire Python process in R (
reticulate
package). One of the main benefits of this design is that R users have full access to everything in both R. and Python. That is, R interfaces always have feature parity with Python interfaces. Everything you can do with TensorFlow in Python, you can easily do in R. This means that the release notes for the Python release of TensorFlow are also relevant for R users.
Thanks for reading!
Photo by Raphael Wild on Unsplash
recycle
Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Illustrations reused from other sources do not fall under this license and can be recognized by the note “Illustration of…” in the caption.
Summons
To give attribution, please cite this work as follows:
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 Quotes
@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} }