We are pleased to introduce keras3
The next version of the Keras R package. keras3
It's a basic rebuild. {keras}
Based on valuable insights gathered over the past few years, we're improving and simplifying our API while retaining the beloved features of the original.
Keras provides a complete toolkit for building deep learning models in R. Building, training, evaluating, and deploying deep learning models has never been easier.
installation
To install Keras 3:
install.packages("keras3")
library(keras3)
install_keras()
What's new:
documentation
Good documentation is essential and we have worked hard to ensure this. keras3
You have great documentation now and in the future.
Keras 3 has a completely new website (https://keras.posit.co). Here you'll find guides, tutorials, a reference page with rendered examples, and a new example gallery. All reference pages and guides are also available through R's built-in help system.
In a fast-moving ecosystem like deep learning, creating great documentation and wrappers once is not enough. You also need a workflow to ensure that your documents are up-to-date with their upstream dependencies. To achieve this, {keras3} includes two new maintenance features that ensure that R documentation and function wrappers remain up-to-date.
-
Now take a snapshot of the upstream documentation and API surface. For each release, all R documentation is based on upstream updates. This workflow ensures that all R documentation (guides, examples, illustrations, and reference pages) and R function signatures are kept up-to-date upstream. This snapshot and rebase functionality is implemented in a new standalone R package, {doctether}. This can be useful for R package maintainers who need to maintain documentation with dependencies and parity.
-
All examples and vignettes can now be evaluated and rendered during package build. This ensures that outdated or corrupted example code is not released. This also means that all the example code you encounter is now additionally available as an expanded suite of snapshot unit and integration tests.
It is still not allowed to evaluate the code in vignettes and examples under CRAN restrictions. Addresses CRAN limitations by adding an additional package build step to pre-render examples and vignettes.
Combining these two features makes it much easier for Keras in R to maintain feature parity and up-to-date documentation with the Python API to Keras.
Multi-backend support
Keras supported the most popular deep learning frameworks: TensorFlow, Theano, MXNet, and CNTK right after its launch in 2015. Over time, the landscape has changed. Theano, MXNet, and CNTK were retired, and TensorFlow surged in popularity. Three years ago, in 2021, TensorFlow became premier and only supported the Keras backend. Now the landscape has changed again.
Keras 3 brings back multi-backend support. Select 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 and medium-sized models, this is still the fastest backend. However, each backend has different strengths, and being able to switch easily allows you to adapt to changes as the project or framework itself evolves.
For some model types today, switching to a Jax backend can provide significant speed improvements. Jax is also the only backend that supports the new model-parallel distributed training API. Switching to Torch can be helpful during development and often produces simpler trackbacks while debugging.
Keras 3 also allows you to integrate existing Torch, Jax, or Flax modules into the standard Keras layer using appropriate wrappers, allowing you to build on top of existing projects using Keras. For example, the Keras advanced learning API (compile()
+
fit()
) or include the Flax module as a component of a larger Keras model. New multi-backend support allows Keras to be used individually.
'Ops' family
{keras3}
Introducing a new “Jobs” suite of features. Now with over 200 features, the Ops suite provides a comprehensive suite of operations commonly required when operating on nd-arrays for deep learning. The Operation suite replaces and significantly extends the previous backend function suite with the prefix. k_
at {keras}
package.
Ops functions allow you to write backend-agnostic code. It provides a uniform API whether you're working with TensorFlow Tensors, Jax Arrays, Torch Tensors, Keras Symbolic Tensors, NumPy arrays, or R arrays.
Operating functions:
- They all start with a prefix
op_
(yes:op_stack()
) - They are all pure functions (no side effects).
- They all use consistent 1-based indexing and coerce doubles to integers as needed.
- All are safe to use with any backend (tensorflow, jax, torch, numpy).
- Both are safe to use in eager and graph/jit/trace modes.
The Ops API includes:
- All NumPy APIs (
numpy.*
) - TensorFlow NN API (
tf.nn.*
) - Common linear algebraic functions (
scipy.linalg.*
) - Sub-family of image converters
- Comprehensive set of loss functions
- And more!
Collect tabular data using: layer_feature_space()
keras3
It provides a new set of features for building models that collect tabular data. layer_feature_space()
and function converter function family (prefix, feature_
) Build a keras model that can use tabular data as input to the keras model or as a preprocessing step in the data loading pipeline, e.g.
tfdatasets::dataset_map()
).
For more information, see the reference page and usage examples for a complete end-to-end example.
New subclassing API
The subclassing API has been improved and extended to more Keras types. Simply define a subclass by calling: Layer()
, Loss()
, Metric()
,
Callback()
, Constraint()
, Model()
and LearningRateSchedule()
. Justice {R6}
The proxy class is no longer needed.
Additionally, the documentation page for each subclassing function now contains a comprehensive list of all properties and methods available for that type. Please confirm
?Layer
To find out what is possible.
Save and export
Keras 3 provides new model serialization and export APIs. Now it's much simpler to save and restore models and export them for delivery.
-
save_model()
/load_model()
:
New advanced file format (extension:.keras
) Save and restore the entire model.The file format is backend-agnostic. This means you can translate trained models between backends by saving them in one backend and loading them in another. For example, train a model using Jax and then convert it to Tensorflow for export.
-
export_savedmodel()
:
Export the forward passing of your model as a compiled artifact for inference using TF Serving or (soon) Posit Connect. This is the easiest way to deploy Keras models for efficient concurrent inference without R or Python runtime dependencies. -
Low-level entry points:
save_model_weights()
/load_model_weights()
:
Save only the weights as follows:.h5
file.save_model_config()
/load_model_config()
:
Save only the model architecture as a json file.
-
register_keras_serializable()
:
Register a custom object to enable serialization and deserialization. -
serialize_keras_object()
/deserialize_keras_object()
:
Convert Keras objects to simple typed R lists that are safe to convert to JSON or rds. -
See the new serialization and storage illustration for more details and examples.
new random
family
A new family of random tensor generators. Like the Ops suite, it works with any backend. Additionally, all RNG-using methods support using statelessness when passing a seed generator. This enables tracking and compilation with frameworks that specifically support stateless pure functions, such as Jax. see
?random_seed_generator()
Example usage.
Other additions:
-
new
shape()
Functions, a one-stop utility for working with tensor shapes in all situations. -
new and improved
print(model)
andplot(model)
method. See some examples of output in the Functional API guide. -
all new
fit()
Progress bar and live metrics viewer output, including new dark mode support in RStudio IDE. -
new
config
A family of curated sets of functions for getting and setting Keras global configuration. -
All other feature families have been expanded with new members.
Migrate from {keras}
to {keras3}
{keras3}
Ultimately, it is a preview of the future. {keras}
package.
If you're writing new code today, you can get started using: {keras3}
soon.
If you have legacy code that uses {keras}
We recommend updating your code for: {keras3}
. For many advanced API features such as: layer_dense()
, fit()
and keras_model()
, requiring minimal or no changes. However, some changes may be required when updating code that uses the low-level Keras API. Some of them are documented here: https://keras.io/guides/migating_to_keras_3/.
If you have any issues or questions about the update, don't hesitate to contact us at https://github.com/rstudio/keras/issues or https://github.com/rstudio/keras/discussions.
that much {keras}
and {keras3}
Packages will coexist during the community transition. During the transition, {keras}
We will continue to receive patch updates for compatibility with Keras v2, which will continue to be published on PyPi under the package name. tf-keras
.
{keras3}
Used as the conversion package name. In future updates {keras}
The package begins sending package welcome messages announcing the deprecation period. After the notice period {keras}
package, {keras3}
Next, the name changes. {keras}
. at that time {keras}
(v2) is no longer supported. {keras3}
It will be an alias for .
{keras}
.
summary
to sum up, {keras3}
A powerful update to the Keras R package, incorporating new features while retaining the usability and functionality of the original. New multi-backend support, a comprehensive Ops feature suite, improved model serialization API, and updated documentation workflow make it easy for users to take advantage of the latest developments in the deep learning community.
Whether you're an experienced Keras user or just starting your deep learning journey, Keras 3 gives you 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. Explore new features, check out updated documentation, and join the conversation on our GitHub discussion page. Welcome to the next chapter of R deep learning with Keras 3!