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.
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.
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
This describes three examples:
- example 0 has values
3and7 - example 1 has value
5 - example 2 has values
2,8, and9
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.
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.
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
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
- Tensorflow feature column for variable list of values
- TensorFlow Federated How to tune non-IIDness in federated dataset?
- Tensorflow 'feed_dict' using same symbol for key-value pair got 'TypeError Cannot interpret feed_dict key as Tensor
- tensorflow feed list feature multi-hot to tf.estimator
- TensorFlow Feeding data with queue vs with direct feeding with feed_dict
- Tensorflow, feeding Estimator.fitbatch
- Tensorflow flatten vs numpy flatten function effect on machine learning training
- Tensorflow How can I assign numpy pre-trained weights to subsections of graph?

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 courseTrack 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.