View on TensorFlow.org
|
Run in Google Colab
|
View on GitHub
|
Download notebook
|
When working with tensors that contain a lot of zero values, it is important to store them in a space- and time-efficient manner. Sparse tensors enable efficient storage and processing of tensors that contain a lot of zero values. Sparse tensors are used extensively in encoding schemes like TF-IDF as part of data pre-processing in NLP applications and for pre-processing images with a lot of dark pixels in computer vision applications.
Sparse tensors in TensorFlow
TensorFlow represents sparse tensors through the tf.sparse.SparseTensor object. Currently, sparse tensors in TensorFlow are encoded using the coordinate list (COO) format. This encoding format is optimized for hyper-sparse matrices such as embeddings.
The COO encoding for sparse tensors is comprised of:
values: A 1D tensor with shape[N]containing all nonzero values.indices: A 2D tensor with shape[N, rank], containing the indices of the nonzero values.dense_shape: A 1D tensor with shape[rank], specifying the shape of the tensor.
A nonzero value in the context of a tf.sparse.SparseTensor is a value that's not explicitly encoded. It is possible to explicitly include zero values in the values of a COO sparse matrix, but these "explicit zeros" are generally not included when referring to nonzero values in a sparse tensor.
Creating a tf.sparse.SparseTensor
Construct sparse tensors by directly specifying their values, indices, and dense_shape.
import
View on TensorFlow.org
Run in Google Colab
View on GitHub
Download notebook