A memory-efficient tensor that stores only non-zero values with their indices and shape, optimizing storage and computation for sparse datasets
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.
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:
The shape of the sparse tensor.
On top of that, its main functions are:
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.
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.
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.
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.
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].
Subscribe to our blog and get the latest posts delivered to your inbox.