Putting preprocessing in layers keeps training and serving consistent and lets the model accept raw inputs.
Preprocessing layers turn raw features into model-ready tensors. Many have state learned from data with `adapt(data)` before training:
Normalization(axis=-1): learns per-feature mean and variance, then outputs(x - mean) / std.Discretization(bin_boundaries=...)ornum_bins=: buckets numeric values.StringLookupandIntegerLookup: map categories to indices, with an out-of-vocabulary slot (index 0 for one-hot output, 1 for integer output when a mask token is present).output_mode="one_hot"or"multi_hot"gives encodings directly.TextVectorization: standardises, tokenises and indexes text.Hashing: for huge or open-ended category sets.- Image layers:
Resizing,Rescaling(1/255),CenterCrop.
You can place them inside the model, so the saved model accepts raw strings and numbers and serving cannot drift from training, or in the input pipeline (dataset.map(layer)), where they run on the CPU asynchronously.
Some of these layers (the lookup and text layers) are implemented with TensorFlow ops; with the JAX or PyTorch backend they work eagerly and inside tf.data pipelines, but not inside a compiled non-TF model, so put them in the data pipeline there.
Example
import numpy as np
import keras
from keras import layers
data = np.array([[1.0, 200.0], [2.0, 300.0], [3.0, 400.0]], dtype="float32")
norm = layers.Normalization(axis=-1)
norm.adapt(data) # learns mean and variance per column
print(keras.ops.convert_to_numpy(norm(data)).round(2))
# [[-1.22 -1.22]
# [ 0. 0. ]
# [ 1.22 1.22]]
lookup = layers.StringLookup(vocabulary=["red", "green", "blue"], output_mode="one_hot")
print(keras.ops.convert_to_numpy(lookup(np.array(["green", "purple"]))))
# [[0. 0. 1. 0.]
# [1. 0. 0. 0.]] column 0 = out-of-vocabularyRemember
adapt()learns state such as mean/variance or vocabularies- Inside the model: consistent serving; in tf.data: async CPU work
- Lookup layers reserve an out-of-vocabulary index
Watch out for
- Adapting a Normalization layer on the full dataset including test data, leaking test statistics.