Sparse Features
TensorFlow
Data Representation
Machine Learning
Neural Networks

How to represent list of sparse features in tensorflow?

Master System Design with Codemia

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

Introduction

In TensorFlow, the right representation for sparse features depends on what the feature actually means. If you have a high-dimensional mostly-empty vector, use tf.sparse.SparseTensor. If you have a variable-length list of IDs per example, a tf.RaggedTensor is often a better fit until you need a truly sparse representation for model input.

Use SparseTensor for genuinely sparse coordinates

A SparseTensor stores only the nonzero or present entries instead of storing an entire dense array full of zeros. It is defined by three parts:

  • 'indices'
  • 'values'
  • 'dense_shape'

Here is a simple example:

python
1import tensorflow as tf
2
3sparse = tf.sparse.SparseTensor(
4    indices=[[0, 1], [0, 3], [1, 2]],
5    values=[1.0, 2.0, 5.0],
6    dense_shape=[2, 5],
7)
8
9print(tf.sparse.to_dense(sparse))

This represents a batch of two examples with five possible feature positions. Only three positions are actually present, so the sparse form is much more efficient than storing a full dense matrix.

Use RaggedTensor for variable-length feature lists

Many recommendation and NLP features are not best thought of as sparse coordinates first. They are lists of token IDs, category IDs, or clicked item IDs, and each example may have a different list length.

That is a natural fit for RaggedTensor:

python
1import tensorflow as tf
2
3feature_ids = tf.ragged.constant([
4    [12, 18, 42],
5    [7],
6    [3, 9]
7])
8
9print(feature_ids)

This is often easier to work with during preprocessing. Later, if your model or feature transformation requires a sparse representation, you can convert:

python
sparse_ids = feature_ids.to_sparse()
print(sparse_ids)

That makes RaggedTensor a good staging format when the data starts as lists but eventually needs sparse-compatible operations.

Model sparse features in Keras inputs

If your model expects sparse input directly, define the Keras input accordingly:

python
1import tensorflow as tf
2
3inputs = tf.keras.Input(shape=(1000,), sparse=True, name="features")
4x = tf.keras.layers.Dense(32, activation="relu")(inputs)
5outputs = tf.keras.layers.Dense(1)(x)
6
7model = tf.keras.Model(inputs=inputs, outputs=outputs)
8model.summary()

This setup is appropriate when each example is conceptually a 1000-dimensional sparse feature vector and only a few positions are present.

You can then feed a SparseTensor into the model:

python
1batch = tf.sparse.SparseTensor(
2    indices=[[0, 10], [0, 50], [1, 3]],
3    values=[1.0, 2.0, 4.0],
4    dense_shape=[2, 1000],
5)
6
7print(model(batch))

Represent ID lists differently from multi-hot vectors

This distinction matters:

  • A multi-hot feature over a known vocabulary is often best represented as a sparse vector.
  • A list of IDs with varying length is often easier as ragged data first.

For example, suppose each user has a list of viewed category IDs. If you need embedding lookup, keeping the IDs as a ragged list may be more natural than expanding them into a giant multi-hot vector immediately.

If instead the model expects a bag-of-features vector over a fixed space, sparse vector form is usually more direct.

Convert carefully in input pipelines

TensorFlow input pipelines often start from Python lists or parsed examples. Here is one way to build a sparse feature from ordinary Python data:

python
1import tensorflow as tf
2
3indices = [[0, 2], [0, 5], [1, 1]]
4values = [1.0, 1.0, 1.0]
5dense_shape = [2, 6]
6
7dataset = tf.data.Dataset.from_tensors(
8    tf.sparse.SparseTensor(indices=indices, values=values, dense_shape=dense_shape)
9)
10
11for item in dataset:
12    print(tf.sparse.to_dense(item))

The important part is that the structure must be consistent with the declared shape. Sparse bugs often come from mismatched indices or incorrectly assumed feature dimensions.

Common Pitfalls

The biggest mistake is using dense tensors for extremely sparse data just because they are easier to print. That wastes memory and can slow the pipeline down.

Another common issue is choosing SparseTensor when the data is really a variable-length list and would be easier to manage as RaggedTensor until a later transformation step.

People also get tripped up by index format. indices must point to actual coordinates in the dense shape, and they must align with the number of values provided.

Finally, not every TensorFlow or Keras layer handles sparse input the same way. Check whether a layer truly supports sparse tensors before building the whole pipeline around that assumption.

Summary

  • Use tf.sparse.SparseTensor for high-dimensional mostly-empty feature vectors.
  • Use tf.RaggedTensor for variable-length lists of feature IDs.
  • Convert ragged lists to sparse form only when the model or preprocessing step needs it.
  • Keras models can accept sparse inputs with sparse=True on the input layer.
  • Choose the representation that matches the semantic meaning of the feature, not just the storage format.

Course illustration
Course illustration

All Rights Reserved.