Keras Newsletter (April 7, 2023)

keras.utils.FeatureSpace (Keras 2.12 release highlights)

An cleaner way to do feature indexing, preprocessing and encoding.
(Tutorial Link)

feature_space = FeatureSpace(
        # Categorical features encoded as integers
        "sex": FeatureSpace.integer_categorical(num_oov_indices=0),
        "cp": FeatureSpace.integer_categorical(num_oov_indices=0),
        "fbs": FeatureSpace.integer_categorical(num_oov_indices=0),
        "restecg": FeatureSpace.integer_categorical(num_oov_indices=0),
        "exang": FeatureSpace.integer_categorical(num_oov_indices=0),
        "ca": FeatureSpace.integer_categorical(num_oov_indices=0),
        # Categorical feature encoded as string
        "thal": FeatureSpace.string_categorical(num_oov_indices=0),
        # Numerical features to normalize
        "age": FeatureSpace.float_discretized(num_bins=30),
        # Numerical features to normalize
        "trestbps": FeatureSpace.float_normalized(),
        "chol": FeatureSpace.float_normalized(),
        "thalach": FeatureSpace.float_normalized(),
        "oldpeak": FeatureSpace.float_normalized(),
        "slope": FeatureSpace.float_normalized(),
    # Specify feature cross with a custom crossing dim.
        FeatureSpace.cross(feature_names=("sex", "age"), crossing_dim=64),
            feature_names=("thal", "ca"),
  • Specify feature types and feature crosses
  • Call .adapt(dataset) to index feature values
  • Call it on dict data like a layer (e.g. in a pipeline)
  • Easy to customize by creating custom feature types

Example: creating a custom feature to encode a text paragraph with BERT embeddings

Keras V3 Saving Format (.keras) (Keras 2.12 release highlights)

A new saving format is now released in TF/Keras 2.12, marked by the .keras extension. You can start using it with:"your_model.keras", save_format="keras_v3")

More robust, config-based saving.

  • Idempotent saving — “what you reload is what you saved”
  • No reliance on loading via bytecode or pickling — a big advancement for secure ML!
  • Easier debugging via a more detailed serialization format for the model’s config file (JSON)

Wider support for exotic states.

  • Non-numerical states, such as vocabulary files and lookup tables
  • Exotic state elements in custom layers, such as FIFOQueue

Files inside .keras folder:

  • Config JSON
  • Metadata
  • weights files (.weights.h5)

Things to be aware of:

  • Python lambdas are disallowed at loading time.
    • If you trust the source of the model and want to use lambdas, pass safe_mode=False at loading time.
  • Register your custom objects.
    • The new saving format must have access to your custom objects. We recommend using the @keras.utils.register_keras_serializable decorator on the custom object definition.
  • The legacy formats (h5 and SavedModel) will continue to be supported indefinitely.

Note: Starting in TF 2.13,keras_v3 will become default for all files with the .keras extension.

New feature: model.export(filepath) (Keras 2.12 release highlights)

Documentation link

  • Create a lightweight SavedModel archive for serving (e.g. via TF-Serving)
  • Not idempotent!! Also targets Python-less runtimes
  • Customize serving signatures via keras.export.ExportArchive class

KerasNLP text generation (v0.5 preview)

from keras_nlp.models import GPT2CausalLM

model = GPT2CausalLM.from_preset(
    "Snowfall in Buffalo",
>>> 'Snowfall in Buffalo, New York, was expected to reach '
    '2 feet by the end of the day, according to the National '
    'Weather Service.'
  • More in 0.5…
    • Performant generation with XLA by default.
    • Contrastive search (current SOTA for sampling LLMs).
    • Masked language model training.
  • In the works…
    • Seq2Seq: T5 and BART.
    • Audio transcriber: Whisper.


Preview of unified KerasCV / KerasNLP API

from keras_cv.models import (
    ResNetBackbone, ImageClassifier,
backbone = ResNetBackbone.from_preset(
model = ImageClassifier(
from keras_cv.models import (
    ResNetBackbone, RetinaNet,
backbone = ResNetBackbone.from_preset(
model = RetinaNet(

Announcement: import keras is back

  • import keras is the recommended import style going forward instead of from tensorflow import keras.
  • Already on in keras-nightly
  • Will become standard in 2.13
  • Only exposes the public API
  • If you want to keep accessing private API symbols (please don’t):
    • Use keras.src namespace
      • e.g. keras.utils.tf_utilskeras.src.utils.tf_utils
    • (But seriously, just stick to the public API)

Community project highlights

  • Combine Transformers and RNNs to improve generalization. (link)
  • The Capsa project: A data- and model-agnostic neural network wrapper for risk-aware decision making. (link)

Very interesting feature!
Regarding the KerasCV API, ImageClassifier, do we have access to it right now through a nightly version of keras_cv?

1 Like

I love this setup! It was really weird having to repeat adapt for each feature.
Is it possible to export the feature space preprocessor to an environment without tensorflow, just with a runtime like a model in .keras or a TF Lite model?

If vocabulary files and lookup tables are going into the .keras models does that mean that it will be possible to include unicode normalization and tokenization in models without having all of TF installed when doing inference?


Yes, it is available now although we are still in the process of converting all our classifier Backbones.

1 Like