TensorFlow
feature columns
machine learning
variable list
data preprocessing

Tensorflow feature column for variable list of values

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

A variable-length list feature means each example may carry zero, one, or many values for the same field. In older TensorFlow input pipelines built around feature columns, the normal way to represent that kind of input is a sparse feature, because dense fixed-width tensors do not naturally fit lists of different lengths.

The Core Idea: Use A Sparse Representation

Suppose each example contains a variable list of category ids such as purchased products or tag ids. With feature columns, you typically model that as a sparse categorical feature and then convert it into either a one-hot style indicator or an embedding.

python
1import tensorflow as tf
2
3product_column = tf.feature_column.categorical_column_with_identity(
4    key="product_ids",
5    num_buckets=100,
6)
7
8product_embedding = tf.feature_column.embedding_column(
9    categorical_column=product_column,
10    dimension=8,
11)

The important part is that the input under product_ids is not a scalar per example. It is a variable-length list represented as a sparse tensor.

Building A SparseTensor Example

python
1product_ids = tf.SparseTensor(
2    indices=[[0, 0], [0, 1], [1, 0], [2, 0], [2, 1], [2, 2]],
3    values=[3, 7, 5, 2, 8, 9],
4    dense_shape=[3, 3],
5)
6
7features = {"product_ids": product_ids}

This describes three examples:

  • example 0 has values 3 and 7
  • example 1 has value 5
  • example 2 has values 2, 8, and 9

The missing positions are simply absent rather than padded with a fake value.

Turning The Sparse Feature Into Model Input

A dense feature layer can consume the embedding column.

python
feature_layer = tf.keras.layers.DenseFeatures([product_embedding])
output = feature_layer(features)
print(output.shape)

TensorFlow combines the variable-length ids into one dense representation per example. For embeddings, that typically means a pooled embedding across the example's ids.

String Values Instead Of Integer Ids

If your list values are strings instead of integer ids, use a vocabulary-based or hash-based categorical column.

python
1keyword_column = tf.feature_column.categorical_column_with_hash_bucket(
2    key="keywords",
3    hash_bucket_size=1000,
4)
5
6keyword_indicator = tf.feature_column.indicator_column(keyword_column)

The input is still sparse. The difference is how TensorFlow maps raw values to buckets.

Why Feature Columns Feel Awkward Today

Feature columns still appear in legacy code, but modern TensorFlow often prefers Keras preprocessing layers such as StringLookup, IntegerLookup, CategoryEncoding, and explicit RaggedTensor pipelines. Those APIs are usually easier to compose, especially in end-to-end Keras models.

Still, if you are working in an existing estimator or feature-column codebase, sparse tensors are the key concept for variable-length list inputs.

A Modern Keras Alternative

python
1inputs = tf.keras.Input(shape=(None,), ragged=True, dtype=tf.int32)
2embedding = tf.keras.layers.Embedding(input_dim=100, output_dim=8)(inputs)
3pooled = tf.reduce_mean(embedding, axis=1)
4model = tf.keras.Model(inputs, pooled)

This approach often feels more direct than feature columns because the variable-length structure is explicit in the model input.

When Sequence Semantics Matter

If order matters, a pooled multi-hot representation may be too weak. A feature column that collapses a set of ids into a bag-of-values representation loses the distinction between first, second, and third positions. In that case, sequence models with ragged inputs, padding, and masking are usually more appropriate than plain feature columns.

Common Pitfalls

The most common mistake is trying to feed a plain dense scalar tensor into a feature column that really expects multiple values per example. Another is padding variable-length lists with a fake id and then forgetting that the model now treats that fake id as real input. Developers also often overlook that feature columns are an older API and may not be the best choice for new Keras-first projects. Finally, pooled representations discard order, so they are not a good fit when the sequence position carries meaning.

Summary

  • Variable-length list features are usually represented as sparse tensors in feature-column pipelines.
  • Use categorical columns plus indicator or embedding columns to convert those lists into model features.
  • Sparse input works for both integer ids and string categories.
  • Feature columns are still useful in legacy pipelines, but newer Keras preprocessing APIs are often cleaner.
  • If order matters, use a true sequence model rather than a bag-of-values feature representation.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.