Skip to main content

What is a Sparse Tensor?

A memory-efficient tensor that stores only non-zero values with their indices and shape, optimizing storage and computation for sparse datasets

by Databricks Staff

  • A sparse tensor is a way of representing data where most values are zero by storing only the non zero entries and their positions instead of every element in the array.
  • This design can dramatically cut memory usage and computation for very large, mostly empty datasets such as text embeddings, recommendation matrices or scientific measurements.
  • Sparse tensor libraries provide functions to convert between sparse and dense forms, count non zero values and reshape or permute dimensions so developers can work efficiently with high dimensional data.

Python offers an inbuilt library called numpy to manipulate multi-dimensional arrays. The organization and use of this library is a primary requirement for developing the pytensor library. Sparse Tensor Sptensor is a class that represents the sparse tensor. A sparse tensor is a dataset in which most of the entries are zero, one such example would be a large diagonal matrix. (which has many zero elements). It does not store the whole values of the tensor object but stores the non-zero values and the corresponding coordinates of them. Sparse tensor storage formats allow us to only store non-zero values thereby reducing storage requirements and eliminating unnecessary silent computations involving zero values. Here are its main attributes:

  • vals (numpy.ndarray) A 1-dimensional array of non-zero values of the sparse tensor.
  • subs (numpy.ndarray) A 2-dimensional array of coordinates of the values in vals.
  • shape(tuple)

The shape of the sparse tensor.

  • func(binary operator) This function is used to construct the sparse tensor as an accumulator.

On top of that, its main functions are:

  • __init__(self, subs, vals, shape = None, func=sum.__call__) Consturctor for the sptensor class. subs and vals (numpy.ndarray) or (list) are coordinates and values of the sptensor.
  • tondarray(self) This function returns a numpy. ndarray object that has the same values with the sptensor.
  • permute(self, order) By applying this function it will return the sptensor object that is permuted by the given order (list).
  • ipermute(self, order) Returns the sptensor object that is permuted by the inverse of the given order (list).
  • copy(self) Returns the copied sptensor object of the sptensor.
  • totensor(self) Returns the tensor object that has the same values with the sptensor.
  • nnz(self) Returns the number of non-zero elements in the sptensor.
  • ndims(self) Returns the number of dimensions of the tensor.
  • dimsize(self, ind)
  • Returns the size of the specified dimension. Same as shape[ind].

Additional resources on sparse tensors


REPORT

The agentic AI playbook for the enterprise

Frequently asked questions

What makes a tensor "sparse" instead of dense?

A tensor is sparse when most of its entries are zero, as in a large diagonal matrix, and the sparse representation stores only the non-zero values along with their coordinates instead of every entry in the array. This is different from a dense tensor, which explicitly stores every value, including all the zeros. Because so much of a sparse dataset's content is empty, capturing only the meaningful entries avoids storing repetitive zero values.

Why use a sparse tensor instead of a regular dense array?

Sparse tensors are used because they reduce storage requirements and eliminate unnecessary computations involving zero values. This design can dramatically cut memory usage and computation for very large, mostly empty datasets such as text embeddings, recommendation matrices or scientific measurements. By skipping the zero entries entirely, sparse tensor storage formats let developers work efficiently with high dimensional data that would otherwise be too costly to process in dense form.

What information does the sptensor class actually store?

The sptensor class stores non-zero values, their coordinates, the tensor's shape, and an accumulator function rather than a full array of every element. Specifically, vals is a 1-dimensional array of the non-zero values, subs is a 2-dimensional array of the coordinates corresponding to those values, and shape is a tuple describing the tensor's overall shape. The func attribute is a binary operator used to construct the sparse tensor as an accumulator when values are combined.

How do you convert a sparse tensor back into a dense form?

The sptensor class provides two functions for this: tondarray, which returns a numpy.ndarray object holding the same values as the sptensor, and totensor, which returns a tensor object with the same values as the sptensor. Both let developers move from the compact sparse representation to a full dense structure when a dense format is needed for further processing. This conversion capability is one of the core functions sparse tensor libraries provide alongside reshaping and dimension counting.

What other operations can be performed on a sparse tensor?

Beyond converting to dense form, the sptensor class supports functions for reordering, copying, and inspecting the tensor's structure. permute returns the sptensor object permuted by a given order, while ipermute returns it permuted by the inverse of that order; copy returns a duplicate of the sptensor object. Additionally, nnz returns the number of non-zero elements, ndims returns the number of dimensions, and dimsize returns the size of a specified dimension, equivalent to shape[ind].

Get the latest posts in your inbox

Subscribe to our blog and get the latest posts delivered to your inbox.