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 can contain a different number of values, such as a list of viewed products, clicked tags, or token ids. In older TensorFlow input pipelines, feature columns could help represent this kind of data, but you had to choose the right column type and parsing strategy.
The key distinction is whether the feature is sparse categorical data or a real sequence for models such as RNNs. Those use related but different APIs.
Parse Variable-Length Features Correctly
For tf.Example input, variable-length lists are often parsed with VarLenFeature or RaggedFeature depending on the pipeline style.
This produces a sparse tensor, which is a common starting point for variable-length categorical values.
Use a Categorical Column for Multi-Value Sparse Input
If each example contains a bag or set of categorical values rather than a time sequence, an older feature-column pipeline might look like this:
This is useful when an example can contain multiple tags and you want a multi-hot style representation.
The parsed VarLenFeature input works well here because sparse categorical columns expect sparse-style data.
Sequence Feature Columns Are for Ordered Sequences
If the feature is truly ordered sequence data, use sequence-oriented APIs instead of treating it like an unordered bag.
This older API was designed for sequential model inputs where order matters.
That distinction matters:
- use ordinary categorical columns for unordered multi-value membership
- use sequence feature columns when order and timestep structure matter
Modern TensorFlow Note
Feature columns still appear in older TensorFlow codebases, especially Estimator-based pipelines, but newer Keras workflows often use preprocessing layers, ragged tensors, embeddings, and direct tensor operations instead.
For many modern projects, a Keras-native approach is clearer.
This is often easier to reason about than an older feature-column stack.
Choosing the Right Representation
Ask these questions first:
- Is the list ordered or unordered
- Are values numeric or categorical
- Do you need sparse, ragged, or embedded output
- Are you using Estimator-style code or Keras
The answers determine whether a feature column is still the right tool.
Common Pitfalls
The biggest mistake is treating all variable-length lists as sequences. Many are really unordered multi-value categorical features.
Another common issue is parsing the input incorrectly. Sparse categorical columns generally expect sparse-style parsed data, while ragged and Keras-native pipelines may want different input shapes.
People also reach for feature columns in modern TensorFlow code even when preprocessing layers would be simpler.
Finally, do not confuse sequence feature columns with ordinary categorical columns. They encode different modeling assumptions.
Summary
- Variable-length list features usually start with sparse or ragged parsing.
- Use ordinary categorical columns for unordered multi-value features.
- Use sequence feature columns only when order matters.
- Feature columns are common in older Estimator code, but Keras preprocessing layers are often cleaner in modern TensorFlow.
- Parse the input format to match the representation your column or layer expects.
- Choose the API based on modeling semantics, not just on input shape.
Related reading
- 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 Finetune pretrained model on new dataset with different number of classes
- Tensorflow flatten vs numpy flatten function effect on machine learning training
- Tensorflow How can I assign numpy pre-trained weights to subsections of graph?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free 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.