liveBook · Manning

Keras preprocessing layer

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

Keras preprocessing layers are neural-network layers that transform input features, such as by normalization or categorical encoding, within a Keras model.

A Keras preprocessing layer transforms tabular features inside a Keras model, reducing the need for separate custom preprocessing pipelines. Continuous inputs can be normalized, while categorical inputs can be looked up and encoded before reaching the model. section 9.3 section 9.3.2

A practical tabular workflow uses preprocessing layers to:

  • Convert DataFrame rows into batched tf.data.Dataset objects, optionally shuffling training data and prefetching batches. section 9.3.2
  • Adapt a Normalization layer to each continuous feature using training data. section 9.3.2
  • Adapt StringLookup or IntegerLookup and apply CategoryEncoding to categorical features. section 9.3.2
  • Concatenate processed features and pass them to ordinary Dense and Dropout layers. section 9.3.2
Table 9.1 Comparison of the Kuala Lumpur real solutions using custom classes and using Keras preprocessing layers (view table figure)

Using custom layers

Using Keras preprocessing layers

Pipeline

Custom pipeline classes based on Scikit-learn Pipeline class (https://mng.bz/pKP5); requires classes to be defined in a separate file so they can be used at training and inference (https://mng.bz/OBxK); complex code to define, train, invoke, and save the pipelines.

The standard, off-the-shelf Keras preprocessing layers, available at https://mng.bz/YD1o. The code to define, train, and invoke the pipeline is much simpler and more robust.

Model definition

A complex set of layers, specified to work with categorical, continuous, and text inputs

A simple set of layers, capable of working with generic categorical and continuous inputs

Compare custom-layer pipelines with Keras preprocessing layers across pipeline complexity and model-definition simplicity. section 9.3.1

Kuala Lumpur property-price prediction

A property-price model separates continuous columns such as Bathrooms, Car Parks, Rooms, and Size from categorical columns such as Location, Property Type, Furnishing, and Size_type_bin. Preprocessing layers normalize the continuous columns, encode the categorical columns, and feed the combined representation into a compact neural network. Rename columns to lowercase snake_case before saving the model, because spaces and uppercase names can cause saving errors. section 9.3.2

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Explains dataset creation, feature adaptation, categorical encoding, model assembly, and a column-naming constraint, without deriving layer internals.
Examples
Uses Kuala Lumpur real-estate price prediction with property size, rooms, location, furnishing, and property-type features.
What it assumes
Assumes familiarity with DataFrames, continuous and categorical columns, Keras models, and basic TensorFlow dataset operations.
What you can do after
Build a Keras tabular model that batches DataFrames, adapts feature transformations, encodes categories, and saves with compatible column names.
How it connects
Connects tabular data, TensorFlow datasets, normalization, categorical encoding, custom layers, and Scikit-learn pipelines.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.