Introducing Keras 3 for R

TensorFlow/Keras R

We are thrilled to introduce {keras3}, the next version of the Keras R package. {keras3} is a ground-up rebuild of {keras}, maintaining the beloved features of the original while refining and simplifying the API based on valuable insights gathered over the past few years.

Tomasz Kalinowski (Posit)https://www.posit.co/
2024-05-21

We are thrilled to introduce keras3, the next version of the Keras R package. keras3 is a ground-up rebuild of {keras}, maintaining the beloved features of the original while refining and simplifying the API based on valuable insights gathered over the past few years.

Keras provides a complete toolkit for building deep learning models in R—it’s never been easier to build, train, evaluate, and deploy deep learning models.

Installation

To install Keras 3:

install.packages("keras3")
library(keras3)
install_keras()

What’s new:

Documentation

Great documentation is essential, and we’ve worked hard to make sure that keras3 has excellent documentation, both now, and in the future.

Keras 3 comes with a full refresh of the website: https://keras.posit.co. There, you will find guides, tutorials, reference pages with rendered examples, and a new examples gallery. All the reference pages and guides are also available via R’s built-in help system.

In a fast moving ecosystem like deep learning, creating great documentation and wrappers once is not enough. There also need to be workflows that ensure the documentation is up-to-date with upstream dependencies. To accomplish this, {keras3} includes two new maintainer features that ensure the R documentation and function wrappers will stay up-to-date:

Combined, these two features will make it substantially easier for Keras in R to maintain feature parity and up-to-date documentation with the Python API to Keras.

Multi-backend support

Soon after its launch in 2015, Keras featured support for most popular deep learning frameworks: TensorFlow, Theano, MXNet, and CNTK. Over time, the landscape shifted; Theano, MXNet, and CNTK were retired, and TensorFlow surged in popularity. In 2021, three years ago, TensorFlow became the premier and only supported Keras backend. Now, the landscape has shifted again.

Keras 3 brings the return of multi-backend support. Choose a backend by calling:

use_backend("jax") # or "tensorflow", "torch", "numpy"

The default backend continues to be TensorFlow, which is the best choice for most users today; for small-to-medium sized models this is still the fastest backend. However, each backend has different strengths, and being able to switch easily will let you adapt to changes as your project, or the frameworks themselves, evolve.

Today, switching to the Jax backend can, for some model types, bring substantial speed improvements. Jax is also the only backend that has support for a new model parallelism distributed training API. Switching to Torch can be helpful during development, often producing simpler trackbacks while debugging.

Keras 3 also lets you incorporate any pre-existing Torch, Jax, or Flax module as a standard Keras layer by using the appropriate wrapper, letting you build atop existing projects with Keras. For example, train a Torch model using the Keras high-level training API (compile() + fit()), or include a Flax module as a component of a larger Keras model. The new multi-backend support lets you use Keras à la carte.

The ‘Ops’ family

{keras3} introduces a new “Operations” family of function. The Ops family, currently with over 200 functions, provides a comprehensive suite of operations typically needed when operating on nd-arrays for deep learning. The Operation family supersedes and greatly expands on the former family of backend functions prefixed with k_ in the {keras} package.

The Ops functions let you write backend-agnostic code. They provide a uniform API, regardless of if you’re working with TensorFlow Tensors, Jax Arrays, Torch Tensors, Keras Symbolic Tensors, NumPy arrays, or R arrays.

The Ops functions:

The Ops API includes:

Ingest tabular data with layer_feature_space()

keras3 provides a new set of functions for building models that ingest tabular data: layer_feature_space() and a family of feature transformer functions (prefix, feature_) for building keras models that can work with tabular data, either as inputs to a keras model, or as preprocessing steps in a data loading pipeline (e.g., a tfdatasets::dataset_map()).

See the reference page and an example usage in a full end-to-end example to learn more.

New Subclassing API

The subclassing API has been refined and extended to more Keras types. Define subclasses simply by calling: Layer(), Loss(), Metric(), Callback(), Constraint(), Model(), and LearningRateSchedule(). Defining {R6} proxy classes is no longer necessary.

Additionally the documentation page for each of the subclassing functions now contains a comprehensive listing of all the available attributes and methods for that type. Check out ?Layer to see what’s possible.

Saving and Export

Keras 3 brings a new model serialization and export API. It is now much simpler to save and restore models, and also, to export them for serving.

New random family

A new family of random tensor generators. Like the Ops family, these work with all backends. Additionally, all the RNG-using methods have support for stateless usage when you pass in a seed generator. This enables tracing and compilation by frameworks that have special support for stateless, pure, functions, like Jax. See ?random_seed_generator() for example usage.

Other additions:

Migrating from {keras} to {keras3}

{keras3} supersedes the {keras} package.

If you’re writing new code today, you can start using {keras3} right away.

If you have legacy code that uses {keras}, you are encouraged to update the code for {keras3}. For many high-level API functions, such as layer_dense(), fit(), and keras_model(), minimal to no changes are required. However there is a long tail of small changes that you might need to make when updating code that made use of the lower-level Keras API. Some of those are documented here: https://keras.io/guides/migrating_to_keras_3/.

If you’re running into issues or have questions about updating, don’t hesitate to ask on https://github.com/rstudio/keras/issues or https://github.com/rstudio/keras/discussions.

The {keras} and {keras3} packages will coexist while the community transitions. During the transition, {keras} will continue to receive patch updates for compatibility with Keras v2, which continues to be published to PyPi under the package name tf-keras. After tf-keras is no longer maintained, the {keras} package will be archived.

Summary

In summary, {keras3} is a robust update to the Keras R package, incorporating new features while preserving the ease of use and functionality of the original. The new multi-backend support, comprehensive suite of Ops functions, refined model serialization API, and updated documentation workflows enable users to easily take advantage of the latest developments in the deep learning community.

Whether you are a seasoned Keras user or just starting your deep learning journey, Keras 3 provides the tools and flexibility to build, train, and deploy models with ease and confidence. As we transition from Keras 2 to Keras 3, we are committed to supporting the community and ensuring a smooth migration. We invite you to explore the new features, check out the updated documentation, and join the conversation on our GitHub discussions page. Welcome to the next chapter of deep learning in R with Keras 3!

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

Kalinowski (2024, May 21). Posit AI Blog: Introducing Keras 3 for R. Retrieved from https://blogs.rstudio.com/tensorflow/posts/2024-05-21-keras3/

BibTeX citation

@misc{kalinowskikeras3,
  author = {Kalinowski, Tomasz},
  title = {Posit AI Blog: Introducing Keras 3 for R},
  url = {https://blogs.rstudio.com/tensorflow/posts/2024-05-21-keras3/},
  year = {2024}
}