TensorFlow
Keras
feature columns
machine learning
deep learning

Using a Tensorflow feature_column in a Keras model

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

TensorFlow feature columns were designed to preprocess structured features such as numeric values, bucketized ages, and vocabulary-based categories before passing them into a model. They still appear in older tf.keras examples, but for new Keras code the preferred direction is usually preprocessing layers or FeatureSpace, not feature_column.

When Feature Columns Still Make Sense

Feature columns are mostly a TensorFlow-specific input pipeline tool. They are useful when you are maintaining an existing model that already depends on:

  • 'tf.feature_column.numeric_column'
  • vocabulary-backed categorical columns
  • indicator or embedding columns
  • 'tf.keras.layers.DenseFeatures'

If you are starting fresh, Keras preprocessing layers are easier to debug and fit better with modern Keras workflows. Still, it is important to know how the older pattern works because many production models still use it.

Build A DenseFeatures Input Layer

The classic integration pattern is:

  1. define TensorFlow feature columns
  2. create Keras Input tensors in a dictionary
  3. pass that dictionary through DenseFeatures
  4. connect the transformed tensor to the rest of the model

Here is a minimal example with one numeric feature and one categorical feature:

python
1import tensorflow as tf
2
3age = tf.feature_column.numeric_column("age")
4city = tf.feature_column.categorical_column_with_vocabulary_list(
5    "city", ["Toronto", "Montreal", "Vancouver"]
6)
7city_one_hot = tf.feature_column.indicator_column(city)
8
9feature_columns = [age, city_one_hot]
10
11inputs = {
12    "age": tf.keras.Input(shape=(1,), name="age", dtype=tf.float32),
13    "city": tf.keras.Input(shape=(1,), name="city", dtype=tf.string),
14}
15
16x = tf.keras.layers.DenseFeatures(feature_columns)(inputs)
17x = tf.keras.layers.Dense(16, activation="relu")(x)
18outputs = tf.keras.layers.Dense(1, activation="sigmoid")(x)
19
20model = tf.keras.Model(inputs=inputs, outputs=outputs)
21model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])

The key detail is that the input names must match the feature column keys exactly.

Feeding Data Into The Model

Because the model expects a dictionary of named tensors, training data should use the same structure:

python
1features = {
2    "age": tf.constant([[28.0], [42.0], [35.0]]),
3    "city": tf.constant([["Toronto"], ["Vancouver"], ["Montreal"]]),
4}
5
6labels = tf.constant([1.0, 0.0, 1.0])
7
8model.fit(features, labels, epochs=3, verbose=0)

This part often confuses people because the features are not passed as one matrix. They are passed as a keyed mapping that DenseFeatures knows how to transform.

Embeddings And Bucketization

Feature columns also support transformations that would otherwise be tedious to hand-code. For example, you can bucketize a continuous value or embed a categorical feature:

python
1price = tf.feature_column.numeric_column("price")
2price_buckets = tf.feature_column.bucketized_column(
3    price, boundaries=[50, 100, 200]
4)
5
6product_id = tf.feature_column.categorical_column_with_hash_bucket(
7    "product_id", hash_bucket_size=1000
8)
9product_embedding = tf.feature_column.embedding_column(product_id, dimension=8)
10
11feature_columns = [price_buckets, product_embedding]

This is powerful, but it also makes the preprocessing graph more tightly coupled to TensorFlow-specific APIs.

The Modern Alternative

For new code, many teams now prefer Keras preprocessing layers because they are more explicit and easier to inspect. A rough equivalent might use StringLookup, CategoryEncoding, Normalization, or Discretization.

That version tends to look like normal Keras model code:

python
1import tensorflow as tf
2
3age_input = tf.keras.Input(shape=(1,), name="age")
4city_input = tf.keras.Input(shape=(1,), name="city", dtype=tf.string)
5
6city_lookup = tf.keras.layers.StringLookup(
7    vocabulary=["Toronto", "Montreal", "Vancouver"],
8    output_mode="one_hot",
9)
10
11city_encoded = city_lookup(city_input)
12x = tf.keras.layers.Concatenate()([age_input, city_encoded])
13x = tf.keras.layers.Dense(16, activation="relu")(x)
14output = tf.keras.layers.Dense(1, activation="sigmoid")(x)
15
16modern_model = tf.keras.Model(
17    inputs={"age": age_input, "city": city_input},
18    outputs=output,
19)

If you are using modern Keras patterns, this approach is generally easier to maintain.

How To Choose

Use feature columns when:

  • you are maintaining an existing TensorFlow model that already uses them
  • your serving pipeline depends on the same transformed feature graph
  • you need compatibility with legacy code paths

Prefer preprocessing layers when:

  • you are writing new code
  • you want cleaner model summaries and easier debugging
  • you want to stay closer to current Keras recommendations

Common Pitfalls

  • Mismatched input names. The keys in the input dictionary must match the feature column names exactly.
  • Feeding arrays instead of a dictionary of named tensors.
  • Mixing feature_column code with newer Keras patterns and assuming they are interchangeable.
  • Using feature columns in new code without considering preprocessing layers first.
  • Forgetting that some Keras environments outside TensorFlow will not support TensorFlow-specific feature column utilities.

Summary

  • Feature columns are a legacy but still useful TensorFlow mechanism for structured-data preprocessing.
  • The classic Keras integration pattern uses a dictionary of Input tensors plus tf.keras.layers.DenseFeatures.
  • Input names and data structure must match the feature column definitions exactly.
  • Embeddings, hashed categories, and bucketization are supported, but they increase TensorFlow-specific coupling.
  • For new code, Keras preprocessing layers are usually the cleaner choice.

Course illustration
Course illustration

All Rights Reserved.